Your browser does not support JavaScript
Exit Print View

Lightweight UI Toolkit Developer’s Guide

Get PDF Book Print View

Document Information


1.  Introducing the Lightweight UI Toolkit Library

2.  Using Lightweight UI Toolkit Widgets

3.  Using Lists

4.  Table and Tree

5.  Using Dialogs

6.  Using Layout Managers

7.  Painters

7.1 Using Painter

7.2 Painter Chain

7.3 Glass Pane

8.  Using the Style Object

9.  LWUIT Implementation

10.  HTMLComponent

11.  Theming

12.  Resources

13.  Using Transitions and Animations

14.  M3G

15.  Logging

16.  Authoring Components

17.  Portability and Performance



Chapter 7


Painter is an interface that can be used to draw on a component background. The Painter draws itself and then the component draws itself on top within the restrictions of the component bounds. One of the biggest advantages of using a painter is that you can write arbitrary code to draw the component background. An example of such code might be a gradient background for a component, or tiling (using an image to tile the component background). Using a generic painter allows you to reuse background painters for various components.

Note - To view the painter drawing, a component must have some level of transparency.

To clarify these points, assume you want to make a painter that draws a diagonal line in the background of a component. This kind of painting is vectoring since you are specifying the absolute coordinates and dimensions of a component. You can reuse the painter for other components.

7.1 Using Painter

The Painter code might look like the following example:

Painter diagonalPainter = new Painter() {
   public void paint(Graphics g, Rectangle rect) {
                 rect.getX() + rect.getSize().getWidth(),
                 rect.getY() + rect.getSize().getHeight());

To use the diagonalPainter you created, use it as the component background painter:


Let's create a Label, Button and a RadioButton and set their background painter with the above diagonalPainter.

Label myLabel  = new Label(Image.createImage("/images/duke.png"));
Button myButton  = new Button("Image and Text Button");
RadioButton myRadioButton = new RadioButton("RadioButton");

The three components are shown in link iconLabel, Button, and RadioButton With diagonalPainter in Background.

Label, Button, and RadioButton With diagonalPainter in Background
Label, Button, and RadioButton With diagonalPainter in Background

As a result, you see a diagonal line that is painted in the components’ background (behind the Duke images and text).

7.2 Painter Chain

Sometimes a single painter is not enough to represent complex drawing logic necessary for an application's needs. The painter chain allows you to bind together several painters and present them as one. This can be used to separate responsibilities. For example, one painter can draw a background image while another painter can highlight validation errors.

To create a painter chain just use:

PainterChain chain = new PainterChain(new Painter[]{painter1, painter2});

The painter chain is very useful with the glass pane.

7.3 Glass Pane

The glass pane is a painter that is drawn on top of the form. The form cannot paint over the glass panel! This allows creating very unique visual effects for an application and allows a developer to implement functionality such as validation errors, or special effects such as fading tooltips.A glass pane can be installed using a painter chain to prevent a new glasspane from overriding the already installed glass pane.

To install a glass pane invoke:

Painter glassPane = ...;

Use this code to   install a glass pane without overriding an existing glass pane (this method works correctly even if a glass pane is not installed):

Painter glassPane = ...;
PainterChain.installGlassPane(myForm, glassPane);