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
Developer
MFSI
Posts: 80
Registered: ‎09-03-2012
My Device: 9810
Accepted Solution

Drawing RoundRect shape of a customLabel does not provide roundRect shape

Hi

I am creating a Custom Label field that has been created successfully.

 

Now after that In order to have a round shape for that label in paint(Graphics graphics)   I am drawing a roundRect and then further label content( an image and and a textString) over it.

 

Yes I can also have label shape round using setBorder(RoundBorder) method (I tried it and thats working ) but I was trying it as follows .

 

public class CustomField extends Field {

	private String mLabelText = null;
	private int mForegroundColor = 0;
	private int mBackgroundColor = 0;
	private Bitmap mBitmap = null;

	public CustomField(long style, String iLabelText, int iForegroundColor,
			int iBackgroundColor) {
		super(style);
		mLabelText = iLabelText;
		mForegroundColor = iForegroundColor;
		mBackgroundColor = iBackgroundColor;
	}

	public CustomField(Bitmap iBitmap, long style, String iLabelText,
			int iForegroundColor, int iBackgroundColor) {
		this(style, iLabelText, iForegroundColor, iBackgroundColor);
		mBitmap = iBitmap;
	}

	protected void layout(int width, int height) {
		
		if ((getStyle() & Field.USE_ALL_WIDTH) == Field.USE_ALL_WIDTH) {
			setExtent(width, Math.min(height, getPreferredHeight()));
		} else {
			setExtent(getPreferredWidth(),
					Math.min(height, getPreferredHeight()));
		}
	}

	protected void paint(Graphics graphics) {

		graphics.drawRoundRect(0, 0, getWidth(), getHeight(),
				20	, 20);

graphics.setBackgroundColor(mBackgroundColor); graphics.clear(); graphics.setFont(Font.getDefault().derive(Font.SANS_SERIF_STYLE, 25, Ui.UNITS_px, Font.ANTIALIAS_NONE, Font.COLORED_OUTLINE_EFFECT)); graphics.setColor(mForegroundColor); if (mBitmap != null) { int textHeight = (getHeight() - getFont().getHeight()) / 2; int imageHeight = (getHeight() - mBitmap.getHeight()) / 2; graphics.drawBitmap(0, imageHeight, mBitmap.getWidth(), mBitmap.getHeight(), mBitmap, 0, 0); graphics.drawText(mLabelText, mBitmap.getWidth(), textHeight, DrawStyle.ELLIPSIS, getWidth() - mBitmap.getWidth()); } else { graphics.drawText(mLabelText, 0, 0, DrawStyle.ELLIPSIS, getWidth()); } } public int getPreferredHeight() { if (mBitmap != null) { return Math.max(mBitmap.getHeight(), getFont().getHeight()); } else { return getFont().getHeight(); } } public int getPreferredWidth() { int width = getFont().getAdvance(mLabelText); if (mBitmap != null) { width += mBitmap.getWidth(); } return width; } }

 

For creating label I am using second constructor(one with a Bitmap content);

 


Can any body Please tell where am I making mistake ? I did tried a range of values for arcWidth and arcHeight params of drawRoundRect() method but no round rectangle (plain rectangular  shape ).

 

 

Thanks

Always Success
Developer
MFSI
Posts: 80
Registered: ‎09-03-2012
My Device: 9810

Re: Drawing RoundRect shape of a customLabel does not provide roundRect shape

OK I got it , problem was in paint() method

 

I was drawing Rect before calling clear() .  Now i got the Label's rounded shape.

 

can we set backgroundcolor to drawn RoundRect instead of field(graphics object) ?

Always Success
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Drawing RoundRect shape of a customLabel does not provide roundRect shape

[ Edited ]

Great, can you mark this as solved then - try Help at the top is you don't know how to do this.

 

And no, the background color is an attribute of the graphics Object.  You can fillRect with the background color you want before you do a drawRoundRect if you want.

 

BTW, in my experience drawRoundRect is not very pretty.  You get better curves if you use an image, which has various alpha values (transparency) which gives a better impression of a curve. 

Developer
MFSI
Posts: 80
Registered: ‎09-03-2012
My Device: 9810

Re: Drawing RoundRect shape of a customLabel does not provide roundRect shape

Thanks peter for the reply

 

Yes I did it by mistake. It happened because I thought of creating rounded shape for the Customlabel at last.

 

Ok , I tried fillRoundRect instead of drawRoundRect. Yes its now as I needed. Thanks

 

Ok I will also try to use image

Always Success