屏幕撕裂JPanel游戏和双缓冲

时间:2014-08-17 11:05:32

标签: java swing buffer doublebuffered double-buffering

我目前正在使用JPanel从我的游戏中绘制元素,然而,偶尔会出现图形卡顿,经过一些研究我了解到屏幕撕裂和双缓冲,我认为这是解决我的问题的方法,但是,目前使用我的代码,我发现如果不撕掉很多代码就很难实现。

我想知道是否有人可以提供一种更简单的方法来修复我的屏幕撕裂小型2D平铺游戏,或者如何使用我当前的代码进行双缓冲,非常感谢!

Frame.java

public class Frame extends JPanel implements KeyListener {

public static Game game;
public static boolean[] mouseDown = new boolean[4];
public static int width, height;
public static Font font = new Font("Arial", Font.BOLD, 16);
public static JFrame frame;
public Frame()
{
    setFocusable(true);
    requestFocus();
    setOpaque(true);
    addKeyListener((KeyListener) this);
    addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
        }
        @Override
        public void mouseReleased(MouseEvent e) {

        }
    });
    addMouseMotionListener(new MouseMotionListener() {
        @Override
        public void mouseMoved(MouseEvent e) {
        }
        @Override
        public void mouseDragged(MouseEvent e) {
        }
    });
}

@Override
public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setBackground(Color.BLACK);
    g2d.setFont(font);
    super.setBackground(Color.BLACK);
    super.paintComponent(g2d);
    //This is where I use g2d to draw onto the JPanel
}

public static void main(String[] args) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            frame = new JFrame("Game");
            frame.add(new Frame());
            frame.setSize(1000, 750);
            frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }});
}

Game.java

    public void gameStart() { 

    Thread gameThread =  new Thread() {
        // Override run() to provide the running behavior of this thread.
        @Override
        public void run() {
            gameLoop();
        }
    };
    // Start the thread. start() calls run(), which in turn calls gameLoop().
    gameThread.start();
}

public void gameLoop() {
    while (!isGameFinished) {
        beginTime = System.nanoTime();
        gameUpdate();
        frame.repaint();
        // Delay timer to provide the necessary delay to meet the target rate
        timeTaken = System.nanoTime() - beginTime;
        timeLeft = (UPDATE_PERIOD_NSEC - timeTaken) / 1000000L;  // in milliseconds
        if (timeLeft < 10) timeLeft = 10;   // set a minimum
        try { // Provides the necessary delay and also yields control so that other thread can do work.
            Thread.sleep(timeLeft);
        } catch (InterruptedException ex) { }
    }
}

public void gameStart() { 

    Thread gameThread =  new Thread() {
        // Override run() to provide the running behavior of this thread.
        @Override
        public void run() {
            gameLoop();
        }
    };
    // Start the thread. start() calls run(), which in turn calls gameLoop().
    gameThread.start();
}

public void gameLoop() {
    while (!isGameFinished) {
        beginTime = System.nanoTime();
        gameUpdate();
        frame.repaint();
        timeTaken = System.nanoTime() - beginTime;
        timeLeft = (UPDATE_PERIOD_NSEC - timeTaken) / 1000000L;
        if (timeLeft < 10) timeLeft = 10;   // set a minimum
        try { 
            Thread.sleep(timeLeft);
        } catch (InterruptedException ex) { }
    }
}

如代码所示,Game类包含Game循环,它将不断重绘Frame类。如何调整此代码以允许双缓冲以消除屏幕撕裂?谢谢!

1 个答案:

答案 0 :(得分:0)

双缓冲是修复它的最佳方法。尝试拨打Toolkit#syncToolkit.getDefaultToolkit().sync()),这有时可以解决问题。如果切换到双缓冲,则不会拆除太多代码,只需将paint中的代码移动到单独的方法中即可。