Java Applet 缓冲图像

发布于 2024-09-03 03:26:22 字数 345 浏览 8 评论 0原文

好的,这是我的代码: http://www.so.pastebin.com/Qca4ERmy

我我正在尝试使用缓冲区,以便小程序不会在 redraw() 时闪烁,但似乎我遇到了麻烦。小程序仍然闪烁......

帮忙吗?

谢谢。

我制作了一个有关此问题的快速视频:http://www.vimeo.com/12035196

OK so here's my code: 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. The applet still flickers....

Help?

Thank you.

I made a quick video about this problem: http://www.vimeo.com/12035196

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟凡古楼 2024-09-10 03:26:22

创建一个 Swing 小程序。 Swing 默认情况下是双缓冲的,因此您不应该遇到此问题。从 Swing 教程中关于如何制作 Applet 了解创建 Swing 小程序的正确方法。

Create a Swing applet. Swing is double buffered by default so you should not have this problem. Start with the section from the Swing tutorial on How to Make Applets for the proper way to create a Swing applet.

暗喜 2024-09-10 03:26:22

我这样做的最好方法是创建另一个与您的小程序大小相同的图像,绘制到该图像,然后在您的绘制/更新方法中将该图像的内容复制到您的图形对象。您必须确保在绘制小程序时没有更新其他图像,否则会导致闪烁。绘图可能也应该在另一个线程中完成,只是为了让事情更容易理解。

我无权访问我的代码,因此以下内容可能有点不对劲(并且代码可能不是最有效的):

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

希望这有帮助!

-担

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. You have to make sure that you aren't updating the other image when you draw to your applet otherwise it will cause flicker. 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

寄与心 2024-09-10 03:26:22

您可以尝试使用 BufferedImage 来解决这个问题,这样您只需创建一个与您的框架兼容的 BufferedImage ,然后在将整个图像传输到上面之前绘制所有内容。 JFrame 的内容。

更好的方法是通过 BufferStrategy 类使用自动缓冲,您可以阅读有关它的教程 这里

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.

A better approach is to use automatic buffering with BufferStrategy class, you can read a tutorial about it here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文