Results 1 to 9 of 9
  1. #1
    keo
    keo is offline Member
    Join Date
    May 2010
    Posts
    34
    Rep Power
    0

    Default 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.
    Java Code:
    public void whosTurn()
    if(computer player) {
         //comuter stuff
         //turn++;
         //update GUI
         DELAY
    }
    else {
         //player stuff
         //turn++;
         //update GUI
    }
    whosTurn();
    }
    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:
            try {
                Thread.sleep(2000);
            }
            catch (InterruptedException e) {
            }

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

  3. #3
    keo
    keo is offline Member
    Join Date
    May 2010
    Posts
    34
    Rep Power
    0

    Default

    yes it is a swing gui. i'm using, repaint(), removeAll(), *changes* revalidate() for my updates.

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

    Default

    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.

  5. #5
    keo
    keo is offline Member
    Join Date
    May 2010
    Posts
    34
    Rep Power
    0

    Default

    that whosTurn method I wrote there is not in the GUI class, its from the "brains of it" class..

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

    Default

    Quote Originally Posted by keo View Post
    that whosTurn method I wrote there is not in the GUI class, its from the "brains of it" class..
    But is it called on the EDT, on the main Swing thread?

  7. #7
    keo
    keo is offline Member
    Join Date
    May 2010
    Posts
    34
    Rep Power
    0

    Default

    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.

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

    Default

    Quote Originally Posted by keo View Post
    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

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by keo View Post
    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.
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Read URL with delay
    By Eviler in forum Advanced Java
    Replies: 5
    Last Post: 04-04-2011, 07:16 AM
  2. Why doesn't this work? (multi-type array)
    By 711groove in forum New To Java
    Replies: 2
    Last Post: 12-18-2009, 09:56 PM
  3. how to give delay?
    By shaluchandran in forum New To Java
    Replies: 10
    Last Post: 12-17-2008, 05:57 PM
  4. Delay on inputs during calculation
    By matt_well in forum New To Java
    Replies: 14
    Last Post: 07-26-2008, 04:17 PM
  5. How to mesure ping delay in JAVA..?
    By sacr83 in forum Networking
    Replies: 4
    Last Post: 06-15-2008, 06:37 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
  •