Skip to content

Learn Xperience

Menu
  • ACTIVITY
  • ABOUT US
  • CONTACT US
  • SIGN IN
  • SIGN UP
Menu

Learn Painters in LWUIT

Posted on April 10, 2023

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. When the application is compiled without setting a custom background painter, the combo box looks as shown in the following screenshot:

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 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.. painter near me

Leave a Reply Cancel reply

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


Recent Posts

  • The Thrills of Online Casino Gaming
  • How Online Casinos Are Shaping the Gaming Industry
  • Effective SoundCloud Promotion Strategies to Boost Your Presence
  • Renovation Builders Sydney Craft Homes With Excellence
  • Effiziente Lagerorganisation mit dem richtigen Lagerbedarf

Recent Comments

No comments to show.

Archives

  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • January 2022
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • January 2021
  • June 2019

Categories

  • ACTIVITY
  • game
  • General
  • health
  • How to
  • Iphone
  • Mobile
©2025 Learn Xperience | Design: Newspaperly WordPress Theme