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
khalidhabib
Posts: 16
Registered: ‎01-22-2013
My Device: Blackberry9800

paint method slow down typing speed

Hi,I have 2 paint methods..Every thing is working fine,but the problem is that paint method slows down the typing speed in text fields,some one told me to put SetBackground ans  SetBorder outside the paint method,it gone well in another program,but in this program there are two paint methods,one for Label and other one for ImageButton..I am getting confused...Please anyone help me out there...

---------------------------------------------------LockedHeaderHorizontalManager----------------------------------------------

 

public LockedHeaderHorizontalManager(String label)
{
super(Manager.HORIZONTAL_SCROLL | Manager.HORIZONTAL_SCROLLBAR | USE_ALL_WIDTH | USE_ALL_HEIGHT);


ButtonField bf = new ButtonField(ButtonField.CONSUME_CLICK)
{          //------Start Of Button Field
boolean _inFocus = false;

protected void layout(int width, int height)
{
super.layout(80,50);
super.setExtent(80,50);
}

public void onFocus(int direction)
{
_inFocus = true;
this.invalidate();
}

public void onUnfocus()
{
_inFocus = false;
this.invalidate();
}

protected void paint(Graphics graphics)
{
graphics.setGlobalAlpha(0);
setBackground(BackgroundFactory.createSolidTransparentBackground(0, 0));
graphics.setGlobalAlpha(255);
super.setBorder(BorderFactory.createSimpleBorder(new XYEdges(0,0,0,0))); 

if(_inFocus)
{ graphics.drawBitmap(15,0,getWidth(),getHeight(),Bitmap.getBitmapResource("homeon.png"),0,0);

}
else

graphics.drawBitmap(15,0,getWidth(),getHeight(),Bitmap.getBitmapResource("homeoff.png"),0,0);
}
}


protected void drawFocus(Graphics g, boolean on)
{
}

protected boolean navigationClick(int status, int time)
{
fieldChangeNotify(0);

return false;

}

};

bf.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field,int context)
{
// ((ButtonField)field).setEnabled(false);
}
});


add(bf);

LabelField lf = new LabelField(label, DrawStyle.HCENTER | USE_ALL_WIDTH)
{             //------Start Of Label Field
protected void layout(int width, int height)
{
super.layout(Display.getWidth()-160, height);
setExtent(Display.getWidth()-160, height);
}

protected void paint(Graphics g)
{

synchronized (UiApplication.getEventLock()){
g.setGlobalAlpha(0);
setBackground(BackgroundFactory.createSolidTransparentBackground(0, 0));
g.setColor(0xA7A7A7);
g.setGlobalAlpha(255);

}
super.paint(g);
}
};

lf.setBorder(BorderFactory.createSimpleBorder(new XYEdges(0,1,0,1), new XYEdges(0,0xA7A7A7,0,0xA7A7A7), Border.STYLE_SOLID));
lf.setFont(Font.getDefault().derive(Font.PLAIN, Font.getDefault().getHeight()-2));
if(lf.getFont().getAdvance(label)>Display.getWidth()-160)
{
lf.setPadding((50-lf.getPreferredHeight()*2)/2,0,0,0);
}
else
{
lf.setPadding((50-lf.getPreferredHeight())/2,0,0,0);
}

add(lf);
}             //-----End Of Label Field

protected void sublayout(int maxWidth, int maxHeight)
{
super.sublayout( Display.getWidth(), 50);
setExtent( Display.getWidth(), 50);
}

public void paintBackground(Graphics graphics)
{
graphics.setBackgroundColor(0x353535);
graphics.clear();
graphics.drawBitmap(Display.getWidth()-Bitmap.getBitmapResource("headerlogo.png").getWidth(), 0, Bitmap.getBitmapResource("headerlogo.png").getWidth(),Bitmap.getBitmapResource("headerlogo.png").getHeight(),Bitmap.getBitmapResource("headerlogo.png"),0,0);
super.paintBackground(graphics);
}
}

 

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

Re: paint method slow down typing speed

Only looked very briefly, but the basic problem here is that you appear to be trying to use paint for things it is not supposed to be used for. 

 

All paint should do is paint.  Period.  It should not try to set any of the attributes of the Field being painted.  So don't set the Background of the Field.  Setting things like the Font and the Background, just cause the Field to repaint itself.  So you are almost certainly slowing down because you are repeatedly painting. 

 

So move your setBackgrounds out of the paint().  Have a look at the setBackground method - note that you can have different backgrounds depending on the visual state.  I think using that you can achieve what you want without doing it in paint. 

 

In addition paint is called on the Event Thread so there is no need to request the Event Lock.

 

Your various setting of the globalAlpha seem odd.  Think about what you are trying to do.

 

Finally I would always make sure that your routines have no side effects.  So if you set the background color, or set the foreground color or set the global alpha, then set it back.  You might find this a useful template:

 

protected paint()Graphics g) {

int currentColor = g.getColor();

try {

g.setColor(...);

// other processing

} finally {

g.setColor(currentColor);

}

}

 

 

So I would think carefully