estou com problemas no meu código e não sei como resolver o erro é esse:
Exception in thread “Thread-0” java.lang.IllegalStateException: Component must have a valid peer
at java.desktop/java.awt.Component$FlipBufferStrategy.createBuffers(Component.java:4106)
at java.desktop/java.awt.Component$FlipBufferStrategy.(Component.java:4080)
at java.desktop/java.awt.Component$FlipSubRegionBufferStrategy.(Component.java:4612)
at java.desktop/java.awt.Component.createBufferStrategy(Component.java:3943)
at java.desktop/java.awt.Canvas.createBufferStrategy(Canvas.java:195)
at java.desktop/java.awt.Component.createBufferStrategy(Component.java:3867)
at java.desktop/java.awt.Canvas.createBufferStrategy(Canvas.java:170)
at module.Game.render(Game.java:37)
at module.Game.run(Game.java:72)
at java.base/java.lang.Thread.run(Thread.java:832)
package module;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable
{
private static final long serialVersionUID = 1L;
private boolean isrunning;
private Thread thread;
public JFrame frame;
private int Width = 160;
private int Height = 120;
private int Scale = 3;
private BufferedImage imagem;
public Game() {
frame = new JFrame("Kypon's Club");
tela();
}
public void tela() {
frame.setPreferredSize(new Dimension(Width*Scale,Height*Scale));
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public void tick() {
}
public void render() {
BufferStrategy bs = this.getBufferStrategy();
if(bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = imagem.getGraphics();
g.setColor(new Color(12,12,12));
g.fillRect(0,0, Width*Scale, Height*Scale);
g = bs.getDrawGraphics();
g.drawImage(imagem, 0, 0, Width*Scale, Height*Scale, null);
bs.show();
}
public synchronized void start() {
thread = new Thread(this);
isrunning = true;
thread.start();
}
public synchronized void stop() {
}
public static void main(String[] args) {
Game game = new Game();
game.start();
}
public void run() {
Long lastTime =System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000/amountOfTicks;
double delta = 0;
int frames = 0;
double timer = System.currentTimeMillis();
while(isrunning) {
long now = System.nanoTime();
delta += (now-lastTime)/ns;
lastTime = now;
if(delta >= 1) {
tick();
render();
delta--;
frames++;
} if(System.currentTimeMillis() - timer >= 1000) {
frames = 0;
timer+= 1000;
}}}}