简体   繁体   English

Java Applet缓冲映像

[英]Java Applet Buffering images

OK so here's my code: http://www.so.pastebin.com/Qca4ERmy 好的,所以这是我的代码: http : //www.so.pastebin.com/Qca4ERmy

I am trying to use buffers so the applet won't flicker upon redraw() but it seems I am having trouble. 我正在尝试使用缓冲区,因此applet在redraw()时不会闪烁,但是似乎我遇到了麻烦。 The applet still flickers.... 小程序仍然闪烁。

Help? 救命?

Thank you. 谢谢。

I made a quick video about this problem: http://www.vimeo.com/12035196 我制作了有关此问题的快速视频: http : //www.vimeo.com/12035196

Create a Swing applet. 创建一个Swing applet。 Swing is double buffered by default so you should not have this problem. 默认情况下,Swing是双缓冲的,因此您应该不会遇到此问题。 Start with the section from the Swing tutorial on How to Make Applets for the proper way to create a Swing applet. 从Swing教程中有关如何制作Applet的部分开始,以了解创建Swing Applet的正确方法。

You can try to solve this issue using a BufferedImage , in this way you just create a BufferedImage that is compatible with your frame and then draw everything there before blitting the whole image onto the JFrame 's content. 您可以尝试使用BufferedImage解决此问题,以这种方式,您只需创建一个与框架兼容的BufferedImage ,然后在将整个图像拖到JFrame的内容上之前在其中绘制所有内容。

A better approach is to use automatic buffering with BufferStrategy class, you can read a tutorial about it here . 更好的方法是对BufferStrategy类使用自动缓冲,您可以在此处阅读有关它的教程。

The best way I've done it is to create another image the same size as your applet, draw to that, then in your paint / update method copy the contents of that image to your graphics object. 我做过的最好方法是创建另一个与小程序大小相同的图像,然后绘制到该图像上,然后在paint / update方法中将该图像的内容复制到图形对象中。 You have to make sure that you aren't updating the other image when you draw to your applet otherwise it will cause flicker. 您必须确保在绘制到Applet时未更新其他图像,否则会导致闪烁。 Drawing should probably be done in another Thread as well, just to make things a little easier to understand. 绘制可能也应该在另一个线程中完成,只是为了使事情更容易理解。

I don't have access to my code so the following might be a little off (and the code may not be the most efficient): 我无权访问我的代码,因此以下内容可能会有点过时(并且代码可能不是最有效的):

public class MyApplet extends Applet {

    Image offscreen;
    boolean pageFlipped = false;
    Thread drawingThread;

    public void init() {
        offscreen = createImage(this.getWidth(), this.getHeight());
        drawingThread = new Thread(new DrawingLoop());
        drawingThread.start();
    }

    public void update(Graphics g) {
        paint(g);
    }
    public void paint(Graphics g) {
        if (!pageFlipped) {
            g.drawImage(offscreen, 0, 0);
            pageFlipped = true;
        }
    }

    class DrawingLoop implements Runnable {
        public void run() {
            while (true) {
                Graphics g = offscreen.getGraphics();
                if (pageFlipped) {
                    // do your graphics code here
                    pageFlipped = false;
                }
            }
        }
    }
}

Hope this helps! 希望这可以帮助!

-Dan -担

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM