Force444
Force444

Reputation: 3381

How do I pause Java Simulation using Swing

Okay I have a simulation running. I have implemented two JButtons which actionListeners on the GUI of my simulation. What I want is to have the whole simulation to pause if I press the pause button and to resume if I press the resume button.

There are multiple threads running, and I have tried to get each of the threads and invoke their wait() method when the pause button is clicked but I have not been successful in doing so.

Hence, I need some advice on how to do this. I'm using Swing for the GUI and the listeners work fine. I did try to invoke a sleep() and wait() on the current thread in the View class (using MVC pattern) just to see what happened but that caused the whole application to crash.

Any ideas?

Upvotes: 1

Views: 1782

Answers (4)

Andrew Thompson
Andrew Thompson

Reputation: 168835

If the animation is using a Swing Timer the simple answer would be to call stop().

I highly recommend using the Timer for animation since it ensures the actions are performed on the EDT.

Upvotes: 2

hoaz
hoaz

Reputation: 10161

You need to provide additional logic to your simulation thread that will accept "signals" from controlling (GUI) thread and wait for the next controlling signal to resume execution.

For example you can use volatile boolean isPaused instance field in simulation thread. Set it to true/false to pause/resume simulation thread. In simulation thread implement corresponding logic:

public void run() {
    while (true) {
        simulate();
        while (isPaused) {
            Thread.sleep(100);
        }
    }
}

Upvotes: 3

bhuang3
bhuang3

Reputation: 3633

I think you can create a ThreadGroup to store all your threads, then try to use

ThreadGroup threadGroup = Thread.currentThread( ).getThreadGroup( );

By the way, ExecutorService is a good choice, too.

Upvotes: 0

durron597
durron597

Reputation: 32323

Use a shared object to communicate between the two classes. Then have your gui set the value of the state object when the button is clicked, and change it again when it's clicked again. Here's some sample code:

public class Simulation implements Runnable {
  private State myState = null;

  public Simulation(State myState) {
    this.myState = myState;
  }

  public void run() {
    while(myState != null) {
      if (myState.isPaused()) myState.wait();
      // Do other stuff
    }
  }
}

public class MainClass implements ActionListener() {
  private State myState = new MyState();

  private void beginSimulation() {
    Simulation s = new Simulation(this.myState());
    new Thread(s).start();
  }  

  public void actionPerformed(ActionEvent e) {
    if(myState.isPaused()) {
      myState.setPaused(false);
      myState.notify();
    } else {
      myState.setPaused(true);
    }
  }
}

public class MyState() {
  private boolean paused = false;
  public MyState(boolean paused) { this.paused = paused; }
  public boolean getPaused() { return paused; }
  public void setPaused(boolean paused) { this.paused = paused; }
}

Upvotes: 0

Related Questions