Learn Painters in LWUIT


The Painter interface

Painter defines the elemental interface for all objects that should draw backgrounds or to render on a glass pane. This interface declares just one method-public void paint(Graphics g, Rectangle rect)-for drawing contained in the bounding rectangle (specifed by rect) of a element. The library offers a category that implements Painter and is used as a default background painter for widgets and containers. That is the BackgroundPainter class that has (you guessed it) simply the one methodology paint, which both paints the background picture if one has been assigned or fills within the bounding rectangle of the element with the colour set in its model.

Once we need to paint a background ourselves, we will write our personal class that implements Painter, and set it because the background painter for the related element. The DemoPainter MIDlet, mentioned within the subsequent part, exhibits how that is completed.

The DemoPainter software

This software creates a combo field and makes use of a theme to set the model for the varied parts which are displayed. When the appliance is compiled with out setting a customized background painter, the combo field appears to be like as proven within the following screenshot:

The MIDlet code has the next assertion commented out within the MIDlet. When uncommented, this assertion units an occasion of ComboBgPainter because the background painter for the combo field.

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

The category answerable for drawing the background is ComboBgPainter, which implements Painter. The constructor for this class takes the colour for use for background portray as its solely parameter. The paint methodology determines the coordinates of the top-left nook of the rectangle to be painted and its dimensions. The rectangle is then flled utilizing the colour that was set by the constructor.

class ComboBgPainter implements Painter
{

personal 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 precise follow, there may be hardly any level in utilizing a customized painter simply to color a background shade, as a result of the setBgColor methodology of Fashion will normally do the job. Themes too can be utilized for setting background colours. Nonetheless, painters are very helpful when intricate background patterns must be drawn, and particularly if a number of layers are concerned. PainterChain, described within the subsequent part, is a category designed for dealing with such necessities.

The PainterChain class

It’s potential to make use of multiple painter to render totally different layers of a background. Such a set of painters might be chained collectively by the PainterChain class. The one constructor of this class has the shape public PainterChain(Painter[] chain) the place the parameter chain is an array of painters in houston. The contents of chain will probably be referred to as sequentially throughout the portray of a background, ranging from the factor at index 0 to the final one.

There are two strategies of the PainterChain class that present assist for including painters to the array underlying the chain. A brand new painter might be added both to the highest (the prependPainter methodology) or on the finish (the addPainter methodology) of the array. The array itself might be accessed by the getChain methodology.

PainterChain implements Painter in order that the setBgPainter methodology can be utilized to set a PainterChain in addition to a lone painter, which suggests the paint methodology is also current right here. The perform of paint in PainterChain is to name the paint strategies of the painter array parts one after the other beginning at index 0.

The DemoPainterChain software that comes up subsequent exhibits how a sequence of painters can be utilized to attract the a number of layers of a background.


Leave a Reply

Your email address will not be published. Required fields are marked *