6 min read

The Painter interface

Painter defnes the fundamental interface for all objects that are meant to draw backgrounds or to render on a glass pane. This interface declares  only one method—public void paint(Graphics g, Rectangle rect)—for drawing inside the bounding rectangle (specifed by rect) of a component. The library provides a class that implements Painter and is used as a default background painter for widgets and containers. This is the BackgroundPainter class that has (you guessed it) just the one method paint, which either paints the background image if one has been assigned or fills in the bounding rectangle of the component with the color set in its style.

When we want to paint a background ourselves, we can write our own class that implements Painter, and set it as the background painter for the relevant component. The DemoPainter MIDlet, discussed in the next section, shows how this is done.

The DemoPainter application

This application creates a combo box and uses a theme to set the style for the various elements that are displayed. When the application is compiled without setting a custom background painter, the combo box looks as shown in the following screenshot:

LWUIT-combo box

The MIDlet code has the following statement commented out in the MIDlet. When uncommented, this statement sets an instance of ComboBgPainter as the background painter for the combo box.

combobox.getStyle().setBgPainter(new ComboBgPainter(0x4b338c));

The recompiled application produces the following display showing the new background color:

LWUIT-background color

The class responsible for drawing the background is ComboBgPainter, which implements Painter. The constructor for this class takes the color to be used for background painting as its only parameter. The paint method determines the coordinates of the top-left corner of the rectangle to be painted and its dimensions. The rectangle is then flled using the color that was set through the constructor.

class ComboBgPainter implements Painter
{
private int bgcolor;
public ComboBgPainter(int bgcolor)
{
this.bgcolor = bgcolor;
}
public void paint(Graphics g, Rectangle rect)
{
g.setColor(bgcolor);
int x = rect.getX();
int y = rect.getY();
int wd = rect.getSize().getWidth();
int ht = rect.getSize().getHeight();
g.fillRect(x, y, wd, ht);
}
}

Drawing a multi-layered background

In actual practice, there is hardly any point in using a custom painter just to paint a background color, because the setBgColor method of Style will usually do the job. Themes too can be used for setting background colors. However, painters are very useful when intricate background patterns need to be drawn, and especially if multiple layers are involved. PainterChain, described in the next section, is a class designed for handling such requirements.

The PainterChain class

It is possible to use more than one painter to render different layers of a background. Such a set of painters can be chained together through the PainterChain class. The only constructor of this class has the form public PainterChain(Painter[] chain) where the parameter chain is an array of painters. The contents of chain will be called sequentially during the painting of a background, starting from the element at index 0 to the last one.

There are two methods of the PainterChain class that provide support for adding painters to the array underlying the chain. A new painter can be added either to the top (the prependPainter method) or at the end (the addPainter method) of the array. The array itself can be accessed through the getChain method.

PainterChain implements Painter so that the setBgPainter method can be used to set a PainterChain as well as a lone painter, which means the paint method also is present here. The function of paint in PainterChain is to call the paint methods of the painter array elements one by one starting at index 0.

The DemoPainterChain application that comes up next shows how a chain of painters can be used to draw the multiple layers of a background.

The DemoPainterChain application

The DemoPainterChain example uses alphaList to show a painter chain in action. After organizing the form and the list, we set up a painter array to hold the three painters that we shall deploy.

Painter[] bgPainters = new Painter[3];

Once we have the array, we create three painters and load them into the array. The frst (lowest) painter, which will fll the bounding rectangle for the list with a designated color, goes in at index 0. The next (middle) layer, at index 1, will draw an image at the center of the list. Finally, the topmost layer for writing a text a little below the center line of the list is inserted at index 2.


bgPainters[0] = new Eraser(0x334026);
try
{
bgPainters[1] = new ImagePainter(Image.createImage(
"/a.png"));
}
catch(java.io.IOException ioe)
{
}
bgPainters[2] = new TextPainter("This is third layer");

Now we are ready to instantiate a PainterChain object, and install it as a background painter for the list.

PainterChain bgChain = new PainterChain(bgPainters);
alphaList.getStyle().setBgPainter(bgChain);

The list itself will be drawn on top of these three layers, and the background layers will be visible only because the list is translucent as determined by the transparencyvalue 100, set by the AlphaListRenderer instance used to render alphaList. The list now looks as shown in the following screenshot:

LWUIT-list

A close inspection of the screenshot that we have just seen will show that the layers have indeed been drawn in the same sequence as we had intended. The three painters are very similar in structure to the ComboBgPainter class we came across in the previous example. The Eraser class here is virtually identical to ComboBgPainter. The other two classes work in the same way, except for the fact that TextPainter draws a line of text, while ImagePainter draws an image.

class TextPainter implements Painter
{
private String text;
TextPainter(String text)
{
//set the text to be written
this.text = text;
}
public void paint(Graphics g, Rectangle rect)
{
//get the dimension
//of background
int wd = rect.getSize().getWidth();
int ht = rect.getSize().getHeight();
//create and set font for text
Font textFont = Font.createSystemFont(
Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE);
g.setFont(textFont);
//set text color
g.setColor(0x0000aa);
//position text slightly below centerline
int textX = wd/2 - textFont.stringWidth(text)/2;
int textY = ht/2 - textFont.getHeight()/2 + 3;
//write text
g.drawString(text, textX, textY);
}
}

class ImagePainter implements Painter
{
private Image bImage;
ImagePainter(Image bImage)
{
//set the image to be drawn
this.bImage = bImage;
}
public void paint(Graphics g, Rectangle rect)
{
//get the dimensions
//of background
int wd = rect.getSize().getWidth();
int ht = rect.getSize().getHeight();
//position image at center
int imageX = wd/2 - bImage.getWidth()/2;
int imageY = ht/2 - bImage.getHeight()/2;
//draw image
g.drawImage(bImage, imageX, imageY);
}
}

When an image is used on the background of a form, we have seen that it is scaled to occupy the entire form real estate. But if the same image is used as an icon for a label, then it is drawn in its actual size. This task of scaling the image for backgrounds is taken care of by BackgroundPainter, which is used as the default bgPainter.

LEAVE A REPLY

Please enter your comment!
Please enter your name here