Results 1 to 3 of 3
Thread: program not delaying
- 11-29-2009, 02:04 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 5
- Rep Power
- 0
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
and here are the problem areasJava 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);
i dont understand exactly why this doesnt work, because from what i understand, the code creates a square and changes its color randomlyJava 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();
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).
- 11-29-2009, 02:10 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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
-
To understand the problem, please read this: Concurrency in Swing
Then bite the bullet and use a Swing Timer, not Thread.sleep
Similar Threads
-
changing my program to array working program
By Chewart in forum New To JavaReplies: 39Last Post: 11-18-2009, 06:53 PM -
Execute A program from a Program!
By Moncleared in forum Advanced JavaReplies: 2Last Post: 02-22-2009, 04:17 PM -
Executing a program within a program
By gibsonrocker800 in forum New To JavaReplies: 5Last Post: 05-12-2008, 08:24 AM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks