Java Reference
In-Depth Information
The paintBorder() and paintChildren() methods tend not to be overridden. The
paintBorder() method draws a border around the component, a concept described more
fully in Chapter 7. The paintChildren() method draws the components within the Swing
container object, if any are present.
To optimize painting, the JComponent class provides three additional painting properties:
opaque , optimizedDrawingEnabled , and doubleBuffered . These work as follows:
Opacity: The opaque property for a JComponent defines whether a component is trans-
parent. When transparent, the container of the JComponent must paint the background
behind the component. To improve performance, you can leave the JComponent opaque
and let the JComponent draw its own background, instead of relying on the container to
draw the covered background.
Optimization: The optimizedDrawingEnabled property determines whether immediate
children can overlap. If children cannot overlap, the repaint time is reduced consider-
ably. By default, optimized drawing is enabled for most Swing components, except for
JDesktopPane , JLayeredPane , and JViewport .
Double buffering: By default, all Swing components double buffer their drawing operations
into a buffer shared by the complete container hierarchy; that is, all the components
within a window (or subclass). This greatly improves painting performance, because
when double buffering is enabled (with the doubleBuffered property), there is only a
single screen update drawn.
Note For synchronous painting, you can call one of the public void paintImmediately() methods.
(Arguments are either a Rectangle or its parts—position and dimensions.) However, you'll rarely need to
call this directly unless your program has real-time painting requirements.
The public void revalidate() method of JComponent also offers painting support. When
this method is called, the high-level container of the component validates itself. This is unlike
the AWT approach requiring a direct call to the revalidate() method of that high-level
component.
The last aspect of the Swing component painting enhancements is the RepaintManager
class.
RepaintManager Class
The RepaintManager class is responsible for ensuring the efficiency of repaint requests on the
currently displayed Swing components, making sure the smallest “dirty” region of the screen is
updated when a region becomes invalid.
Although rarely customized, RepaintManager is public and provides a static installation
routine to use a custom manager: public static void setCurrentManager(RepaintManager
manager) . To get the current manager, just ask with public static void currentManager
(JComponent) . The argument is usually null , unless you've customized the manager to provide
component-level support. Once you have the manager, one thing you can do is get the off-
screen buffer for a component as an Image . Because the buffer is what is eventually shown on
 
Search WWH ::




Custom Search