"Daemon Thread Image Animator" not finishing

273 Views Asked by At

I'm doing a game and when a ship is dead, I make a Thread that shows a GIF explosion. The problem is that sometimes that explosion doesn't disappear, and stays on the screen until the game is over.

Debugging, I saw "Daemon Thread Image Animator -0" running and I guess that the GIF. This is the code of the explosion:

public void run(){
    setIcon(new ImageIcon(getClass().getClassLoader().getResource("Imag/explosion.gif")));
    Point pos = this.getLocation();
    setBounds(pos.x,pos.y);
    try{
        Thread.sleep(500);
    }
    catch(Exception e){
        e.printStackTrace();
    }
    juego.removeEntidad(this);

}

Sometimes this problem doesn't happen and I don't know how to fix it.

1

There are 1 best solutions below

2
On

I'm doing a game and when a ship is dead, I make a Thread that shows a GIF explosion.

Yes, you need to use a Thread, but you should NOT be updating GUI components in the Thread.

Instead you should probably be using a SwingWorker. Read the section from the Swing tutorial on Concurrency in Swing for more information and examples of using a SwingWorker.