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

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

所以我最近一直在做一个小游戏,但遇到了一个奇怪的问题。虽然游戏在我不使用时运行得非常完美 game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);但是关闭它的 stop() 方法我得到了一个错误,我真的不明白为什么。

错误:

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)

请给出不太复杂的解决方案,因为我还是个初学者,游戏还在开发中

完整代码:

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) {}

}

这是使用 thread.stop();

时的预期行为

从旧的bug report我们发现这不是问题:

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.

因此,如果您想保持简单,请更改您的代码块以捕获并忽略错误:

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");
}

否则,如果您更喜欢使用非代码解决方案,那么您可以在启动应用程序时将此 -Dsun.java2d.d3d=false 作为参数添加到您的命令中,如下所示:

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