Overview of the Java UI |
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 all orchestrated by the AWT drawing system.When the Converter application draws itself, here's what happens:
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.
- The Frame draws itself.
- The Converter object draws itself, drawing a box around its area.
- 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.- The contents of the ConversionPanel -- the Label, TextField, Scrollbar, and Choice -- draw themselves.
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, however, use therepaint()
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 theupdate()
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'spaint()
method. The default implementation of thepaint()
method does nothing.
The Graphics Object
The only argument to thepaint()
andupdate()
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:
- Drawing and filling rectangles, arcs, lines, ovals, polygons, text, and images.
- Getting or setting the current color, font, or clipping area.
- Setting the paint mode.
How to Draw
The simplest way for a Component to draw itself is to put drawing code in itspaint()
method. This means that when the AWT makes a drawing request (by calling the Component'supdate()
method, which is implemented as described above), the Component's entire area is cleared and then itspaint()
method is called. For programs that don't repaint themselves often, the performance of this scheme is fine.Important: The
paint()
andupdate()
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 ControlBelow is an example of an implementation of 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 panel's contents. If they didn't have this method, the box drawn in thepaint()
method would overlap the external boundaries of the panel's contents.)Programs that repaint themselves often can use two techniques to improve their performance: implementing bothpublic void paint(Graphics g) { Dimension d = size(); g.drawRect(0,0, d.width - 1, d.height - 1); }update()
andpaint()
, and using double-buffering. These techniques are discussed [nowhere yet].
Overview of the Java UI |