“Buffers have not been created” … whilst creating buffers

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 18:21:21

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

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!