第一个代码片段是我的主要渲染方法。我正在创建缓冲区策略,然后 getState().render(g) 传递 Graphics 对象并调用一堆 g.drawImage() 方法,毕竟,调用 saveImage() 并生成完全黑色的图像(游戏截图)预期的)。第二个片段是将画布保存到图像。
private void render() {
bs = display.getCanvas().getBufferStrategy();
if (bs == null) {
display.getCanvas().createBufferStrategy(3);
return;
}
g = bs.getDrawGraphics();
g.clearRect(0, 0, width, height);
if (State.getState() != null)
State.getState().render(g); // this just calls g.drawImage many times
saveImage(g);
bs.show();
g.dispose();
}
这是我保存它的方式:
private void saveImage(Graphics g) {
BufferedImage image = new BufferedImage(getCanvas().getWidth(), getCanvas().getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics();
display.getCanvas().paint(graphics);
graphics.dispose();
try {
FileOutputStream out = new FileOutputStream("snapshot.png");
ImageIO.write(image, "png", out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
德玛西亚99
相关分类