Search code examples
javaswingbufferinggame-loop

How do you create a flicker free Java game loop?


I have been trying to create a Java game loop that displays something simple to start with, but no matter what I try I can not get it too stop flickering. I have tried googling solutions but either my google-fu isn't quite up to scratch or I am doing something seriously wrong.

I'm posting this code here in the hope that I am simply doing something wrong that can be corrected. At this point I am tempted to start over in a different language, last time I used it SFML was nice. Apologies for the horrible code.

Main.java:

public class Main {
    public static void main(String[] args) {
        new GameFrame("Game");
    }
}

GameFrame.java:

import java.awt.Color;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.Toolkit;

import javax.swing.JFrame;

public class GameFrame extends JFrame {
    protected GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();

    public GameFrame(String title) {
        super(title);
        init();
    }

    public void init() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        // Calculate the size of the window, taking into account the size of toolbars etc.
        Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(device.getDefaultConfiguration());
        setSize(
            (int) ((device.getDefaultConfiguration().getBounds().width - insets.left - insets.right) * 0.8),
            (int) ((device.getDefaultConfiguration().getBounds().height - insets.top - insets.bottom) * 0.8)
        );

        // Centre and start maximised.
        setLocationRelativeTo(null);
        setExtendedState(MAXIMIZED_BOTH);

        if(false) {
            // Important otherwise you get an ugly border.
            super.setVisible(false);
            setUndecorated(true);
            device.setFullScreenWindow(this);
        } else {
            setUndecorated(false);
            device.setFullScreenWindow(null);
            super.setVisible(true);
        }

        GamePanel panel = new GamePanel();

        panel.setBackground(Color.BLACK);
        new Thread(panel).start();

        add(panel);
    }
}

GamePanel.java:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;

import javax.swing.JPanel;

public class GamePanel extends JPanel implements Runnable {
    protected boolean running = false;

    protected long lastTime;

    protected int width  = 0;
    protected int height = 0;

    protected Image image;
    protected Graphics2D graphics;

    protected double x = 0;
    protected double y = 0;

    protected void updateState(double delta) {
        x = x + 0.00001 * delta;
        y = y + 0.00001 * delta;
    }

    public void run() {
        running  = true;
        lastTime = System.nanoTime();

        while(width == 0 || height == 0) {
            width  = getBounds().width;
            height = getBounds().height;
        }

        setDoubleBuffered(true);

        while(running) {
            long   now          = System.nanoTime();
            long   updateLength = now - lastTime;
            double delta        = updateLength / (10 ^ 9 / 60);

            updateState(delta);

            paintComponent(getGraphics());

            lastTime = System.nanoTime();

            try {Thread.sleep((now - lastTime + (10 ^ 9 / 60)) / (10 ^ 6));} catch (Exception e) {}
        }
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        if(isVisible() && width > 0 && height > 0) {

            setDoubleBuffered(true);

            if(image == null) {
                image    = createImage(width, height);
                graphics = (Graphics2D) image.getGraphics();
                graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            }

            graphics.setColor(Color.black);
            graphics.fillRect(0, 0, width, height);

            graphics.setColor(Color.WHITE);
            graphics.fillRect(100, 20, 100, 100);

            graphics.fillOval((int) x, (int) y, 30, 30);

            g.drawImage(image, 0, 0, this);
        }
    }
}

Solution

  • Your program fragments are incorrect in several ways:

    • Swing GUI objects should be constructed and manipulated only on the event dispatch thread.

    • Don't use getGraphics(); the graphics context is only valid in paintComponent().

    • JPanel is double buffered by default.

    • Use an instance of javax.swing.Timer to pace the animation; a complete example is seen in this AnimationTest.