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
Contributor
tunde011
Posts: 17
Registered: ‎04-09-2012
My Device: BB9900
Accepted Solution

drawFocus and Paint

[ Edited ]

Ok, I feel like i have been through every resource possible but still not getting it

 

public class BitmapButtonField  extends BaseButtonField{
	private Bitmap[] _bitmaps;
	private static final int NORMAL = 0;
	private static final int FOCUS = 1;

	public BitmapButtonField( Bitmap normalState )
	{
	this( normalState, normalState, 0 );
	}

	public BitmapButtonField( Bitmap normalState, Bitmap focusState )
	{
	this( normalState, focusState, 0 );
	}

	public BitmapButtonField( Bitmap normalState, Bitmap focusState, long style )
	{
		
	super( Field.FIELD_HCENTER | Field.FIELD_VCENTER | Field.FIELD_BOTTOM |Field.FOCUSABLE );

	if( (normalState.getWidth() != focusState.getWidth())
	|| (normalState.getHeight() != focusState.getHeight()) ){

	throw new IllegalArgumentException( "Image sizes don't match" );
	}

	setPadding(10,10,10,10);
	
	_bitmaps = new Bitmap[] { normalState, focusState };
	
	}

	public void setImage( Bitmap normalState ){
	_bitmaps[NORMAL] = normalState;
	invalidate();
	}

	public void setFocusImage( Bitmap focusState ){
	_bitmaps[FOCUS] = focusState;
	invalidate();
	}

	public int getPreferredWidth() {
		return _bitmaps[NORMAL].getWidth()+10;
	}

	public int getPreferredHeight() {
		return _bitmaps[NORMAL].getHeight()+10;
	}

	protected void layout( int width, int height ) {
	setExtent( _bitmaps[NORMAL].getWidth(), _bitmaps[NORMAL].getHeight() );
	}

	protected void paint( Graphics g ) {
		
		int index = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? FOCUS : NORMAL;
		g.drawBitmap( 0, 0, _bitmaps[index].getWidth(), _bitmaps[index].getHeight(), _bitmaps[index], 0, 0 );
		
		  
	}

	/**
	* With this commented out the default focus will show through
	* If an app doesn't want focus colours then it should override this and do nothing
	**/
	/*
	protected void paintBackground( Graphics g ) {
	// Nothing to do here
	}
	
	*/
	
	protected void drawFocus( Graphics g, boolean on ) {
	// Paint() handles it all
	
		g.setDrawingStyle(Graphics.DRAWSTYLE_FOCUS, true );
		paintBackground(g);
		paint(g);
	}
	
}

 

How do I make the image highlight with a color round it or something. I think i am suppose to ovewrite the paintBackground but with what? when i set g.setBackgroundColor(Color.YELLOWGREEN); in paint, it colors the whole bitmap

Developer
crispyoz
Posts: 223
Registered: ‎10-01-2011
My Device: Bold 9900

Re: drawFocus and Paint

We can't see what is in BaseButtonField so there may be an issue there but all I do is override drawFocus becasue I only want to highlight a small area of my field.

 

 

protected void drawFocus(final Graphics graphics, final boolean on) {
    drawHighlightRegion(graphics, Field.HIGHLIGHT_FOCUS, on, 0, 0, 48, 48);

}

 

Chris

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

Re: drawFocus and Paint

The drawFocus API method description gives a pretty good summary of what you need to do in that method:

 

"Draws the focus indicator for this field.
A field's manager invokes this method after painting the field. The manager initializes the graphics object passed in for drawing with field-local coordinates. It is also assumed that the region is already drawn correctly with the opposing state for the on parameter."

 

So you should' not call paint or paintBackground.  Just do whatever painting you need to do to indicate that it is focused (or not).  As a start you could draw a rectangle around your Field in different colors depending the value of 'on'.

 

Let us know if this is not enough to get you going.

Contributor
tunde011
Posts: 17
Registered: ‎04-09-2012
My Device: BB9900

Re: drawFocus and Paint

OK guys, thank you very much. Got it working, so annoying I have spent over 4 hours on these two methods :Wacko: I followed both of your inputs. I didn't even have totouch the paintBackground or paint hahaha, never felt sooo dumb  before. Anyways for future developers who struggle with the same thing. here is a modification of the drawFocus method.

 

protected void drawFocus( Graphics g, boolean on ) {
	// Paint() handles it all
	
		g.setDrawingStyle(Graphics.DRAWSTYLE_FOCUS, true );
		
		if(on){
			drawHighlightRegion(g, Field.HIGHLIGHT_FOCUS, on, 0, 0, 48, 48);
		}
		
		paint(g);
	}

 

Developer
crispyoz
Posts: 223
Registered: ‎10-01-2011
My Device: Bold 9900

Re: drawFocus and Paint

Hi tunde011,

 

I'm not sure why you are calling paint, nor what you are setting the drawing style. The drawing style already defaults and my reading of the docs is that drawFocus is called after paint so effective you are getting:

 

paint

drawFocus

paint

 

You should only need:

 

if(on){
			drawHighlightRegion(g, Field.HIGHLIGHT_FOCUS, on, 0, 0, 48, 48);
		}