我正在尝试制作一个游戏,因此我开始创建所有绘图方法和方式,这样我就可以开始图形工作,但是当我创建 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;
}
}
我做错了什么?
检查了你的代码,我建议你给每个类起一个好名字。
不良风格的命名,例如 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;
}
我是一名优秀的程序员,十分优秀!