我正在尝试创建一个游戏,所以我开始创建所有绘图方法和方法,所以我可以从图形工作开始,但是当我创建我的bufferyStrategy时,java会返回错误。
为什么会发生错误?
Exception in thread "Thread-0" java.lang.IllegalStateException: Component must have a valid peer
at java.awt.Component$FlipBufferStrategy.createBuffers(Unknown Source)
at java.awt.Component$FlipBufferStrategy.<init>(Unknown Source)
at java.awt.Component$FlipSubRegionBufferStrategy.<init>(Unknown Source)
at java.awt.Component.createBufferStrategy(Unknown Source)
at java.awt.Canvas.createBufferStrategy(Unknown Source)
at java.awt.Component.createBufferStrategy(Unknown Source)
at java.awt.Canvas.createBufferStrategy(Unknown Source)
at Game.render(Game.java:28)
at Game.run(Game.java:17)
at Game$1.run(Game.java:45)
at java.lang.Thread.run(Unknown Source)
这是来源:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
public class Game extends Frame {
public boolean playing = false;
public Game() {
}
public void run() {
while (playing) {
update();
render();
}
}
public void update() {
}
public void render() {
BufferStrategy b = null;
if (super.getBufferStrategy() == null) {
super.createBufferStrategy(3);
}
b = super.getBufferStrategy();
Graphics2D g = (Graphics2D) b.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
g.dispose();
b.show();
}
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
Game game = new Game();
game.playing = true;
game.run();
}
}).start();
}
}
和父类:
import java.awt.Canvas;
import java.awt.Dimension;
import javax.swing.JFrame;
public class Frame extends Canvas {
private JFrame frame = new JFrame();
public Frame() {
frame.setTitle("Finland2D");
frame.setPreferredSize(new Dimension(765, 500));
frame.pack();
super.setVisible(true);
frame.setVisible(true);
}
public JFrame getFrame() {
return this.frame;
}
}
我做错了什么?
答案 0 :(得分:0)
检查了你的代码,我建议你给每个班级一个好名字。
错误的命名方式,例如Frame extends Canvas,会弄得一团糟。
如果您使用Swing,那么内置缓冲策略,就像双缓冲策略一样。
请提供更多信息。
目前,我重构了您的代码,并制作了如下示例:
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
/**
* this code still Need to improve
* deal with Thread related code
*
*/
public class Game extends Canvas implements Runnable{
public static void main(String[] args) {
//GUI EDT thread
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
JFrame frame = new JFrame("Finland2D");
frame.setPreferredSize(new Dimension(765, 500));
final Game game = new Game();
game.playing = true;
frame.add(game);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
game.finish();
System.exit(0);
}
});
//start game thread
new Thread(game).start();
}
});
}
public Game() {
}
@Override
public void run() {
while (playing) {
update();
render();
}
}
public void finish() {
playing = false;
}
public void update() {
}
public void render() {
BufferStrategy b = null;
if (super.getBufferStrategy() == null) {
super.createBufferStrategy(3);
}
b = super.getBufferStrategy();
Graphics2D g = (Graphics2D) b.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
g.dispose();
b.show();
}
private static final long serialVersionUID = 1L;
public boolean playing = false;
}
答案 1 :(得分:0)
在将Canvas(Frame)添加到对等组件之前,似乎已调用render()
。
尝试将“Frame”实例显式添加到Frame构造函数中的容器JFrame中:
private JFrame frame = new JFrame();
public Frame() {
frame.setTitle("Finland2D");
frame.setPreferredSize(new Dimension(765, 500));
frame.pack();
super.setVisible(true);
frame.setVisible(true);
//Explicitly add this to the container
frame.add(this);
}
我尝试用它执行它,我没有异常和黑屏。