javajava-canvas

Java bufferstrategy, graphics error: : Buffers have not been created


So I have been making a small game lately and I have a weird problem. Altough the game runs just perfectly when I don't use game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); but the stop() method to close it I get an error and I don't really understand why.

The error:

Exception in thread "thread" Program closed, processes halted
java.lang.IllegalStateException: Buffers have not been created
at sun.awt.windows.WComponentPeer.getBackBuffer(WComponentPeer.java:1018)
at java.awt.Component$FlipBufferStrategy.getBackBuffer(Component.java:4065)
at 
java.awt.Component$FlipBufferStrategy.updateInternalBuffers(Component.java:4
050)
at java.awt.Component$FlipBufferStrategy.revalidate(Component.java:4165)
at java.awt.Component$FlipBufferStrategy.revalidate(Component.java:4147)
at java.awt.Component$FlipBufferStrategy.getDrawGraphics(Component.java:4139)
at com.bacskai.peashooter.Game.render(Game.java:105)
at com.bacskai.peashooter.Game.run(Game.java:75)
at java.lang.Thread.run(Thread.java:748)

Please give solutions that are not too complicated because I'm still a beginner and the game is still work in progress

The full code:

package com.bacskai.peashooter;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;

public class Game extends Canvas implements Runnable, KeyListener {

private static final long serialVersionUID = -4227990863874935837L;

JFrame frame;
static Dimension d;
Thread thread;


public static int width = 300;
public static int height = width / 16 * 9;
int scale = 3;

boolean running;

int[][] track = new int[5][900];

int playerY = 3;
int health = 3;
int score = 0;

public Game() {

    d = new Dimension(width * scale, height * scale);
    setPreferredSize(d);
    frame = new JFrame();

}

public static void main(String[] args) {

    Game game = new Game();

    game.frame.setResizable(false);
    game.frame.setTitle("Peasooter");
    game.frame.add(game);
    game.frame.pack();
    game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.frame.setLocationRelativeTo(null);
    game.frame.setVisible(true);
    game.frame.addKeyListener(game);

    game.start();

}

private void start() {

    System.out.println("Program started");
    thread = new Thread(this, "thread");
    running = true;
    thread.start();

    System.out.println("Window width: " + getWidth() + ", height: " + 
 getHeight());

}

public void run() {

    while (running) {

        update();
        render();

    }

}

private void stop() {

    try {
        frame.dispose();
        thread.join();

    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("Program closed, processes halted");
}

public void update() {



}

private void render() {

BufferStrategy bs = getBufferStrategy();

if (bs == null) {createBufferStrategy(3); return;}

Graphics g = bs.getDrawGraphics();

// Map

g.setColor(Color.black);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.cyan);
g.fillRect(100, 0, 10, 490);
g.fillRect(0, 98, 900, 10);
g.fillRect(0, 196, 900, 10);
g.fillRect(0, 294, 900, 10);
g.fillRect(0, 392, 900, 10);

g.setColor(Color.red);
Font font = new Font("Default", Font.PLAIN, 50);
g.setFont(font);
g.drawString("Score: " + score, 700, 50);

// Player

g.setColor(Color.green);

// Enemies

g.setColor(Color.red);

// Projectiles
g.setColor(Color.green);



bs.show();
g.dispose();    
} // End of render


public void keyTyped(KeyEvent e) {}

public void keyPressed(KeyEvent e) {

    if(e.getKeyCode() == KeyEvent.VK_ESCAPE) {
        stop();
    }

}

public void keyReleased(KeyEvent e) {}

}

Solution

  • That is expected behaviour when using thread.stop();

    From an old bug report we find that it is not a problem:

    Note also, that this problem is hard to reproduce, and has no consequences other than a stack trace dump in a console (no hang, no visual artifacts were reported). Because of this, I am decreasing the priority for this CR.

    So if you want to keep it simple then alter your code block to catch and ignore the error:

    private void stop() {
        try {
            frame.dispose();
            thread.join();
        } catch (IllegalStateException e) {
            //Do nothing
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("Program closed, processes halted");
    }
    

    Otherwise, if you prefer to use a non-code solution then you can add this -Dsun.java2d.d3d=false to your command as a parameter when you launch your application like so:

    java -jar path_to/jar_file/myjar.jar -Dsun.java2d.d3d=false