views:

47

answers:

3

OK so here's my code: http://www.so.pastebin.com/Qca4ERmy

I am trying to use buffers so the applet won't flicker upon redraw() but it seems I am having trouble. The applet still flickers....

Help?

Thank you.

I made a quick video about this problem: http://www.vimeo.com/12035196

+2  A: 

You can try to solve this issue using a BufferedImage, in this way you just create a BufferedImage that is compatible with your frame and then draw everything there before blitting the whole image onto the JFrame's content.

A better approach is to use automatic buffering with BufferStrategy class, you can read a tutorial about it here.

Jack
I tried adding createBufferStragtegy.. but all I get is:C:\Users\Dan\Documents\DanJavaGen\tileGen.java:39: cannot find symbolsymbol : method createBufferStrategy(int)location: class tileGencreateBufferStrategy(2);I have the right imports...
Dan
+3  A: 

Create a Swing applet. Swing is double buffered by default so you should not have this problem. Start with the section from the Swing tutorial on How to Make Applets for the proper way to create a Swing applet.

camickr
Not true. I just turned my applet into a JApplet and it still does it.
Dan
Well, you have a coding problem somewhere. For example, why would you be reading in a file every time you paint the screen?
camickr
+2  A: 

The best way I've done it is to create another image the same size as your applet, draw to that, then in your paint / update method copy the contents of that image to your graphics object. You have to make sure that you aren't updating the other image when you draw to your applet otherwise it will cause flicker. Drawing should probably be done in another Thread as well, just to make things a little easier to understand.

I don't have access to my code so the following might be a little off (and the code may not be the most efficient):

public class MyApplet extends Applet {

    Image offscreen;
    boolean pageFlipped = false;
    Thread drawingThread;

    public void init() {
        offscreen = createImage(this.getWidth(), this.getHeight());
        drawingThread = new Thread(new DrawingLoop());
        drawingThread.start();
    }

    public void update(Graphics g) {
        paint(g);
    }
    public void paint(Graphics g) {
        if (!pageFlipped) {
            g.drawImage(offscreen, 0, 0);
            pageFlipped = true;
        }
    }

    class DrawingLoop implements Runnable {
        public void run() {
            while (true) {
                Graphics g = offscreen.getGraphics();
                if (pageFlipped) {
                    // do your graphics code here
                    pageFlipped = false;
                }
            }
        }
    }
}

Hope this helps!

-Dan

Dan Watling