Previous | Next | Trail Map | Creating a User Interface (with Swing) | Working with Graphics

Overview of AWT Graphics Support

As you learned from the first lesson in this trail, in the Drawing(in the Creating a User Interface trail) page, the AWT drawing system controls when and how programs can draw. In response to a Component's repaint() method being called, the AWT invokes the Component's update() method to request that the Component redraw itself. The update() method in turn (by default) invokes the Component's paint() method.

An additional wrinkle in this system is that sometimes the AWT calls the paint() method directly, instead of calling update(). 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 about paint() and update() in the Eliminating Flashing discussion, later in this lesson.

The Graphics Object

The lone argument to the paint() and update() methods is a Graphics(in the API reference documentation) 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's repaint() method to request that the AWT call the Component's update() method. Here are descriptions of the four forms of the repaint() 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 of repaint().
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.


Previous | Next | Trail Map | Creating a User Interface (with Swing) | Working with Graphics