The painting contractors interface
Painter defines the essential interface for all objects which are intended to attract backgrounds or to render on a glass pane. This interface proclaims best one approach-public void paint(Graphics g, Rectangle rect)-for drawing in the bounding rectangle (specifed through rect) of a element. The library presents a category that implements Painter and is used as a default heritage painter for widgets and packing containers. This is the BackgroundPainter class that has (you guessed it) just the one technique paint, which either paints the heritage picture if one has been assigned or fills in the bounding rectangle of the component with the color set in its fashion.
When we need to paint a historical past ourselves, we are able to write our own magnificence that implements Painter, and set it as the history painter for the relevant component. The DemoPainter MIDlet, mentioned in the next phase, indicates how that is executed.
The DemoPainter utility
This utility creates a mixture box and makes use of a theme to set the style for the various factors that are displayed. When the software is compiled without setting a custom history painter, the mix field looks as shown inside the following screenshot:
The MIDlet code has the following declaration commented out in the MIDlet. When uncommented, this declaration units an instance of ComboBgPainter as the heritage painter for the combination box.
The class accountable for drawing the background is ComboBgPainter, which implements Painter. The constructor for this magnificence takes the shade for use for background painting as its best parameter. The paint approach determines the coordinates of the top-left nook of the rectangle to be painted and its dimensions. The rectangle is then flled using the colour that changed into set thru the constructor.
Class ComboBgPainter implements Painter
non-public 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 exercise, there is infrequently any factor in using a custom painter just to color a heritage shade, due to the fact the setBgColor method of Style will usually do the job. Themes too may be used for placing history colors. However, painters are very beneficial whilst complex historical past styles need to be drawn, and specifically if multiple layers are concerned. PainterChain, defined within the next segment, is a category designed for managing such necessities.
The PainterChain magnificence
It is feasible to apply a couple of painter to render exceptional layers of a background. Such a set of painters may be chained together through the PainterChain class. The simplest constructor of this elegance has the shape public PainterChain(Painter chain) wherein the parameter chain is an array of painters. The contents of chain might be known as sequentially throughout the painting of a historical past, beginning from the detail at index 0 to the last one.
There are techniques of the PainterChain magnificence that offer support for including painters to the array underlying the chain. A new painter may be introduced either to the pinnacle (the prependPainter approach) or at the cease (the addPainter approach) of the array. The array itself can be accessed through the getChain technique.
PainterChain implements Painter in order that the setBgPainter technique can be used to set a PainterChain in addition to a lone painter, which means the paint approach also is present right here. The feature of paint in PainterChain is to call the paint strategies of the painter array factors one at a time starting at index 0.