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 orchestrated by the AWT drawing system.Here, as a reminder, is the component hierarchy for the converter program:
a Frame | ... | a Converter | ---------------------------------- | | a ConversionPanel (metricPanel) a ConversionPanel (usaPanel) | | ------------------- ------------------- | | | | | | a Label | a Choice a Label | a Choice | | -------------- -------------- | | | | a TextField a Scrollbar a TextField a ScrollbarWhen 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 use therepaint()
method to request to be scheduled for drawing.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 Control.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 aninsets()
method that specifies the padding around the panels' contents. If they didn't have this method, the box drawn in thepaint()
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()
andpaint()
, and using double buffering. These techniques are discussed in Eliminating Flashing.For more information on how to draw, see the Working with Graphics lesson.
Overview of the Java UI |