天天看點

雙緩沖與Java swing

Image imageBuffer = null;

public void update(Graphics g) {
        if (imageBuffer == null) {
            imageBuffer = createImage(GAME_WIDTH, GAME_HEIGHT);

        }
        Graphics graphics = imageBuffer.getGraphics();
        Color color = graphics.getColor();
        graphics.setColor(Color.GREEN);
        graphics.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
        graphics.setColor(color);
        paint(graphics);

        graphics.dispose();
        g.drawImage(imageBuffer, 0, 0, null);
    }

private class PaintThread implements Runnable

        public void run() {
            while (true) {
                repaint();
                try {
                    Thread.sleep(20);
                } catch