Discover Painters in LWUIT

The Painter interface

Painter defines the basic interface for all objects that should attract backgrounds or to render over a glass pane. This interface declares only one method-public void paint(Graphics g, Rectangle rect)-for drawing within the bounding rectangle (specifed by rect) of a element. The library presents a category that implements Painter and it is employed as a default track record painter for widgets and containers. This can be the BackgroundPainter course which has (you guessed it) just the 1 method paint, which both paints the qualifications picture if one has been assigned or fills from the bounding rectangle with the element with the color established in its type visit us.

Whenever we want to paint a track record ourselves, we are able to write our own course that implements Painter, and established it as being the track record painter to the applicable ingredient. The DemoPainter MIDlet, mentioned within the up coming segment, reveals how this is carried out.

The DemoPainter application

This software generates a combo box and makes use of a concept to set the design and style to the various features which are displayed. When the software is compiled with no environment a tailor made qualifications painter, the combo box appears as demonstrated inside the next screenshot:

The MIDlet code has the following assertion commented out from the MIDlet. When uncommented, this statement sets an occasion of ComboBgPainter as being the qualifications painter for your combo box.

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

The class liable for drawing the qualifications is ComboBgPainter, which implements Painter. The constructor for this course can take the colour for use for qualifications portray as its only parameter. The paint system decides the coordinates of the top-left corner in the rectangle to generally be painted and its proportions. The rectangle is then flled applying the colour which was set by way of the constructor.

Drawing a multi-layered history

In genuine practice, there is certainly barely any level in using a custom painter in order to paint a history shade, because the setBgColor system of favor will usually do the task. Themes as well can be employed for location history hues. Nevertheless, painters are incredibly handy when intricate background patterns should be drawn, and particularly if numerous layers are associated. PainterChain, explained within the upcoming section, is often a class created for managing these types of necessities.

The PainterChain course

It can be feasible to utilize a lot more than one particular painter to render various levels of a background. This kind of established of painters could be chained together as a result of the PainterChain course. The only constructor of the class has the form community PainterChain(Painter[] chain) wherever the parameter chain is surely an variety of painters. The contents of chain will likely be called sequentially during the painting of a background, beginning in the aspect at index 0 to the final 1.

You’ll find two ways of the PainterChain course that give assistance for incorporating painters on the array fundamental the chain. A fresh painter is often added either to the prime (the prependPainter approach) or on the end (the addPainter strategy) from the array. The array itself is usually accessed via the getChain technique.