Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
Posts: 133
Registered: ‎12-31-2009
My Device: Not Specified

How to draw rectangle and fill that rectangle for LabelField

[ Edited ]

 

 

Hello Friends,

 

I am trying to draw a LabelField with the rectangular border with one color and fill the rectangle with other color (let say I want Black color for border of rectanlge to be filled with Gray color. I am able to paint the Label Field with following code. I am also able to draw the background color to text but It is applicable for only the text background but if there is empty space after LabelField, then this empty space is not filled with color. How can I solve this problem? MY Code is as below . I tried with "setBackgroundColor" method from "Graphics" class.

 

 

import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.LabelField;

public class BBLabelField extends LabelField {
	private String text;
	private int colorOfText;

	public BBLabelField(String text, int colorOfText) {
		super(text);
		this.text = text;
		this.colorOfText = colorOfText;

	}

	protected void paintBackground(Graphics graphics) {
		graphics.setColor(colorOfText);
		graphics.clear();
		super.paintBackground(graphics);
	}

}

 

Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: How to draw rectangle and fill that rectangle for LabelField

In general, paintBackground is for painting the background (obvious, isn't it?) while paint is for painting content. It is therefore not a good design to use graphics.setColor in paintBackground. Many people here just paint everything in paint, leaving paintBackground untouched. Personally, I prefer splitting the tasks, so I'll give you the example of doing just that:

protected void paintBackground(Graphics g) {
  int prevColor = g.getColor();
  g.setColor(Color.GRAY);
  g.fillRect(0, 0, getWidth(), getHeight());
  g.setColor(Color.BLACK);
  g.drawRect(0, 0, getWidth(), getHeight());
  g.setColor(prevColor);
}

protected void paint(Graphics g) {
  int prevColor = g.getColor();
  g.setColor(colorOfText);
  super.paint(g);
  g.setColor(prevColor);
}

 You can combine paintBackground then paint code in paint and leave paintBackground alone.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!