Problems with the Bufferstrategy

Asked

Viewed 31 times

1

I am recreating a code from scratch, but when creating a Bufferstrategy to draw on the screen I come across the following error in the console "Exception in thread "Thread-0" java.lang.Illegalstateexception: Component must have a Valid peer" and my ide says that the problem is on the line where I create Bufferstrategy within the check. (I am relatively new to java and this is my first programming language). Code below:


import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;

import com.flstudios.graficos.Janela;

public class Jogo extends Canvas implements Runnable {
    
    boolean estaRodando = false;
    
    Janela janela;
    Thread thread;
    
    
    public Jogo() {
        janela = new Janela(800,600);
        iniciar();
    }
    
    public synchronized void iniciar() {
        thread = new Thread(this);
        thread.start();
        estaRodando = true;
    }
    
    public synchronized void parar() {
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    
    public void atualizar() {
        
        
        
        
        
        
        //System.out.println("Atualizando");
    }
    public void renderizar() {
        BufferStrategy bs = this.getBufferStrategy();
        if(bs == null) {
            this.createBufferStrategy(3);
            return;
        }
        Graphics g = bs.getDrawGraphics();
        
        g.setColor(Color.BLUE);
        g.fillRect(0, 0, 15, 15);
        bs.show();
        
        
        
        
        
        //System.out.println("Renderizando");
    }
    
    public void run() {
    while(estaRodando == true) {
        try {
            thread.sleep(1000/60);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        atualizar();
        renderizar();
        
        
        //System.out.println("Looping funciona!");
    }
    }
}

ps: I redid the entire code in another project and only in one class (main) and although I kept the same code (only in English) it worked perfectly. Would the solution be not to divide the project into classes? Or maybe to do everything in English?

  • Just an aside. Your first programming language, and it’s object-oriented, and you’re threading? Each one knows what motivates you to program, but I find concepts that are kind of advanced to mess with a first language. Just an opinion.

1 answer

1

From what I’ve researched the problem, you should only mess with BufferStrategy after the JFrame ready and displayed.

So in case I believe you missed the call janela.setVisible(true) after the line with the new Janela() and before the iniciar().

Source: https://stackoverflow.com/q/37304356/2241463

How I found it: googlando by Exception text

Browser other questions tagged

You are not signed in. Login or sign up in order to post.