我无法获得双缓冲工作

时间:2014-07-11 23:29:40

标签: java graphics japplet doublebuffered

我已经尝试了很多教程,我似乎无法获得双缓冲工作。这是我的主要(update()方法是我试过的代码,但我仍然看到闪烁):

public class Main extends JApplet implements Runnable {
    private static final long serialVersionUID = 1L;
    private static int width = 900;
    private static int height = 600;
    public static int fps = 60;
    public Thread thread = new Thread(this);
    private Image dbImage;
    private Graphics dbg;
    public static Ailoid ailoid = new Ailoid();

    // Initialize
    public void init() {
        setSize(width, height);
        setBackground(Color.white);

        ailoid.setLocation(new Location(100, 100));
        AlienManager.registerAlien(ailoid);
    }

    // Paint graphics
    public void paint(Graphics g) {
        super.paint(g);
        g.setColor(Color.green);
        for (Alien alien : AlienManager.getAliens()) {
            Location loc = alien.getLocation();
            int x = loc.getX();
            int y = loc.getY();
            g.fillRect(x, y, 10, 20);
        }
    }

    // Update graphics for double buffering
    public void update(Graphics g) {
        if (dbImage == null) {
          dbImage = createImage (width, height);
          dbg = dbImage.getGraphics();
        }

        dbg.setColor (getBackground ());
        dbg.fillRect (0, 0, width, height);

        dbg.setColor (getForeground());
        paint (dbg);

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

    // Thread start
    @Override
    public void start() {
        thread.start();
    }
    // Thread stop
    @Override
    public void destroy() {
        thread = null;
    }

    // Thread run
    @Override
    public void run() {
        while (thread != null) {
            Updater.run();
            repaint();
            try {
                // 1000 divided by fps to get frames per millisecond
                Thread.sleep(1000 / fps);
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

如果有人可以提供帮助,我们将不胜感激!

1 个答案:

答案 0 :(得分:0)

不是逐个绘制对象,而是在image上绘制它们,然后告诉renderer绘制整个image。这消除了 闪烁

这是 示例 ,了解如何完成it

class DoubleBufferedCanvas extends Canvas {

    public void update(Graphics g) {
    Graphics offgc;
    Image offscreen = null;
    Dimension d = size();

    // create the offscreen buffer and associated Graphics
    offscreen = createImage(d.width, d.height);
    offgc = offscreen.getGraphics();
    // clear the exposed area
    offgc.setColor(getBackground());
    offgc.fillRect(0, 0, d.width, d.height);
    offgc.setColor(getForeground());
    // do normal redraw
    paint(offgc);
    // transfer offscreen to window
    g.drawImage(offscreen, 0, 0, this);
    }
}