Previous | Next | Trail Map | Creating a User Interface (AWT Only) | Overview of the Java UI

Drawing

When a Java program with a GUI needs to draw itself -- whether for the first time, or in response to becoming unhidden or because its appearance needs to change to reflect something happening inside the program -- it starts with the highest component that needs to be redrawn (for example, the top Component in the hierarchy) and works its way down to the bottom-most Components. This is orchestrated by the AWT drawing system.

Here, as a reminder, is the component hierarchy for the converter program:

When the Converter application draws itself, here's what happens:

  1. The Frame draws itself.
  2. The Converter object draws itself, drawing a box around its area.
  3. One of the two ConversionPanels draws itself, drawing a box around its area.
    Note: You can't rely on the order that two Components at the same level will be drawn in. For example, you can't rely on the metric panel being drawn before the U.S. one. Similarly, you can't rely on the order of drawing two Components at different levels if the lower Component isn't contained in the higher Component.
  4. The contents of the ConversionPanel -- the Label, TextField, Scrollbar, and Choice -- draw themselves.
In this way, each Component draws itself before any of the Components it contains. This ensures that a Panel's background, for example, is visible only where it isn't covered by one of the Components it contains.

How Drawing Requests Occur

Programs can draw only when the AWT tells them to. The reason is that each occurrence of a Component drawing itself must execute without interruption. Otherwise, unpredictable results could occur, such as a button being drawn halfway, and then being interrupted by some lengthy animation. The AWT orders drawing requests by making them run in a single thread. A Component can use the repaint() method to request to be scheduled for drawing.

The AWT requests that a Component draw itself by invoking the Component's update() method. The default (Component) implementation of the update() method simply clears the Component's background (drawing a rectangle over the component's clipping area in the Component's background color) and then calling the Component's paint() method. The default implementation of the paint() method does nothing.

The Graphics Object

The only argument to the paint() and update() methods is a Graphics object that represents the context in which the Component can perform its drawing. The Graphics class provides methods for the following:

How to Draw

The simplest way for a Component to draw itself is to put drawing code in its paint() method. This means that when the AWT makes a drawing request (by calling the Component's update() method, which is implemented as described above), the Component's entire area is cleared and then its paint() method is called. For programs that don't repaint themselves often, the performance of this scheme is fine.

Important: The paint() and update() methods must execute very quickly! Otherwise, they'll destroy the perceived performance of your program. If you need to perform some lengthy operation as the result of a paint request, do it by starting up another thread (or somehow sending a request to another thread) to perform the operation. For help on using threads, see Threads of Control(in the Learning the Java Language trail).

Below is an example of implementing the paint() method. Both the Converter and ConversionPanel classes draw a box around their area using this code. Both classes also implement an insets() method that specifies the padding around the panels' contents. If they didn't have this method, the box drawn in the paint() method would overlap the external boundaries of the panels' contents.

public void paint(Graphics g) {
    Dimension d = size();
    g.drawRect(0,0, d.width - 1, d.height - 1);
}

Programs that repaint themselves often can use two techniques to improve their performance: implementing both update() and paint(), and using double buffering. These techniques are discussed in Eliminating Flashing(in the Creating a User Interface trail).

For more information on how to draw, see the Working with Graphics(in the Creating a User Interface trail) lesson.


Previous | Next | Trail Map | Creating a User Interface (AWT Only) | Overview of the Java UI