“Buffers have not been created” … whilst creating buffers

后端 未结 2 1135
时光说笑
时光说笑 2021-01-19 01:33

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

    // Buffer
    container.createBufferStrategy(2);                


        
2条回答
  •  心在旅途
    2021-01-19 02:20

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

提交回复
热议问题