I am trying to make a game so I started creating all the drawing methods and ways, so I can begin with the graphics work, but when I am creating my bufferyStrategy, java returns an error.
Why is that error happening?
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)
This is the 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();
}
}
and the parent class:
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;
}
}
What am I doing wrong?
Reviewed your code,I suggest you to give every class a good name.
Bad-style naming ,such as Frame extends Canvas ,will make a mess.
And If you use Swing,there built-in buffered-Strategy for you,like double-buffered strategy.
please give more information.
currently,I refactored your code ,and make a sample as follow: