Java-无法创建Backbuffer

时间:2018-07-30 14:02:57

标签: java graphics awt buffering double-buffering

所以我有这段代码

//Private Vars
private static final Frame frame = new Frame("UX test");

//Private Classes
private static class WListener implements WindowListener{...}

private static BufferStrategy strat;
//Public Vars
private static class KListener implements KeyListener{...}

static Texture texture = new Texture(0, 0, "HELP.png");

public static void main(String[] args) {
    frame.addWindowListener(new WListener());
    frame.addKeyListener(new KListener());
    frame.setVisible(true);
    frame.setBounds(0, 0, 500, 500);
    frame.createBufferStrategy(2);
    strat = frame.getBufferStrategy();

    while(frame.isVisible()) {
        Graphics g = strat.getDrawGraphics();
        g.clearRect(0, 0, frame.getWidth(), frame.getHeight());
        g.drawImage(texture.GetImage(), texture.x, texture.y, frame);
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        g.dispose();
        strat.show();
    }

    public static class Texture{...}
}

纹理只是一个对象,其中包含我通过imageIO获取的图像

可悲的是,大多数情况下,我启动应用程序时,控制台会为我提供以下堆栈跟踪

Exception in thread "main" java.lang.IllegalStateException: Buffers have not been created
    at java.desktop/sun.awt.windows.WComponentPeer.getBackBuffer(WComponentPeer.java:1018)
    at java.desktop/java.awt.Component$FlipBufferStrategy.getBackBuffer(Component.java:4123)
    at java.desktop/java.awt.Component$FlipBufferStrategy.updateInternalBuffers(Component.java:4108)
    at java.desktop/java.awt.Component$FlipBufferStrategy.createBuffers(Component.java:4099)
    at java.desktop/java.awt.Component$FlipBufferStrategy.<init>(Component.java:4038)
    at java.desktop/java.awt.Component$FlipSubRegionBufferStrategy.<init>(Component.java:4566)
    at java.desktop/java.awt.Component.createBufferStrategy(Component.java:3901)
    at java.desktop/java.awt.Window.createBufferStrategy(Window.java:3392)
    at java.desktop/java.awt.Component.createBufferStrategy(Component.java:3825)
    at java.desktop/java.awt.Window.createBufferStrategy(Window.java:3367)
    at Start.main(Start.java:118)

我只是不知道如何解决它,因为它偶尔会很好地工作。

This is the actual working output

所以我的问题是:

  • 现在有人为什么会这样吗?
  • 如何摆脱此错误?

0 个答案:

没有答案