你如何创建一个无闪烁的Java游戏循环?

时间:2015-01-17 23:37:37

标签: java swing buffering game-loop

我一直在尝试创建一个Java游戏循环,显示一些简单的开始,但无论我尝试什么,我都无法得到它停止闪烁。我已经尝试了google搜索解决方案,但要么我的google-fu还没有完全从头开始,或者我正在做一些严重的错误。

我在这里发布此代码,希望我只是做错了可以纠正的错误。在这一点上,我很想用另一种语言重新开始,上次我用它时SFML很好。为可怕的代码道歉。

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);
        }
    }
}

1 个答案:

答案 0 :(得分:3)

您的程序片段在几个方面不正确: