0

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.

Patch
  • 203
  • 2
  • 3
  • 6
  • 1
    `Since the rendering is all been done on a canvas I have double buffered it.` Why use a Canvas with double buffering. A JPanel is double buffered by default. Read the section from the Swing tutorial on [Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) for more information and examples. – camickr Sep 06 '14 at 01:33
  • @camickr Okay thanks, but just wondering then what would a canvas actually be used for if it's much simpler to just create a JPanel – Patch Sep 06 '14 at 06:43
  • A Canvas was used with AWT. A JPanel is used with Swing. – camickr Sep 06 '14 at 14:34

0 Answers0