“尚未创建缓冲区” ...同时创建缓冲区

发布于 2024-09-13 13:41:14 字数 1323 浏览 9 评论 0原文

我有(我认为是)一个简单的 JFrame BufferStrategy。它是这样创建的:

    // Buffer
    container.createBufferStrategy(2);           
    strategy = container.getBufferStrategy();

但是,偶尔我会收到以下错误(它指向前面代码片段的第一行作为违规项):

java.lang.IllegalStateException:尚未创建缓冲区

错误的出现和消失是很奇怪的——有时会被触发,有时不会。我怀疑这意味着这是一个线程问题。有人对这里可能发生的情况有任何指示吗?我有点不知所措,因为我已经在尝试做 Java 所说的事情了!

编辑:完整跟踪:

Exception in thread "main" java.lang.IllegalStateException: Buffers have not been created
        at sun.awt.windows.WComponentPeer.getBackBuffer(WComponentPeer.java:877)
        at java.awt.Component$FlipBufferStrategy.getBackBuffer(Component.java:3815)
        at java.awt.Component$FlipBufferStrategy.updateInternalBuffers(Component.java:3800)
        at java.awt.Component$FlipBufferStrategy.createBuffers(Component.java:3791)
        at java.awt.Component$FlipBufferStrategy.<init>(Component.java:3730)
        at java.awt.Component$FlipSubRegionBufferStrategy.<init>(Component.java:4253)
        at java.awt.Component.createBufferStrategy(Component.java:3612)
        at java.awt.Window.createBufferStrategy(Window.java:3015)
        at java.awt.Component.createBufferStrategy(Component.java:3536)
        at java.awt.Window.createBufferStrategy(Window.java:2990)

I have (what I thought was) a straightforward BufferStrategy for a JFrame. It is created like so:

    // Buffer
    container.createBufferStrategy(2);           
    strategy = container.getBufferStrategy();

However, occassionally I receive the following error (which points to the first line of the preceeding snippet as the offending item) :

java.lang.IllegalStateException: Buffers have not been created

This error is peculiar as it comes and goes - sometimes it is triggered, sometimes not. I suspect this means it's a threading issue. Does anyone have any pointers as to what might be going on here? I'm a little at a loss, since I'm already trying to do what Java says it wants me to do!

edit: full trace:

Exception in thread "main" java.lang.IllegalStateException: Buffers have not been created
        at sun.awt.windows.WComponentPeer.getBackBuffer(WComponentPeer.java:877)
        at java.awt.Component$FlipBufferStrategy.getBackBuffer(Component.java:3815)
        at java.awt.Component$FlipBufferStrategy.updateInternalBuffers(Component.java:3800)
        at java.awt.Component$FlipBufferStrategy.createBuffers(Component.java:3791)
        at java.awt.Component$FlipBufferStrategy.<init>(Component.java:3730)
        at java.awt.Component$FlipSubRegionBufferStrategy.<init>(Component.java:4253)
        at java.awt.Component.createBufferStrategy(Component.java:3612)
        at java.awt.Window.createBufferStrategy(Window.java:3015)
        at java.awt.Component.createBufferStrategy(Component.java:3536)
        at java.awt.Window.createBufferStrategy(Window.java:2990)

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

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

发布评论

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

评论(2

暖风昔人 2024-09-20 13:41:14

当您调用createBufferStrategy 时,框架需要可显示。另外,正如 camickr 所指出的,您需要从 EDT 中调用它。

确保这一点的一种方法是扩展 JFrame 并覆盖 addNotify

class MyFrame extends JFrame {
    public void addNotify() {
        super.addNotify();
        // Buffer
        createBufferStrategy(2);           
        strategy = getBufferStrategy();
    }
}

The frame needs to be displayable when you call createBufferStrategy. Also as camickr has pointed out you need to call it from the EDT.

One way to ensure this is to extend JFrame and override addNotify:

class MyFrame extends JFrame {
    public void addNotify() {
        super.addNotify();
        // Buffer
        createBufferStrategy(2);           
        strategy = getBufferStrategy();
    }
}
春夜浅 2024-09-20 13:41:14

Swing 组件默认情况下是双缓冲的,因此无需使用 BufferStrategy。

然而,当您遇到这样的随机错误时,通常是因为代码未在 EDT 上执行。有关详细信息,请阅读 Swing 教程中有关并发的部分。

Swing components are double buffered by default, so there is no need to play around with a BufferStrategy.

However when you get random errors like that its usually because code is not executed on the EDT. Read the section from the Swing tutorial on Concurrency for more information.

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