Results 1 to 6 of 6
  1. #1
    BigBear is offline Member
    Join Date
    Jan 2010
    Posts
    37
    Rep Power
    0

    Default 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:

    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;
        }
    }
    I need to interrupt this game by click the button. How can I put it through? Thank you.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    BigBear is offline Member
    Join Date
    Jan 2010
    Posts
    37
    Rep Power
    0

    Default

    Oh... ok, I'll try. I hope I can handle it :-)... thank you.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  5. #5
    BigBear is offline Member
    Join Date
    Jan 2010
    Posts
    37
    Rep Power
    0

    Default

    Whoopee :-)... thank you very much. It's done :-)

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. How to stop thread when it is sleeping
    By luoluo in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-24-2010, 04:09 AM
  2. interrupt a jdbc connection thread
    By ko_aung in forum Threads and Synchronization
    Replies: 5
    Last Post: 04-12-2010, 09:04 PM
  3. Replies: 2
    Last Post: 03-19-2010, 09:24 AM
  4. Mouse Listener for mouse floating over object?
    By Krooger in forum AWT / Swing
    Replies: 1
    Last Post: 11-18-2009, 05:34 AM
  5. Anyone know how to interrupt a process forcibly?
    By 2potatocakes in forum Advanced Java
    Replies: 3
    Last Post: 04-05-2009, 07:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •