applications that need this capability. To write output directly to the surface of a component,
you will use one or more drawing methods defined by the AWT, such as drawLine( ) or
drawRect( ). Thus, most of the techniques and methods described in Chapter 23 also apply
to Swing. However, there are also some very important differences, and the process is
discussed in detail in this section.
Painting Fundamentals
Swing's approach to painting is built on the original AWT-based mechanism, but Swing's
implementation offers more finally grained control. Before examining the specifics of
Swing-based painting, it is useful to review the AWT-based mechanism that underlies it.
The AWT class Component defines a method called paint( ) that is used to draw output
directly to the surface of a component. For the most part, paint( ) is not called by your program.
(In fact, only in the most unusual cases should it ever be called by your program.) Rather,
paint( ) is called by the run-time system whenever a component must be rendered. This
situation can occur for several reasons. For example, the window in which the component
is displayed can be overwritten by another window and then uncovered. Or, the window
might be minimized and then restored. The paint( ) method is also called when a program
begins running. When writing AWT-based code, an application will override paint( ) when
it needs to write output directly to the surface of the component.
Because JComponent inherits Component, all Swing's lightweight components inherit
the paint( ) method. However, you will not override it to paint directly to the surface of a
component. The reason is that Swing uses a bit more sophisticated approach to painting that
involves three distinct methods: paintComponent( ), paintBorder( ), and paintChildren( ).
These methods paint the indicated portion of a component and divide the painting process
into its three distinct, logical actions. In a lightweight component, the original AWT method
paint( ) simply executes calls to these methods, in the order just shown.
To paint to the surface of a Swing component, you will create a subclass of the component
and then override its paintComponent( ) method. This is the method that paints the interior
of the component. You will not normally override the other two painting methods. When
overriding paintComponent( ), the first thing you must do is call super.paintComponent( ), so
that the superclass portion of the painting process takes place. (The only time this is
not required is when you are taking complete, manual control over how a component is
displayed.) After that, write the output that you want to display. The paintComponent( )
method is shown here:
protected void paintComponent(Graphics g)
The parameter g is the graphics context to which output is written.
To cause a component to be painted under program control, call repaint( ). It works in
Swing just as it does for the AWT. The repaint( ) method is defined by Component. Calling
it causes the system to call paint( ) as soon as it is possible to do so. Because painting is a
time-consuming operation, this mechanism allows the run-time system to defer painting
momentarily until some higher-priority task has completed, for example. Of course, in
Swing the call to paint( ) results in a call to paintComponent( ). Therefore, to output to the
surface of a component, your program will store the output until paintComponent( ) is
called. Inside the overridden paintComponent( ), you will draw the stored output.
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home