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
Posts: 143
Registered: ‎07-14-2008
My Device: Not Specified
Accepted Solution

How to - Using paintBackground()

If I have this sort of thing:

 

 

protected void paint(Graphics g) {
g.drawBitmap( 0, 0, bgBitmap.getWidth( ), bgBitmap.getHeight( ), bgBitmap, 0, 0 );
super.paint( g );

// Paint other foreground stuff here
}

 

 how would I correctly use the paintBackground() method instead of painting the bgBitmap (in sample code above) in the paint() method?  I tried using it and was not able to see the bgBitmap.

 

 

Developer
Posts: 68
Registered: ‎07-21-2008
My Device: Not Specified

Re: How to - Using paintBackground()

 
Developer
Posts: 143
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to - Using paintBackground()

Ah, the Zen solution, words can just not describe it.  Smiley Wink

 

Developer
Posts: 143
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to - Using paintBackground()

Never mind.  It does work exactly like I thought, I just did something wrong (I think) the first time I tried it.

 

Here is the working code in case anyone else has a problem:

 

protected void paintBackground( Graphics g ) { super.paintBackground( g ); g.drawBitmap( 0, 0, bgBitmap.getWidth( ), bgBitmap.getHeight( ), bgBitmap, 0, 0 ); } protected void paint(Graphics g) { super.paint( g ); }

 

 I think when I first tried it, I forgot to call super.paintBackground( ).

 

 

Developer
Posts: 146
Registered: ‎11-05-2008
My Device: Not Specified

Re: How to - Using paintBackground()

To draw a bitmap use

protected void paintBackground( Graphics g ) {
  g.drawBitmap( 0, 0, bgBitmap.getWidth( ), bgBitmap.getHeight( ), bgBitmap, 0, 0 );
             super.paint( g );
}
And if you use it to draw a whole screen instead of to draw background of a field component,use FullScreen instead of MainScreen.

 

Developer
Posts: 143
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to - Using paintBackground()

Calling super.paint() from paintBackground() is a bad idea.  You either want to call g.clear(), or superPaintBackground(), and the framework will call paint() as appropriate.

 

Developer
Posts: 146
Registered: ‎11-05-2008
My Device: Not Specified

Re: How to - Using paintBackground()

Actually in this way this is working properly at my end .And Can u please tell me what's the good idea?
Developer
Posts: 143
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to - Using paintBackground()

In any derived class, in an overrieded method such as paintBackgroun(), you use super.method() to call the overrided method in the extended class to allow you to use the overrided method to extend the behavior of the method (vs. replace it totally).

 

In this case, the paintBackground() method in the base class looks like this:

 

 

protected void paintBackground( Graphics g ) { g.clear( ); }

So, in our derived class, if we override this method and DO not call the base class method with super, then our background will NOT be cleared when it is repainted.

 

paint() is another callback method (ie one that the framework calls, not us in general), and so you do not call it, the framework does.  You just tell the framework what (if anything) you wish to paint when it wants to paint.

 

In other words, the framework does this (more or less)

 

yourClass.paintBackground( graphics );

yourClass paint( graphics );

 

so, you calling paint( graphics ), is redundent.

 

Use the code I posted above, but you only need the paint() method posted if you are going to add code to it, otherwise there is no need to override it.