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
Regular Contributor
Posts: 57
Registered: ‎07-27-2010
My Device: Bold 9700, 9000 and Storm 9520, 9500
My Carrier: SingTel
Accepted Solution

ButtonField with background

Hi guys,

 

I have a custom ButtonField with a background image. Here is a snippet of my layout code

 

                    myWidth =(int)((double)bgOff.getWidth());
                    myHeight =  (int)((double)bgOff.getHeight()) ;

                   setExtent(myWidth, myHeight);

 

This will set the ButtonField's dimensions to fit that of the background image.

 

In my paint method, i use drawBitmap to draw out the background image. Here is my code.

 

  graphics.drawBitmap(0, 0, bgOn.getWidth(), bgOn.getHeight(),bgOn, 0, 0);

 

 

However this results in the background image being drawn only. I would like to have my main bitmap image on top of this background image. Centralized, if possible. Is it possible to be done this way?

 

 

Developer
Posts: 16,984
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: ButtonField with background

try paintbackground.

however, the buttonfield is a complicated construct, maybe you will be better off creating your own field.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: ButtonField with background

Agree with Simon.

 

We had better luck creating a custom button by extending BitmapField, then placing the text (if any) over the button image.

 

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

Re: ButtonField with background

You probably forgot to put super.paint(graphics) in your code somewhere - paint does not magically call the paint of the superclass (which is what draws that button depending on focus etc.)

 

A cleaner solution would be

 

protected void paintBackground(Graphics graphics) {
    graphics.drawBitmap(0,0,getWidth(),getHeight(),bgOn,0,0);
}

 This call is poorly documented but it works.  Just look for paintBackground under Screen and you will suddenly discover that it overrides an otherwise undocumented paintBackground call in Field.

 

As for centralized - you will need some tricks for that.  One might be able to achieve this with Graphics.pushContext().  Another approach would be like this:

 

public class CenteringManager extends Manager {
  private Bitmap _bgOn;
  private int _bgWidth;
  private int _bgHeight;
  private int _width;
  private int _height;

  public CenteringManager(Bitmap bg) {
    _bgOn = bg;
    if (_bgOn != null) {
      _bgWidth = _bgOn.getWidth();
      _bgHeight = bgOn.getHeight();
    }
  }

  public int getPreferredWidth() {
    return _bgWidth;
  }

  public int getPreferredHeight() {
    return _bgHeight;
  }

  protected void sublayout(int w, int h) {
    _width = w;
    _height = h;
    if (_bgOn != null) {
      _width = Math.min(_width, _bgWidth);
      _height = Math.min(_height, _bgHeight);
    }
    if (getFieldCount() > 1) {
      throw new IllegalStateException("Cannot center more than one field.");
    }
    if (getFieldCount() > 0) { // there is one!
      Field onlyChild = getField(0);
      int childX;
      int childY;
      layoutChild(onlyChild, _width, _height);
      childX = (_width - onlyChild.getWidth()) / 2;
      if (childX < 0) {
        childX = 0;
      }
      childY = (_height - onlyChild.getHeight()) / 2;
      if (childY < 0) {
        childY = 0;
      }
      setPositionChild(onlyChild, childX, childY);
    }
    setExtent(_width, _height);
  }

  protected void paintBackground(Graphics g) {
    if (_bgOn != null) {
      g.drawBitmap(0,0,_width,_height,_bgOn,0,0);
    } else {
      g.clear();
    }
  }
}
    

 You add your ButtonField to an instance of CenteringManager (created with the background image you want) which you then add to the Manager/Screen to which you would normally add that ButtonField.

 

 

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