我正在使用缓冲区策略在 java 中创建一个应用程序。唯一的问题是,当我在集成显卡上使用最大化的缓冲区策略时,它会因以下错误而崩溃:
Exception in thread "Thread-2" java.lang.IllegalStateException: Buffers have not been created
我相信它崩溃的原因是我在多显示器上使用 AMD 显卡和集成显卡,或者换句话说,两台显示器正在使用 AMD 显卡的显卡,而一台显示器正在使用集成显卡。
结果,我无法在 igpu 运行的监视器上最大化我的应用程序,但可以在由显卡操作的监视器上最大化我的窗口。
你有解释为什么Java给我废话吗?另外,有没有解决方案,所以我也可以在我的 igpu 运行监视器上最大化我的窗口?
这是创建和使用缓冲区的方式:
旁注,每当使用 Graphics 对象时,我都会使用g.drawImage(BufferedImage, x, y, null);
private void render() {
BufferStrategy bs = this.getBufferStrategy();
if(bs == null) {
this.createBufferStrategy(3); //creation
//this being a class that implements Runnable
return;
}
Graphics g = bs.getDrawGraphics(); //bufferstrategy is converted to graphics here
g.setColor(Color.black);
g.fillRect(0, 0, mutableWidth, mutableHeight);
handler.render(g); //Graphics is transported to hundreds of objects to get displayed
g.dispose();
bs.show();
}
我相信您不需要阅读实现可运行的整个类,但以防万一:
package GenRuntime;
import GenRuntime.GameObjects.Block.Block;
import GenRuntime.Handler.Handler;
import UI.UIInput.KeyInput;
import Resources.Initializer;
import java.awt.*;
import java.awt.image.BufferStrategy;
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = -240840600533728354L;
public static final int WIDTH = 800, HEIGHT = WIDTH /12 * 9;
private int mutableWidth, mutableHeight;
private int windowXLoc, windowYLoc;
private Thread thread;
private boolean running = false;
private Handler handler;
Window window;
public Game() {
handler = new Handler();
this.addKeyListener((new KeyInput(handler)));
window = new Window(WIDTH, HEIGHT, "Survior", this);
mutableHeight = HEIGHT;
mutableWidth = WIDTH;
}
public synchronized void start() {
thread = new Thread(this);
thread.start();
running = true;
}
public synchronized void stop() {
try {
thread.join();
running = false;
} catch(Exception e) {
e.printStackTrace();
}
}
public int Frames;
public void run() {
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 0;
while (running) {
long now = System.nanoTime();
delta += (now-lastTime) / ns;
lastTime = now;
while (delta >= 1) {
tick();
delta--;
}
if (running)
render();
frames++;
if(System.currentTimeMillis() - timer > 1000) {
timer += 1000;
Frames = frames;
System.out.println("FPS: " + frames);
frames = 0;
}
}
}
private void tick() {
handler.tick();
}
private void render() {
BufferStrategy bs = this.getBufferStrategy();
if(bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.black);
g.fillRect(0, 0, mutableWidth, mutableHeight);
handler.render(g);
g.dispose();
bs.show();
}
public static void main(String[] args) {
Initializer.init();
Block b = new Block();
new Game();
}
//The rest of the code is getter and setter methods. Don't worry about them
public void setSize(int width, int height) {
mutableHeight = height;
mutableWidth = width;
}
public void setSize(Dimension dim) {
mutableWidth = dim.width;
mutableHeight = dim.height;
}
public void setWindowLoc(int x, int y) {
windowXLoc = x;
windowYLoc = y;
}
public void setWindowLoc(Point p) {
windowXLoc = p.x;
windowYLoc = p.y;
}
public int getMutableWidth() {
return mutableWidth;
}
public int getMutableHeight() {
return mutableHeight;
}
public int getWindowXLoc() {
return windowXLoc;
}
public int getWindowYLoc() {
return windowYLoc;
}
}