Problems with thread and bufferstrategy: IllegalStateException: Component must have a valid peer

242 views Asked by At

Here is the code that I tried to execute, but failed:

public class Game extends Canvas implements Runnable,KeyListener {

    private static final long serialVersionUID = 1L;
    private static Thread thread ;
    private int width = 240;
    private int height = 120;
    private int scale = 3;
    private boolean isRunning = true;
    
    private BufferedImage image;
    public static Spritesheet spritesheet;
    
    private Player player;
    
    Game(){
         Dimension dimension = new Dimension(width*scale,height*scale);
         this.setPreferredSize(dimension);
         
         image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        
    }
    
    void initframe() {
        
        JFrame f = new JFrame("Game");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setVisible(true);
        f.setLocationRelativeTo(null);
        f.setResizable(false);
    }
    
    public synchronized void start() {
            
        thread = new Thread(this);
        isRunning = true;
        thread.start();
    }
    
    public synchronized void stop() {
        
        isRunning = false;
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
    }
    public static void main(String[] args) {
    
        Game game = new Game();
        game.start();
        
        
    }
    
    void tick() {
        
    }
    
    void render() {
        
        BufferStrategy bs = this.getBufferStrategy();
        if(bs == null) {
            this.createBufferStrategy(3);
            return;
        }
        Graphics g = image.getGraphics();
        g.setColor(Color.GREEN);
        g.fillRect(0, 0, width, height);
        
        
        g.dispose();
        g = bs.getDrawGraphics();
        g.drawImage(image, 0, 0, width*scale,height*scale,null);
        bs.show();
        
        
    }

    public void run() {
        

     long lastTime = System.nanoTime();
     double amountOfTicks = 60.0;
     double ns = (10^9)/amountOfTicks ;
     int frames = 0;
     double delta;
     double timer = System.currentTimeMillis();
     
     while(isRunning) {
         long now = System.nanoTime();
         delta = (now - lastTime)/ns ;
         lastTime = now;
         if(delta >= 1) {
             tick();
             render();
             frames++;
             delta--;
         }
    
         if(System.currentTimeMillis() - timer >= 1000) {
             System.out.println(" A taxa de fps e:   "+frames);
             frames = 0;
             timer+=1000;
         }
         
     }
            
        
        stop();
    }

And the error that appears to me is:

Exception in thread "Thread-0" java.lang.IllegalStateException: Component must have a valid peer
    at java.desktop/java.awt.Component$FlipBufferStrategy.createBuffers(Component.java:4105)
    at java.desktop/java.awt.Component$FlipBufferStrategy.<init>(Component.java:4079)
    at java.desktop/java.awt.Component$FlipSubRegionBufferStrategy.<init>(Component.java:4611)
    at java.desktop/java.awt.Component.createBufferStrategy(Component.java:3942)
    at java.desktop/java.awt.Canvas.createBufferStrategy(Canvas.java:195)
    at java.desktop/java.awt.Component.createBufferStrategy(Component.java:3866)
    at java.desktop/java.awt.Canvas.createBufferStrategy(Canvas.java:170)
    at com.main.Game.render(Game.java:83)
    at com.main.Game.run(Game.java:115)
    at java.base/java.lang.Thread.run(Thread.java:834)

I checked out every line of this code and I don't know why show that the problem is with thread or bufferStrategy, the method render or method run that I used.

0

There are 0 answers