Results 1 to 9 of 9
- 05-07-2011, 12:33 AM #1
Member
- Join Date
- May 2010
- Posts
- 34
- Rep Power
- 0
What type of delay will work for me?
I am making a game, and since this is normally a real life game, I need to set a delay between each computer player.
In the following example, when either a computer or player finishes their turn, the whosturn method will be called again.
I tried the sleep (below) but it would accumulate into 6 (or even longer) seconds instead of (delay 2 seconds, update gui)x3. I've also tried, wait, delay, (pause?) but those haven't worked yet... So which one am I supose to use for this situation?Java Code:public void whosTurn() if(computer player) { //comuter stuff //turn++; //update GUI DELAY } else { //player stuff //turn++; //update GUI } whosTurn(); }
Java Code:try { Thread.sleep(2000); } catch (InterruptedException e) { }
-
Is this a GUI? If so, what kind? Swing?
- 05-07-2011, 12:46 AM #3
Member
- Join Date
- May 2010
- Posts
- 34
- Rep Power
- 0
yes it is a swing gui. i'm using, repaint(), removeAll(), *changes* revalidate() for my updates.
-
If it's Swing, then don't call Thread.sleep() in the GUI code (actually on the event dispatch thread or main Swing thread) which will put the whole application to sleep. If you need a delay, use a Swing Timer.
- 05-07-2011, 12:50 AM #5
Member
- Join Date
- May 2010
- Posts
- 34
- Rep Power
- 0
that whosTurn method I wrote there is not in the GUI class, its from the "brains of it" class..
-
- 05-07-2011, 12:55 AM #7
Member
- Join Date
- May 2010
- Posts
- 34
- Rep Power
- 0
EDT? I don't think the
try {
Thread.sleep(2000);
}
catch (InterruptedException e) {
}
interfears much with the GUI, But i'm not sure where "Tread" is from, as this is my first time writing swing.
-
It would be hard for us to know what else is going on in your code though as we're only privy to a small glimpse. Consider creating a small compilable and runnable app that demonstrates your problem, an SSCCE
- 05-07-2011, 08:15 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
If you can find an execution path starting at a, say, ActionListener, called by the Event Dispatch Thread (EDT) owned by Swing, to your Thread.sleep( ... ) call without your code having started a new Thread for that call, you are executing that entire execition path on the EDT so Swing will freeze when it sleeps.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Read URL with delay
By Eviler in forum Advanced JavaReplies: 5Last Post: 04-04-2011, 07:16 AM -
Why doesn't this work? (multi-type array)
By 711groove in forum New To JavaReplies: 2Last Post: 12-18-2009, 09:56 PM -
how to give delay?
By shaluchandran in forum New To JavaReplies: 10Last Post: 12-17-2008, 05:57 PM -
Delay on inputs during calculation
By matt_well in forum New To JavaReplies: 14Last Post: 07-26-2008, 04:17 PM -
How to mesure ping delay in JAVA..?
By sacr83 in forum NetworkingReplies: 4Last Post: 06-15-2008, 06:37 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks