08-11-2010 09:53 AM
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?
Solved! Go to Solution.
08-11-2010 10:03 AM
try paintbackground.
however, the buttonfield is a complicated construct, maybe you will be better off creating your own field.
08-11-2010 10:05 AM
Agree with Simon.
We had better luck creating a custom button by extending BitmapField, then placing the text (if any) over the button image.
08-11-2010 10:47 AM
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(),bgO n,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.