Learn Painters in LWUIT
Tutorials Shared by the Internet Community
Total Hits - 33386
Total Votes - 98 votes
Vote Up - 42 votes
Vote Down - 56 votes
Domain - www.packtpub.com
Category - JAVA/JAVA Misc
Submitted By - Veena
Submitted on - 2009-09-16 03:31:48
The Painter interface
Painter defines 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.
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.
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)
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.
For more information, please visit http://www.packtpub.com/lwuit-1-1-for-java-me-developers/book More detail...