Thread.sleep() behaves weird in Listeners

Hey,

I am a rookie in Swing and I am writing a small game. The game is a two player game. I am now trying to add AI to the game so when the human player clicks on the board he plays and then the computer plays its turn.

So in pseudo code:

//in action listener

human.play

robot.proceed

sleep(3000)

reprint the board

robot.proceed

sleep(3000)

reprint the board

robot.proceed

sleep(3000)

reprint the board

where proceed is a method that makes the robot play on three stages...

So basically I am trying to simulate the actual way a human plays...

The problem is that when the human player clicks the game hangs for all 9 seconds then suddenly the board is reprinted with both moves...

How can I overcome this problem?

Thank you

[838 byte] By [Abdallaha] at [2007-11-27 3:41:44]
# 1

first of all, take the time to read the documentation, at least the basics.

secondly, never sleep in a GUI-component method, because then the GUI will not be updated until the sleep is over (just what you see).

create a separate thread for sleeping and updating your board and let the GUI repaint as needed.

thomas

kloebera at 2007-7-12 8:45:17 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thanks
Abdallaha at 2007-7-12 8:45:17 > top of Java-index,Desktop,Core GUI APIs...