double buffered custom paint() for a game

41 views
Skip to first unread message

Matthias Bay

unread,
Nov 1, 2013, 11:41:01 AM11/1/13
to codenameone...@googlegroups.com
Hi,

I had some flickering in my game, so I made a double buffer. For the game I use a custom Form.
It has an Image offscreen and a Thread that calls offPaint() up to 60 times per second.
In the paint() method it calls g.drawImage(offscreenImage, 0, 0);
The offPaint() method works like this:
    public void offPaint() {
       
Image offscreenImage2 = Image.createImage(getWidth(), getHeight());
       
if (offscreenImage2 != null) {
           
Graphics g = offscreenImage2.getGraphics();
           
//draw things with g
            offscreenImage
= offscreenImage2;
       
}
   
}
In a JavaSE game I was able to get rid of the flickering with this.
In CN1 there are no freezes or errors, but there still is flickering. (tested on the Simulator and j2me)

Can you help me? Or is there a different, better way to prevent flickering?

Thanks in advance, Matthias Bay

Shai Almog

unread,
Nov 1, 2013, 4:01:28 PM11/1/13
to codenameone...@googlegroups.com
Hi,
Codename One is already double buffered so this is really not the right way to go.
I don't see a reason for an offline thread here.

I'm assuming that you are seeing flickering because the screen is being cleared by something, how are you drawing to the screen and what are you doing specifically.

Matthias Bay

unread,
Nov 2, 2013, 8:42:04 AM11/2/13
to codenameone...@googlegroups.com
Ok, it was my fault^^
There was flickering because the player was moving while painting, so some textures were painted wrong.
I synchronized the paint method and the method that was changing the players position and flickering was gone.

Shai Almog

unread,
Nov 2, 2013, 1:21:35 PM11/2/13
to codenameone...@googlegroups.com
I would suggest not using 2 threads at all. Try to do everything on the EDT to reduce synchornization costs. In fact synchronization is so expensive you will gain more performance by that strategy.

Unless you do io/networking I would stick to the EDT for almost anything.
Reply all
Reply to author
Forward
0 new messages