0

I have an application that is using a BufferStrategy created by a JFrame object to paint shapes. The code is pretty standard, what you would find elsewhere:

// JFrame class
frame.setVisible( true );
frame.createBufferStrategy( 2 );
new Logic( frame.getBufferStrategy() ).loop();

// Logic class
public Logic( BufferStrategy bs ){
        this.bs = bs;
}

public void loop(){
    // looping should be in another thread
    Thread loop = new Thread(new Runnable() {
        public void run(){
            looooooooop();
        }
    });
    loop.start();
}

Now the looooooooop function is where the drawing is actually happening:

g = (Graphics2D)bs.getDrawGraphics();

// draw stuff here, leaving out for simplicity

g.dispose();
bs.show();
Toolkit.getDefaultToolkit().sync();

Now the issue is about 75% of the time the application boots up and runs great, the other 25% of the time the frame appears but only the background colour of the frame can be seen, it is like the graphics objects isn't drawing anything on the screen. I have tried debugging and figuring out were this issue is stemming but it is really hard to do so. I am thinking it has something to do with the logic loop running in another thread (maybe this explains the inconsistent nature of the application) but haven't had any luck. Does this issue sound familiar to anyone? Thanks!

Edit - After removing the separate thread this is still happening so it can't be because of that.

Edit 2 - It seems like this is only happening when full screen mode is enabled (with or without the call to setVisible()):

this.setVisible( true );

if(isFullScreen){
    GraphicsEnvironment
        .getLocalGraphicsEnvironment()
        .getDefaultScreenDevice()
        .setFullScreenWindow(this);     
}

Is this a known issue or am I missing something? Thanks!

Edit 3 - This is only happening on Ubuntu - verified through brute force. Can't find anything online regarding a bug so I am not sure what to think.

Sloth Armstrong
  • 1,066
  • 2
  • 19
  • 39
  • 1
    Have you tried looping on the content state as Oracle demonstrated here: [java.awt.image.BufferStrategy](http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferStrategy.html)? – Obicere Dec 28 '14 at 23:54
  • @Obicere I hate to say this didn't solve my issue but my application runs WAY smoother now. Thank you! – Sloth Armstrong Dec 29 '14 at 15:15

1 Answers1

0

If you think creating a thread is necessary (I don't think so), I would do this instead:

javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
      looooooooop();
    }
});

My recommendation is to try it without creating the new thread.

UPDATE:

Try these events in this order:

// Create a canvas object (where you are actually doing the drawing)

frame.add(canvas);
frame.setVisible(true);

canvas.createBufferStrategy(2);
canvas.requestFocus();

// Do your drawing now.

You can't create the buffer before it's displayed. Also, if you are calling pack() on the frame anywhere on your code, just remove it.

hfontanez
  • 5,774
  • 2
  • 25
  • 37
  • Thanks for the advice. I actually just tried it with both the `invokeLater` method and completely removing the new thread all together and this is still happening. – Sloth Armstrong Dec 28 '14 at 16:51
  • It seems like your update didn't work either. I am now just confused about what could be causing this. – Sloth Armstrong Dec 28 '14 at 20:38