Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a delay in Swing

I made a blackjack game, and I want the AI player to pause between taking cards. I tried simply using Thread.sleep(x), but that makes it freeze until the AI player is done taking all of his cards. I know that Swing is not thread safe, so I looked at Timers, but I could not understand how I could use one for this. Here is my current code:

while (JB.total < 21) {

          try {
            Thread.sleep(1000);
          } catch (InterruptedException ex) {
            System.out.println("Oh noes!");
          }

          switch (getJBTable(JB.total, JB.aces > 0)) {
            case 0:
              JB.hit();
              break;
            case 1:
              break done;
            case 2:
              JB.hit();
              JB.bet *= 2;
              break done;
          }
        }

BTW, the hit(); method updates the GUI.

like image 224
Fractaly Avatar asked Aug 31 '11 01:08

Fractaly


People also ask

How do you delay an action in Java?

The easiest way to delay a java program is by using Thread. sleep() method. The sleep() method is present in the Thread class. It simply pauses the current thread to sleep for a specific time.

How do you stop a swing timer?

To start the timer, call its start method. To suspend it, call stop .


3 Answers

so I looked at Timers, but I could not understand how I could use one for this

The Timer is the solution, since as you say you are updating the GUI which should be done on the EDT.

I'm not sure what your concern is. You deal a card and start the Timer. When the Timer fires you decide to take another card or hold. When you hold your stop the Timer.

like image 181
camickr Avatar answered Nov 05 '22 23:11

camickr


Well, a quick explanation about Timers.

First of all, you need a java.util.Timer variable in your class and another class in your project which extends from java.util.TimerTask (let's call it Tasker).

The initialization of the Timer variable is so easy:

Timer timer = new Timer();

Now the Tasker class:

public class Tasker extends TimerTask {
    @Override
    public void run() {
        actionToDo(); // For example take cards 
    }

    // More functions if they are needed
}

Finally, the installation of the timer with its related Tasker:

long delay = 0L;
long period = pauseTime;
timer.schedule(new Tasker(),delay,period);

The schedule function indicates the following: Fisrt param: Action to do each period milliseconds (Executes the run function of a TimerTask class or its extension) Second param: When the timer must start. In this case, it starts when the schedule function is called. The following example indicates a starting 1 second after call the schedule function: timer.schedule(new Tasker(),1000,period); Third param: milliseconds between one call of Tasker.run() function and the following call.

I hope you understand this microtutorial :). If you have any problem, ask for more detailed information!

Kind regards!

like image 4
Charliemops Avatar answered Nov 05 '22 23:11

Charliemops


I think that in this tutorial is clear how to use Timers in order to achieve what you want, without having to deal with Threads.

like image 3
Luismahou Avatar answered Nov 06 '22 01:11

Luismahou