Results 1 to 6 of 6
- 08-12-2010, 11:36 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 37
- Rep Power
- 0
interrupt sleeping thread by mouse action
Hello, I'm developing a computer desk game like checkers (swing application). Now I can't solve up one of my last problems. I have to implement user-user, user-computer and computer-computer game. The first two of them are simple and the last, computer-computer, is problem to me.
My application has menu and toolbar, where the settings of the game items are set up. If I choose computer-computer game in settings, everything is correct. Computer starts to play and answers again. But If I want to interrupt the play, it is impossible. I'd like to click the button in the toolbar, or the item in the menu bar, but the game won't let me nor click the button neither expand the menu.
I have written something like this:
I need to interrupt this game by click the button. How can I put it through? Thank you.Java Code:// loop while one of the players isn't set to 'user' while(!(getPlayer1().equals("user") || getPlayer2().equals("user"))) { Thread.sleep(4000); // time for interrupt computer-computer game String[] bestMove = generateBestMove(); // generate best move with given difficulty move(bestMove[0], bestMove[1]); // put the move through repaintBoard(); // repaint the board if (Referee.checkGameEnd()) { break; } }
-
Calling Thread.sleep will only put the entire application to sleep. If you need to slow the steps of the computer-computer game, I'd get rid of the while loop and instead use a Swing Timer.
- 08-12-2010, 12:03 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 37
- Rep Power
- 0
Oh... ok, I'll try. I hope I can handle it :-)... thank you.
-
Edit, also, if you want to stop the logic by mouse button, then you'll need to pause the timer on button click by calling stop() on the Timer.
- 08-12-2010, 09:42 PM #5
Member
- Join Date
- Jan 2010
- Posts
- 37
- Rep Power
- 0
Whoopee :-)... thank you very much. It's done :-)
-
Similar Threads
-
How to stop thread when it is sleeping
By luoluo in forum Threads and SynchronizationReplies: 2Last Post: 04-24-2010, 03:09 AM -
interrupt a jdbc connection thread
By ko_aung in forum Threads and SynchronizationReplies: 5Last Post: 04-12-2010, 08:04 PM -
How to interrupt threads blocked in a RMI operation
By sky in forum Advanced JavaReplies: 2Last Post: 03-19-2010, 08:24 AM -
Mouse Listener for mouse floating over object?
By Krooger in forum AWT / SwingReplies: 1Last Post: 11-18-2009, 04:34 AM -
Anyone know how to interrupt a process forcibly?
By 2potatocakes in forum Advanced JavaReplies: 3Last Post: 04-05-2009, 06:29 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks