Results 1 to 3 of 3
  1. #1
    Brain_Child is offline Member
    Join Date
    Nov 2009
    Posts
    5
    Rep Power
    0

    Default program not delaying

    i want my program to wait for a click, show a label for a second and remove the label. i wasnt able to do this and what i found was that the behaviour of code changed when it was in a the run method and when it is moved to the mouseClicked method

    here is the code

    Java Code:
    		GRect square = new GRect(0, 0, SQUARE_SIZE, SQUARE_SIZE); 
    		square.setFilled(true); 
    		add(square); 
    		for (int i=0; i<5; i++) { 
    			square.setColor(rgen.nextColor()); 
    			pause(PAUSE_TIME);
    and here are the problem areas

    Java Code:
    	private static final int SQUARE_SIZE = 100;
    	/* Pause time in milliseconds */ 
    	private static final int PAUSE_TIME = 1000; 
    	
    	public void run() {
    		[COLOR="Blue"]// code works here[/COLOR]
    		addMouseListeners();
                             [COLOR="Blue"]// code works here[/COLOR]
    		
    	}
    	
    	public void mouseClicked(MouseEvent e) {
    		[COLOR="Red"]//code does not work here[/COLOR]
    		} 
    	}
    
                 private RandomGenerator rgen = RandomGenerator.getInstance();
    i dont understand exactly why this doesnt work, because from what i understand, the code creates a square and changes its color randomly
    5 times, with each color change being 1 second apart.

    but when the code is inside the mouseClicked method, this doesnt happen. all you get is 5 seconds of nothing and then you get the final position instantly. its like the pause(PAUSE_TIME) is being executed 5 times first, and then everything else happens in a few milliseconds.

    (i have googled and found some thread.sleep alternative, so i am aware of that. but i dont want an alternative, i want to understand my error, not ignore it).

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default

    Swing/AWT uses one single thread for all the mouse scanning, updating graphics etc. Most likely your code is running in that same EDT (Event Dispatch Thread) and it keeps that thread busy working on your code (waiting, etc.) so the EDT thread can't update the screen. Take your code away from the EDT and all will be fine again. All you have to do is start another thread and do your work in that thread. Note that your actionPerformed( ... ) method (if any) is called from the EDT, so you should start your new Thread in there.

    kind regards,

    Jos

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

Similar Threads

  1. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  2. Execute A program from a Program!
    By Moncleared in forum Advanced Java
    Replies: 2
    Last Post: 02-22-2009, 04:17 PM
  3. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 08:24 AM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 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
  •