Results 1 to 2 of 2
  1. #1
    erversteeg is offline Member
    Join Date
    Dec 2010
    Posts
    12
    Rep Power
    0

    Default Thread.sleep, Event, Repaint(), problems

    Hi Java Forums! I'm writing a MindReader game for my programming class and have run into a problem. Basically the user selects 'heads' or 'tails' and my program implements crude AI to guess what the user picked. My program works flawlessly but I want the thread to pause a few times to display what the computer guessed, ect. Well my program doesn't seem to call repaint() until my entire event code is done firing. Any ideas on what I can do to repaint() before the thread sleeps?

    Below is my event code.

    Java Code:
    class UserInputListener implements MouseListener{
    
    		public void mouseClicked(MouseEvent arg0) {}
    		public void mouseEntered(MouseEvent arg0) {}
    		public void mouseExited(MouseEvent arg0) {}
    		public void mousePressed(MouseEvent e) {}
    		public void mouseReleased(MouseEvent e) {
    			
    			gui.clearH();
    			if(((JPanel) e.getSource()).equals((JPanel) gui.getHDefHeads())){
    				pick = HEADS;
    				gui.getHHeads().setVisible(true);
    				gui.getHHeadsPickMsg().setVisible(true);
    			}
    			else{
    				pick = TAILS;
    				gui.getHTails().setVisible(true);
    				gui.getHTailsPickMsg().setVisible(true);
    			}
    			gui.getGamePanel().revalidate();
    			gui.getGamePanel().repaint();
    			
    			
    				
    			mrGuess = mr.guess();
    			
    			if(mrGuess == pick){
    				cScore++;
    				gui.getCHeads().setVisible(true);
    				gui.getCHeadsGuessMsg().setVisible(true);
    				gui.getCScore().setName(Integer.toString(cScore));
    			}
    			else{
    				hScore++;
    				gui.getCTails().setVisible(true);
    				gui.getCTailsGuessMsg().setVisible(true);
    				gui.getHScore().setName(Integer.toString(hScore));
    			}
    			
    			mr.userPicked(pick);
    			gui.restoreDefault();
    		}
        }
    Thanks,
    Eric

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

    Default

    It's likely that you're calling Thread.sleep(...) on the Event Dispatch Thread, the EDT, which is the main thread that Swing uses to paint the screen and interact with the user. This does little more than put your whole application to sleep which is not what you want to do. Instead read up on using a swing Timer.

Similar Threads

  1. Difference between Thread.yield() and Thread.sleep() methods
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 12
    Last Post: 07-30-2010, 05:37 PM
  2. how to reduce the thread sleep time and wake up the thread
    By baktha.thalapathy in forum Threads and Synchronization
    Replies: 2
    Last Post: 06-24-2010, 07:36 PM
  3. thread.sleep
    By jc5 in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-02-2009, 11:51 PM
  4. Can't get my thread to sleep!
    By jamesfrize in forum New To Java
    Replies: 2
    Last Post: 03-25-2008, 05:14 AM
  5. How to use the sleep and thread?
    By jiuhu in forum Java Applets
    Replies: 4
    Last Post: 08-07-2007, 02:56 AM

Posting Permissions

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