The graphic context maintains states (or attributes) such as the current painting color, the current font for drawing text strings, and the current painting rectangular area (called clip). Graphics Class' Methods for Maintaining the Graphics Context Shapes with negative width and height will not be painted. The drawXxx() methods draw the outlines while fillXxx() methods fill the internal.
These drawing methods is illustrated below. Drawing (or printing) texts on the graphics screen:ĭrawString(String str, int xBaselineLeft, int yBaselineLeft) ĭrawLine(int x1, int y1, int x2, int y2) ĭrawPolyline(int xPoints, int yPoints, int numPoint) ĭrawRect(int xTopLeft, int yTopLeft, int width, int height) ĭrawOval(int xTopLeft, int yTopLeft, int width, int height) ĭrawArc(int xTopLeft, int yTopLeft, int width, int height, int startAngle, int arcAngle) ĭraw3DRect(int xTopLeft, int, yTopLeft, int width, int height, boolean raised) ĭrawRoundRect(int xTopLeft, int yTopLeft, int width, int height, int arcWidth, int arcHeight)ĭrawPolygon(int xPoints, int yPoints, int numPoint) įillRect(int xTopLeft, int yTopLeft, int width, int height) įillOval(int xTopLeft, int yTopLeft, int width, int height) įillArc(int xTopLeft, int yTopLeft, int width, int height, int startAngle, int arcAngle) įill3DRect(int xTopLeft, int, yTopLeft, int width, int height, boolean raised) įillRoundRect(int xTopLeft, int yTopLeft, int width, int height, int arcWidth, int arcHeight)įillPolygon(int xPoints, int yPoints, int numPoint) ĭrawImage(Image img, int xTopLeft, int yTopLeft, ImageObserver obs) // draw image with its sizeĭrawImage(Image img, int xTopLeft, int yTopLeft, int width, int height, ImageObserver o) // resize image on screen
In Java, custom painting is done via the class, which manages a graphics context, and provides a set of device-independent methods for drawing texts, figures and images on the screen on different platforms. The graphics context maintains states such as the color and font used in drawing, as well as interacting with the underlying operating system to perform the drawing. The Class: Graphics Context and Custom PaintingĪ graphics context provides the capabilities of drawing on the screen. Read "Swing Tutorial" trail " Performing Custom Painting". Nonetheless, custom graphics is crucial in game programming. I shall stress that you should try to reuse the standard GUI components as far as possible and leave custom graphics as the last resort. This chapter shows you how you can paint your own custom drawing (such as graphs, charts, drawings and, in particular, computer game avatars) because you cannot find standard GUI components that meets your requirements.