1

I'm building a memory match game and in the ActionListener I want to implement an animated gif before uncovering a block, but the gif never shows, I assume it resets; how do i fix this?

for(int i = 0; i< buttonNum; i++)
{
    if(e.getSource() == button[i])
    {           
        try
        {
            URL blockIcon = this.getClass().getClassLoader().getResource(block[i].toString());
            //sets it to the gif
            button[i].setIcon(uncoverBlock);
            Thread.sleep(5000);
            button[i].setIcon(new ImageIcon(blockIcon));
            System.out.println("Block: " + block[i]);
            currentIndex = i;
        }
        catch(InterruptedException ie)
        {
            ie.printStackTrace();
        }
    }
}
Devid Farinelli
  • 7,514
  • 9
  • 42
  • 73
Toni Golac
  • 45
  • 4
  • 1
    The fact that you have a Thread.sleep in here is a red flag. You should never use sleep when rendering on the UI thread. Instead, you should use a Swing Timer. – ControlAltDel Apr 03 '17 at 13:42
  • 1
    https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html – ControlAltDel Apr 03 '17 at 13:43
  • see also http://stackoverflow.com/questions/16630686/jtextarea-not-updating-dynamically/16630877#16630877 and http://stackoverflow.com/questions/14074329/using-sleep-for-a-single-thread – user85421 Apr 03 '17 at 14:58

0 Answers0