BufferStrategy.getDrawGraphics() sometimes fails after a swap to fullscreen-exclusive mode
<p>I initialize an extended jFrame with a BufferStrategy and so forth, getting a nice animating circle on the screen. I have set a key listener (outside of the update-draw thread) that tells the update-draw thread to change to and from fullscreen exclusive mode, without doing updates or draws until the change is done. This usually works, but on occasion, the draw part of the update-draw thread (update does nothing so far) will have an exception and crash immediately after the switch. Most of the time, it doesn't do this crash, but even this is unacceptable for any program.</p>

<p>Edit: Ah, it's done it again. Notably, though, it actually does it <em>a few frames after the switch</em>, and it seems to only do it on the first time running after my computer starts up! Here's the stack trace:</p>

<pre><code>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.updateInternalBuffers(Unknown Source)
at java.awt.Component$FlipBufferStrategy.revalidate(Unknown Source)
at java.awt.Component$FlipBufferStrategy.revalidate(Unknown Source)
at java.awt.Component$FlipBufferStrategy.getDrawGraphics(Unknown Source)
at albeyamakiir.lightspeed.LightSpeedFrame.screenUpdate(LightSpeedFrame.java:194)
at albeyamakiir.lightspeed.LightSpeedFrame.run(LightSpeedFrame.java:187)
at java.lang.Thread.run(Unknown Source)
</code></pre>

<p>End Edit.</p>

<p>I suspect some kind of thread problem, but that's why I told it to change between fullscreen and windowed mode inside the thread, so it wouldn't try to access anything while messing around with displays.</p>

<p>Relevent code:
Extract from the JFrame constructor:</p>

<pre><code>initWindow();
if(fullScreen){
    initFullscreen();
}
//Setting graphics buffer strategy
createBufferStrategy(2);//double-buffered
bufferStrategy = getBufferStrategy();
</code></pre>

<p>Entirety of screenUpdate() (in the update-draw loop):</p>

<pre><code>try{
        graphics = bufferStrategy.getDrawGraphics();
        gameRender(graphics);
        graphics.dispose();
        if(!bufferStrategy.contentsLost()){
            bufferStrategy.show();
        }else{
            System.out.println("Contents Lost");
        }

        //Sync the display on some systems. (I'm told that on Linux, this fixes event queue problems)
        Toolkit.getDefaultToolkit().sync();
    }catch (Exception e){
        e.printStackTrace();  
        dontExit = false;
    }
</code></pre>

<p>gameRender() just draws a random ellipse.</p>

<p>Also:</p>

<pre><code>private void initFullscreen(){
    if (!gDevice.isFullScreenSupported()){
        System.out.println("Full-screen exclusive mode not supported");
    }else{
        gDevice.setFullScreenWindow(this);

        validate();

        setDisplayMode(resolutionX, resolutionY, 32);//TODO: set to current resolution instead

        fullScreen = true;
    }
}

private void disableFullscreen(){
    gDevice.setFullScreenWindow(null);

    fullScreen = false;
}
</code></pre>

<p>Edit: So, I guess my question comes down to; Why is BufferStrategy changing state, what is it changing to, and how can I prevent it? This bug is most peculiar if it really does only show up the first time it runs after startup, and a few frames after changing to fullscreen.</p>

<p>Edit2: Here's the compilable and runnable source: [Zip file]<a href="http://www.albeyamakiir.hostzi.com/web_documents/LighSpeedFrame.zip" rel="nofollow">http://www.albeyamakiir.hostzi.com/web_documents/LighSpeedFrame.zip</a></p>
