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: 296
Registered: ‎12-07-2008
My Device: Not Specified
Accepted Solution

Wont show background image

I've tried almsot everything. I've spent hours reading the API refernce and examples. I still can't seem to get this simple code to work. Here is what I have, which I would like to use to display an image as the background.

 

 

class StartGame extends MainScreen { StartGame(){ super(NO_VERTICAL_SCROLL | NO_VERTICAL_SCROLLBAR); setTitle("Start Game"); } protected void paintBackground( Graphics g ){ g.clear(); Bitmap bgImage = Bitmap.getBitmapResource("img/bg.png"); g.drawBitmap(0, 0, 360, 480, bgImage, 0, 0); } }

 

 

 

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

Re: Wont show background image

Haven't actually tried this myself, but noted this post which might help:

 

How to - Using paintBackground()

http://supportforums.blackberry.com/rim/board/message?board.id=java_dev&message.id=13262&query.id=59...

Developer
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Wont show background image

There are two steps to debug this. First is to make sure paintBackground() is excuting correctly. If you fill the screen with red, does it appear. If not, then first figure out why. If it does, then something is wrong with the Bitmap loading or painting.
Developer
Posts: 296
Registered: ‎12-07-2008
My Device: Not Specified

Re: Wont show background image

Well I tried to use a solid color and that didn't work either. I placed the setTitle("Start Game")' in the paintBackground method and that worked just fine. So I think the paintBackground method is working fine. I'm not sure how to debug this though.
Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Wont show background image

paintBackground() definitely works, I use it.

 

Make sure you check the return bitmap from getBitmapResource() - it could be null.

 

One piece of advice, though - don't call getBitmapResource in this routine. Loading and converting the image from the COD file is a time-consuming process, and it will be repeated each time the screen is invalidated(). Instantiate the image in your constructor, instead.

 

Developer
Posts: 296
Registered: ‎12-07-2008
My Device: Not Specified

Re: Wont show background image


RexDoug wrote:

paintBackground() definitely works, I use it.

 

Make sure you check the return bitmap from getBitmapResource() - it could be null.

 

One piece of advice, though - don't call getBitmapResource in this routine. Loading and converting the image from the COD file is a time-consuming process, and it will be repeated each time the screen is invalidated(). Instantiate the image in your constructor, instead.

 


Sorry, I'm new to blackberry programming and java/j2me in generalcan you give me a brief code snippt by any chance?

 

I ran this code 

System.out.println(bgImage);

and it outputted "net.rim.device.api.system.Bitmap@c4ea4980"

 

Not sure what to make of this, but the method is difentially being called. I just can't get it to work for some reason.

 

Developer
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Wont show background image

Screen.paint() is calling Graphics.clear() and wiping out everything you draw in paintBackground(). paint() does not call paintBackground(). paintBackground() is called first, and then paint() is called, but for some reason Screen has been calling clear().

 

See this thread:

http://supportforums.blackberry.com/rim/board/message?board.id=java_dev&message.id=3800&query.id=680...

Developer
Posts: 296
Registered: ‎12-07-2008
My Device: Not Specified

Re: Wont show background image


mreed wrote:

Screen.paint() is calling Graphics.clear() and wiping out everything you draw in paintBackground(). paint() does not call paintBackground(). paintBackground() is called first, and then paint() is called, but for some reason Screen has been calling clear().

 

See this thread:

http://supportforums.blackberry.com/rim/board/message?board.id=java_dev&message.id=3800&query.id=680...


 

I just tried this code straight from RIM's site

void DecorTestScreen() { _manager = (VerticalFieldManager)getMainManager(); Background bg = BackgroundFactory.createSolidBackground(Color.RED); _manager.setBackground(bg); XYEdges edges = new XYEdges(20, 20, 20, 20); Border border = BorderFactory.createBevelBorder(edges); _manager.setBorder(border); _manager.add(new LabelField("test")); _manager.add(new LabelField("test 1")); _manager.add(new LabelField("test 3")); System.out.println("paint background called"); }

 

Found Here and I still have a white screen. The System.out.println works just fine so I know it's calling the method but for some reason it just doesn't want to pain the background. Any ideas? I'm using RIM's jde v4.7

 

Thanks

Highlighted
Developer
Posts: 296
Registered: ‎12-07-2008
My Device: Not Specified

Re: Wont show background image

I finally got it working. For those who care this is the code I used. Thanks everyone

class StartGame extends MainScreen { //Vars private VerticalFieldManager _manager; StartGame() { //Set Background _manager = (VerticalFieldManager)getMainManager(); Background bg = BackgroundFactory.createBitmapBackground(Bitmap.getBitmapResource("img/bg.png")); _manager.setBackground(bg); } }