0

I am programming a platformer game in Java. Everything works fine and there are no bigger problems, except the flickering on the screen when the Canvas is updated.

I'll roughly explain how my game "engine" works: In my main method, I have a Loop, that is repeating itself 30 times a second:

while (play) {
    Date delay_time = new Date();
    delay_time.setTime(delay_time.getTime() + (int) (1000*(1.0/FPS)));

    // Here is all the game stuff, like the motion of the player,
    // function calling, etc.

    Graphics g = can.getGraphics();
    can.update(g);
    while(new Date().before(delay_time)) {

    }

}

My FPS variable is a static final int that is currently set to 30. So i am updating my Canvas 30 time a second.

Play is a boolean, which controls if the game is still playing, or the player died.

Can is an instance of my class MyCanvas.java. The Code of the paint() method looks like so:

   if (Main.play) {
       //NOW
       //Draw the now Level Caption
       //SIZE: 240
       g2.setFont(new Font("Bank Gothic", Font.PLAIN, 240));
       g2.setColor(new Color(Math.abs(bg.getRed() - 30), Math.abs(bg.getGreen() - 30), Math.abs(bg.getBlue() - 30)));
       g2.drawString("LEVEL " + Main.lvl, 80, 80 + 300 - Main.groundY);

       //Draw the ground
       g2.setColor(haba);
       g2.fillRect(0, Main.screenSize.height - Main.groundY, Main.screenSize.width, Main.groundY);

       //Draw the level
       g2.setColor(haba);
       for (int i = 0; i < Main.LVLWIDTH; i++) {
            for (int j = 0; j < Main.LVLHEIGHT; j++) {
                if (Main.level[i][j] == '1') {
                    g2.fillRect(i*(Main.BLOCK_X), (j - Main.LVLHEIGHT)*(Main.BLOCK_Y) + Main.screenSize.height - Main.groundY, Main.BLOCK_X, Main.BLOCK_Y);
                }

        }

        //More drawing stuff...

    }

So, as you can see, I am updating quite a lot - 30 times a second. This leads to the problem that my game (which is playing in fullscreen mode BTW) flickers all the time. How do solve that problem? Is a Bufferstrategy the right way? And if yes, how should I do it? Can you please explain the Bufferstartegy or provide links that have great tutorials or explanations, which or not to technical and non-beginner friendly? That would be great.

rekaszeru
  • 19,130
  • 7
  • 59
  • 73
Nicolas Brauch
  • 497
  • 1
  • 4
  • 10
  • AWT components aren't double buffered, Swing components are. You should be sunny getGraphics, it's not how painting is done! you should be overriding the appropriate paint method. I'd only consider using a BufferedStrategy if want absolute control over the painting process, this then excludes been able to use Swing components of any kind, don't know if that's a an issue, but you need to beware of the restriction – MadProgrammer May 17 '14 at 10:57
  • Swing components are double buffered by default. I've done two examples, one component based (so the entities are actual components), which was able animated around 5000 individual elements at 25fps and another which got over 10000 painted objects (by overriding paintComponent of a JPanel) at 25fps without much issue. Just saying – MadProgrammer May 17 '14 at 10:59
  • Another issue in yr code is your forced delay loop. Thread.sleep would put the loop thread to sleep, using 0% CPU! allowing for the other threads to function better – MadProgrammer May 17 '14 at 11:02
  • Have you tried looking at [Full-Screen Exclusive Mode API](http://docs.oracle.com/javase/tutorial/extra/fullscreen/) – MadProgrammer May 17 '14 at 11:04

1 Answers1

0

The BufferStrategy class solved my problem. Here is a good link to it, with a well made example of how to use it:
http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferStrategy.html

Nicolas Brauch
  • 497
  • 1
  • 4
  • 10