Working with Graphics |
As you learned from the first lesson in this trail, in the Drawing page, the AWT drawing system controls when and how programs can draw. In response to a Component'srepaint()
method being called, the AWT invokes the Component'supdate()
method to request that the Component redraw itself. Theupdate()
method in turn (by default) invokes the Component'spaint()
method.An additional wrinkle in this system is that sometimes the AWT calls the
paint()
method directly, instead of callingupdate()
. This almost always happens as a result of the AWT reacting to an external stimulus, such as the Component first appearing onscreen, or the Component being uncovered by another window. You'll learn more aboutpaint()
andupdate()
in the Eliminating Flashing discussion, later in this lesson.The Graphics Object
The lone argument to thepaint()
andupdate()
methods is a Graphics object. Graphics objects are the key to all drawing. They support the two basic kinds of drawing: primitive graphics (such as lines, rectangles, and text) and images. You'll learn about primitive graphics in Using Graphics Primitives. You'll learn about images in Using Images.Besides supplying methods to draw primitive graphics and images to the screen, a Graphics object provides a drawing context by maintaining state such as the current drawing area and the current drawing color. You can decrease the current drawing area by clipping it, but you can never increase the drawing area. In this way, the Graphics objects ensure that Components can draw only within their own drawing area. You'll learn more about clipping in Overriding the update() Method.
The Coordinate System
Each Component has its own integer coordinate system, ranging from (0, 0) to (width - 1, height - 1), with each unit representing the size of one pixel. As the following figure shows, the upper left corner of the Component's drawing area is (0, 0). The X coordinate increases to the right, and the Y coordinate increases downward.Here's an applet that we'll build on later in this lesson. Whenever you click within the framed area, the applet draws a dot where you clicked and displays a string describing where the click occurred.
The Four Forms of the repaint() Method
Remember that programs can call a Component'srepaint()
method to request that the AWT call the Component'supdate()
method. Here are descriptions of the four forms of therepaint()
method:
public void repaint()
- Requests that the AWT call the Component's
update()
method as soon as possible. This is the most frequently used form ofrepaint()
.public void repaint(long time)
- Requests that the AWT call the Component's
update()
method as much as time milliseconds from now.public void repaint(int x, int y, int width, int height)
- Requests that the AWT call the Component's
update()
method as soon as possible, but repaint only the specified part of the component.public void repaint(long time, int x, int y, int width, int height)
- Requests that the AWT call the Component's
update()
method as much as time milliseconds from now, but repaint only the specified part of the component.
Working with Graphics |