1

I'm building a simple 2d java game and having some flickering issues even though I draw the graphics using a double buffering technique.

I've just got started and have a very simple game loop this far. But I think it should work fine anyway? Without any flickering.. Here's how I do it:

@Override
public void run() {
    while (running) {
        currentState.update();
        prepareGameImage();
        currentState.render(gameImage.getGraphics());
        repaint();
        try {
            Thread.sleep(14);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    System.exit(0);
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (gameImage == null) {
        return;
    }
    g.drawImage(gameImage, 0, 0, null);
}

private void prepareGameImage() {
    if (gameImage == null) {
        gameImage = createImage(gameWidth, gameHeight);
    }

    Graphics g = gameImage.getGraphics();
    g.clearRect(0, 0, gameWidth, gameHeight);
}

You can see how I mean in this video

I'm looking for any tips that could lead me to whats causing this glitch. Am I doing double buffering wrong?

Any help appreciated!

Thanks

malmling
  • 2,398
  • 4
  • 19
  • 33
  • 1
    That doesn't look to be a problem with double buffering. I see you painting a big white rectangle everytime you get your image. – arynaq Mar 21 '15 at 12:11
  • I'm not sure I get what you mean? I have a new project, with same drawing code but I never tell the program to draw anything white. – malmling Mar 21 '15 at 14:45
  • Swing has double-buffering by default. I guess the problem is in your currentState calculations. Anyway, you would get better answers sooner if you posted some simplified, but working code where you demonstrate the problem. – lbalazscs Mar 25 '15 at 04:00

0 Answers0