Okay so for a project I'm creating I've decided to create a JFrame and have a menu which when options are clicked will open up a canvas in the center of the JFrame. Since the rendering is all been done on a canvas I have double buffered it. Now when I first press the button and the canvas opens up it works perfectly no problems and the screen is double buffered however when I go back to the menu and reopen the canvas I get an error.
A thread is created as soon as the application is created so that's why I have used the if statement as I was getting an error when the buffer was attempting to be created with 0 height and 0 width since the canvas hadn't been added to the JFrame yet.
Here's the code to create the buffer:
public BufferStrategy createBuffer(){
if(this.getWidth() > 0 && this.getHeight() > 0){
image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(2);
return bs;
}
}else{
return bs;
}
return bs;
This is the code in the run() method for which I use that buffer:
if (shouldRender) {
Graphics g = null;
BufferStrategy bs = createBuffers();
if(bs != null){
g = bs.getDrawGraphics();
render(g, bs);
frames++;
}
}
In the render() method I then call g.dispose() and bs.show().
This is the error I am getting when I reopen the canvas:
Exception in thread "Thread-2" java.lang.IllegalStateException: Buffers have not been created
at sun.awt.windows.WComponentPeer.getBackBuffer(Unknown Source)
at java.awt.Component$FlipBufferStrategy.getBackBuffer(Unknown Source)
at java.awt.Component$FlipBufferStrategy.flip(Unknown Source)
at java.awt.Component$FlipBufferStrategy.show(Unknown Source)
at Launcher.render(Launcher.java:106)
at Launcher.run(Launcher.java:78)
at java.lang.Thread.run(Unknown Source)
I have tried creating a BufferStrategy of (1) and it returned no errors but whenever I use 2 or more I get an error. I also tried checking the BufferCapabilities and when I ran the getFlipContents() it returned undefined.
If someone could explain to me where I am going wrong it would be greatly appreciated as I have spent hours struggling to fix this.