Java bufferstrategy,图形错误: : Buffers have not been created

标签 java java-canvas

所以我最近一直在做一个小游戏,但遇到了一个奇怪的问题。虽然游戏在我不使用时运行得非常完美 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

关于Java bufferstrategy,图形错误: : Buffers have not been created,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50916515/

相关文章:

java - 替换方法不适用于全局修饰符

Java Oracle 插入查询

java - 将大型 WAR 文件上传到 Cloud Foundry 失败

java - java android 中的 XOR 掩码

JavaFX和Canvas的快速更新

java - 如何设计删除时的多对多关系?

Java 8 混淆 -> String::compareToIgnoreCase

java - 从主类加载 Canvas - Java

java - 如何判断Point是否在对角线上?

java - 倒坐标系中直线和矩形的直线裁剪算法?