How can I know if an AnimationTimer is running?

2019-08-02 17:18发布

问题:

Is there a way to know if an AnimationTimer is running or not? I would like to toggle a pause menu like this:

setOnKeyTyped(event -> {
  switch (event.getCode()) {
    case SPACE:
      if (animationTimer.isRunning()) { // What should I put here?
        animationTimer.stop();
      }
      else {
        animationTimer.start();
      }
      break;

    default:
      break;
  }
}

回答1:

You can override start and stop to keep information about whether the timer is running or not:

class StatusTimer extends AnimationTimer {

    private volatile boolean running;

    @Override
    public void start() {
         super.start();
         running = true;
    }

    @Override
    public void stop() {
        super.stop();
        running = false;
    }

    public boolean isRunning() {
        return running;
    }

    ...

}

There seems to be no other way without accessing private members of AnimationTimer.