为什么这不能创建缓冲策略?
Why could this not create a buffer strategy?
我收到一个 Exception in thread "Thread-3" java.lang.InternalError: Could not create a buffer strategy
错误,该错误可以追溯到 Java 的基本代码,它通过游戏 class 下面的第 58 行( render();
) 和 77 ( this.createBufferStrategy(5);
)。
任何帮助都会很棒,如果您需要查看其他 classes,我还将我的所有代码上传到 github (mitisme/mit)。
import java.awt.Canvas;
import java.awt.Color;
import java.awt.*;
import java.awt.image.BufferStrategy;
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
private boolean isRunning = false;
private Thread thread;
private Handler handler;
//Creates background window size and holds objects by handler
public Game() {
Screen myScreen = new Screen(1280, 720, "Deed", this);
start();
handler = new Handler();
addKeyListener(new KeyInput(handler));
handler.addObject(new Player(425, 745, ID.Player, handler));
}
//starts a new thread
private void start() {
isRunning = true;
thread = new Thread(this);
thread.start();
}
// Stops current thread, and catches exceptions
private void stop() {
isRunning = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//Infinite game loop
public void run() {
this.requestFocus();
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 0;
while(isRunning) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while(delta >= 1) {
tick();
delta--;
}
render();
frames++;
if(System.currentTimeMillis() - timer > 1000) {
timer += 1000;
frames = 0;
}
}
stop();
}
public void tick() {
handler.tick();
}
//Holds extra frames before showing (3 extra)
public void render() {
BufferStrategy bs = this.getBufferStrategy();
if(bs == null) {
this.createBufferStrategy(5);
return;
}
Graphics g = bs.getDrawGraphics();
/////////////////Renders background first, then handlers///////////////////
g.setColor(Color.black);
g.fillRect(0,0,1280,720);
handler.render(g);
/////////////////Updates graphics////////////////////
g.dispose();
bs.show();
}
public static void main(String args[]) {
new Game();
}
}
我想通了,this.createBufferStrategy(5);
行只能从 2-4 创建缓冲策略,否则会报错。
我收到一个 Exception in thread "Thread-3" java.lang.InternalError: Could not create a buffer strategy
错误,该错误可以追溯到 Java 的基本代码,它通过游戏 class 下面的第 58 行( render();
) 和 77 ( this.createBufferStrategy(5);
)。
任何帮助都会很棒,如果您需要查看其他 classes,我还将我的所有代码上传到 github (mitisme/mit)。
import java.awt.Canvas;
import java.awt.Color;
import java.awt.*;
import java.awt.image.BufferStrategy;
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
private boolean isRunning = false;
private Thread thread;
private Handler handler;
//Creates background window size and holds objects by handler
public Game() {
Screen myScreen = new Screen(1280, 720, "Deed", this);
start();
handler = new Handler();
addKeyListener(new KeyInput(handler));
handler.addObject(new Player(425, 745, ID.Player, handler));
}
//starts a new thread
private void start() {
isRunning = true;
thread = new Thread(this);
thread.start();
}
// Stops current thread, and catches exceptions
private void stop() {
isRunning = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//Infinite game loop
public void run() {
this.requestFocus();
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 0;
while(isRunning) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while(delta >= 1) {
tick();
delta--;
}
render();
frames++;
if(System.currentTimeMillis() - timer > 1000) {
timer += 1000;
frames = 0;
}
}
stop();
}
public void tick() {
handler.tick();
}
//Holds extra frames before showing (3 extra)
public void render() {
BufferStrategy bs = this.getBufferStrategy();
if(bs == null) {
this.createBufferStrategy(5);
return;
}
Graphics g = bs.getDrawGraphics();
/////////////////Renders background first, then handlers///////////////////
g.setColor(Color.black);
g.fillRect(0,0,1280,720);
handler.render(g);
/////////////////Updates graphics////////////////////
g.dispose();
bs.show();
}
public static void main(String args[]) {
new Game();
}
}
我想通了,this.createBufferStrategy(5);
行只能从 2-4 创建缓冲策略,否则会报错。