Problem with Thread.sleep

I'm making a slot machine game, and I want it so when the user presses the pull button, the 3 slot areas will all display a picture (gif) for about 3 seconds, and then display 3 random pictures in the 3 slots after the 3 seconds.

This is what I have so far but it doesn't work. When I press the button, the gif picture doesn't display, it just pauses for 3 seconds. I'm assuming i'm using the wrong method for this.

publicvoid actionPerformed(ActionEvent e)

{

if (e.getSource() == pull)

{

slot1.setIcon(all);

slot2.setIcon(all);

slot3.setIcon(all);

try{

Thread.sleep( 3000 );

}

catch ( InterruptedException ie ){

return ;

}

newPic(slot1, slot2, slot3);

}

}

[1179 byte] By [Archeea] at [2007-11-26 23:00:01]
# 1

Never sleep in the Event Dispath Thread.

Your GUI will freeze and will not update until you exit the function.

Use a timer instead:

http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html

Read the article about threads and swing:

http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html

Rodney_McKaya at 2007-7-10 13:05:51 > top of Java-index,Desktop,Core GUI APIs...
# 2
I can't really find a way to implement that into my code. I tried it, but now it doesn't even wait 3 seconds, it just displays 3 new pictures in the slots.Any help?
Archeea at 2007-7-10 13:05:51 > top of Java-index,Desktop,Core GUI APIs...
# 3

public void actionPerformed(ActionEvent e) {

if (e.getSource() == pull) {

slot1.setIcon(all);

slot2.setIcon(all);

slot3.setIcon(all);

int delay = 3000; //milliseconds

ActionListener taskPerformer = new ActionListener() {

public void actionPerformed(ActionEvent evt) {

newPic(slot1, slot2, slot3);

}

};

Timer timer = new Timer(delay, taskPerformer);

timer.setRepeats(false);

timer.start();

}

}

Rodney_McKaya at 2007-7-10 13:05:51 > top of Java-index,Desktop,Core GUI APIs...
# 4
That works, thanks Rodney =)
Archeea at 2007-7-10 13:05:51 > top of Java-index,Desktop,Core GUI APIs...