0

I created a game in Java which uses buffer strategy. But sometimes I just get a white frame or a white border. That happens about every fifth time. I searched a lot but I really can't figure out what I'm doing wrong. The code compiles and there aren't any errors printed out.

If if fails, the frame looks for example like that:

white border

completely white frame

Here's code (only the relevant parts):

private BufferStrategy bs;
public Testclass(){
    setResizable(false);
    setSize(1000,600);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setIgnoreRepaint(true);
    setVisible(true);
    createBufferStrategy(2);
    bs = getBufferStrategy();
}

protected void paint() {
    do {
        Graphics2D g=null;
        try {
            g = (Graphics2D) bs.getDrawGraphics();
            g.setColor(Color.CYAN);
            g.fillRect(0, 0, this.getWidth(), this.getHeight());
        } finally {
            g.dispose();
        }
        bs.show();
    } while (bs.contentsLost());
}

public static void main(String[] args) {
    Testclass window = new Testclass();
    window.paint();
}
tobxd
  • 1
  • 1
  • Are you sure there's nothing else going on outside of the code you provided? I dumped it into my sandbox, and I can't get it to reproduce the problem. I always get a window displaying only blue. No white border or anything. – Frank Jan 27 '16 at 14:28
  • I have just this class. I tested it with Eclipse an I manually compiled it and in both cases it fails sometimes. My operating system is Ubuntu, but I think think that shouldn't matter. – tobxd Jan 27 '16 at 14:39
  • The operating system always matters when using Java Swing. Swing behavior is different on different operating systems and different Java versions. – Gilbert Le Blanc Jan 27 '16 at 14:43
  • I'm on Windows, using Intellij. There could be configuration differences :( – Frank Jan 27 '16 at 14:47
  • I just tried it on a Windows 10 an it works there. So probably this problem is operating system specific. But I think it's strange that the program works sometimes on Linux and sometimes not. – tobxd Jan 27 '16 at 15:10

1 Answers1

0

I actually found a better way try this:

   boolean BufferStrategyRunning = true;

    while(BufferStrategyRunning == true) {

        BufferStrategy BS = GUIFrame.getBufferStrategy();

        if(BS == null) {
        GUIFrame.createBufferStrategy(3);
        continue;
        }

        Graphics g = BS.getDrawGraphics();

        g.drawImage(CurrentScreen.BackgroundImage, 0, 0, null);
        g.dispose();
        BS.show();


    }
  • This allows it to have enough time to create the buffer strategy if you look inside the java code there is a sepreate thread created to make the bufferstrategy that will run slower then your application code that is why sometimes it works and sometimes it does not. – VxwkillerjTheWalrus Mar 23 '18 at 15:16
  • That is why other people are not having the same issue because they have a beast of computer that can calculate the cure for cancer are computers can hardly calculate the radius of a potato. – VxwkillerjTheWalrus Mar 23 '18 at 15:18