Painting GUI components
In Java, components are rendered on screen in a process known as painting. Although this is usually handled automatically, there are occasions when you need to trigger repainting, or modify the default painting for a component.
The core painting mechanism is based on the Abstract Windowing Toolkit (AWT). In newer versions of Java, Swing painting mechanisms are based on and extend the functionality of AWT mechanisms.
Components can be heavyweight or lightweight. A heavyweight component has its own native screen peer. For a lightweight component to exist there must be a heavyweight further up the containment hierarchy. So lightweight components are more efficient. Swing components tend to be lightweight.
Painting for lightweight and heavyweight components differs slightly.
Painting is triggered when a GUI element is launched or altered in any way. This can be caused by
- a system event
- an application event
- a system event
- System-triggered painting operations are caused when a system requests a component to be rendered onscreen for the first time, resized, or repaired.
If you open the Save As window in an application, then a system request is triggered.
- an application event
- Application-triggered painting events occur when the internal state of an application changes and requires components to be updated.
For example, if an item is selected, the application might send a request for the item to be highlighted.
Painting in AWT
When a paint request is triggered, AWT uses a callback mechanism to paint the lightweight and heavyweight components of a GUI.
You should place the code for rendering the component in the relevant overriding paint
method of the java.awt.Component
class. The method is invoked whenever a system or application request is triggered.
public void paint(Graphics g)
The paint
method is not invoked directly. Instead, you call the repaint
method to schedule a call to paint
correctly.
The Graphics
object parameter is pre-configured with the state required to draw and render a component. The Graphic
parameter can be reconfigured to customize painting.
public void repaint()
public void repaint(long tm)
public void repaint(int x, int y, int width, int height)
public void repaint(long tm, int x, int y,
int width, int height)
For complex components, you should specify the region to be rendered using the arguments of the repaint
method. The updated region is referred to as the clip rectangle.
The whole component is repainted if no arguments are given.
public void repaint()
public void repaint(long tm)
public void repaint(int x, int y, int width, int height)
public void repaint(long tm, int x, int y,
int width, int height)
You can use an overridden update
method to handle application-triggered painting differently from system-triggered painting.
For application-triggered paint operations, AWT calls the update
method. If the component doesn't override the update()
method, the default update
method paints heavyweight components by clearing the background and calling the paint
method.
The process of updating specified areas of a component is known as incremental painting. Incremental painting is not supported by lightweight components.
Consider the code that is used to refresh the applet when a user types text. You first create the ke
instance to represent the event of typing a key.
public void keyTyped(keyEvent ke) {
msg += ke.getKeyChar();
repaint();
}
You call the ke
instance's getKeyChar
method to determine the value of the typed key. You assign the value to a variable - msg
in this case - using the +=
overloaded operator.
You then call the repaint
method, which in turn calls the overridden paint
method. The value of the key typed - msg
- is passed to the paint
method and the key character is painted onscreen.
Painting in Swing
Painting Swing components is based on the AWT callback method, so it supports the paint
and repaint
methods. Swing painting also extends the functionality of paint operations with a number of additional features.
The Swing API, RepaintManager
, can be used to customize the painting of Swing components. In addition, Swing painting supports Swing structures, such as borders and double-buffering.
Double-buffering is supported by default. Removing double-buffering is not recommended.
Because lightweight components are contained within heavyweight components, the painting of lightweight components is triggered by the paint
method of the heavyweight component.
When the paint
method is called, it is translated to all lightweight components using the java.awt.Container
class's paint
method. This causes all defined areas to be repainted.
There are three customized callbacks for Swing components, which factor out a single paint
method into three subparts. These are
paintComponent()
paintBorder()
paintChildren()
paintComponent()
- You use the
paintComponent
method to call the UI delegate
object's paint
method. The paintComponent
method passes a copy of the Graphics
object to the UI delegate
object's paint
method. This protects the rest of the paint
code from irrevocable changes.
You cannot call the paintComponent
method if UI delegate
is set to null
.
paintBorder()
- You use the
paintBorder
method to paint a component's border.
paintChildren()
- You use the
paintChildren
method to paint a component's child components.
You need to bear several factors in mind when designing a Swing paint operation.
Firstly, application-triggered and system-triggered requests call the paint
or repaint
method of a Swing component, but never the update
method. Also, the paint
method is never called directly. You can trigger a future call to it by invoking the repaint
method.
As with AWT components, it is good practice to define the clip rectangle using the arguments of the repaint
method. Using the clip rectangle to narrow down the area to be painted makes the code more efficient.
You can customize the painting of Swing components using two properties, namely
opaque
optimizedDrawingEnabled
opaque
- You use the
opaque
property to clear the background of a component and repaint everything contained within the paintComponent
method. To do this, opaque
must be set to true
, which is the default. Setting opaque
to true
reduces the amount of screen clutter caused by repainting a component's elements.
optimizedDrawingEnabled
- The
optimizedDrawingEnabled
property controls whether components can overlap. The default value of the optimizedDrawingEnabled
property is true
.
Setting either the opaque
or the optimizedDrawingEnabled
property to false
is not advised unless absolutely necessary, as it causes a large amount of processing.
You use the paintComponent
method for Swing component extensions that implement their own paint code. The scope of these component extensions need to be set in the paintComponent
method.
The paintComponent
method is structured in the same way as the paint
method.
// Custom JPanel with overridden paintComponent method
class MyPanel extends JPanel {
public MyPanel(LayoutManager layout) {
super (layout) ;
}
public void paintComponent(Graphics g) {
// Start by clearing the current screen
g.clearRect( getX(), getY(), getWidth(), getHeight()) ;
g.setColor (Color.red) ;
int[] x = {30, 127, 56, 355, 240, 315 } ;
int[] y = {253, 15, 35, 347, 290, 265} ;
//Draw complex shape on-screen and fill it
g.drawPolygon (x, y, 5) ;
g.fillPolygon (x, y, 5) ;
}
}
Summary
Painting is the process of rendering components onscreen. Heavyweight components have matching native peers, whereas lightweight components rely on a heavyweight container to do their painting. Painting is triggered by requests. System-triggered requests occur when windows are launched, resized, or repaired. Application-triggered requests occur when the internal state of an application changes.
AWT uses a callback method to invoke an overridden version of the paint
method of the java.awt.Component
class. You define the area that requires painting, the clip rectangle, using the arguments of the repaint
method. You can also override the update
method for application-triggered paint operations.
Swing painting is based on AWT painting and then adds further functionality. Heavyweight containers translate the paint
method to all the lightweight components contained within them. Swing painting has three customized callbacks and two properties that you use to customize the Swing painting operation.