BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 09-16-2009, 03:31 AM   #1 (permalink)
Knows Where the Search Button Is
 
Join Date: Jul 2009
Model: 8800
PIN: N/A
Carrier: Vodafone
Posts: 15
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Painting using the BB UI

Please Login to Remove!

Hi. I'm trying to create a simple application that draws some things on the screen. If I do the drawing inside the paint(Graphics graphics) method everything works fine, but if I try to do it in another method outside paint it won't work at all, the screen is left white. Here is the code:

Code:
public class blackberryApp extends UiApplication {
    public static void main(String[] args) {
        blackberryApp theApp = new blackberryApp();
        theApp.enterEventDispatcher();
    }

    public blackberryApp() {
        //display a new screen
        GameScreen Game = new GameScreen();
        Game.start();
        pushScreen(Game);
    }
}

public class GameScreen extends FullScreen implements Runnable {    
    public Graphics theGraphics;
    public GameScreen() {
        super();
    }
    public void start() {
        theGraphics = getGraphics();
        mainThread = new Thread(this);
        mainThread.start();
    }
    public void paint(Graphics graphics) {
    }
    public void draw() {
        if (theGraphics != null) {
            theGraphics.pushRegion(new XYRect(0, 0, 240, 260));
            theGraphics.setColor(Graphics.BLACK);
            theGraphics.setColor(0x00FF00);
            theGraphics.fillRect(50, 50, 30, 30);
            theGraphics.setColor(Graphics.WHITE);
            theGraphics.drawText("It works", 20, 20);
            theGraphics.popContext();
        }
    }
    public final void run() {
        while (true) {
            draw();
            invalidate();
            try {
            Thread.sleep(100);
            } catch (Exception e)
            {
            }
        }
    }
}
The BB UI supports double buffering or I have to create an auxiliary buffer to draw inside it first and then draw the buffer to the screen in the paint method? Maybe I should make "theGraphics = getGraphics()" somewhere else, like before I call in the draw() method?
Offline  
Old 09-16-2009, 08:02 AM   #2 (permalink)
Knows Where the Search Button Is
 
Join Date: Aug 2006
Model: 7100x
Carrier: Vodafone
Posts: 33
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello,

The paint callback is called to paint the screen by the blackberry.
It is not supposed to be in an different way...

The best option is to have your painting code in the paint routine.
And just call the invalidate from the thread.

If you really wants to paint in a different routine, like an offset buffer, you must create a bitmap buffer and get the graphics for it.
And on paint routine blit this bitmap to screen(a litle more complicated).

You should keep it simple and draw on paint(my suggestion), but let me know why do you need to paint in other routine?

CrucialSoft
Offline  
Old 09-16-2009, 08:10 AM   #3 (permalink)
Knows Where the Search Button Is
 
Join Date: Jul 2009
Model: 8800
PIN: N/A
Carrier: Vodafone
Posts: 15
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm making a game so it's a lot of painting and sometimes it has to be outside the routine. But I managed to solve my problem. My screen wasn't showing anything because in the blackberryApp constructor I called the Game.start() before pushScreen(Game), so later when I was doing the
Code:
Graphics = getGraphics();
I wasn't receiving the correct graphics context. And I also used this.updateDisplay() in the end of each drawing method. Now everything is ok, I hope I won't encounter other problems. Thank you for your help.
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.