0

So I was making a program in JavaFX where I use an AnimationTimer. I need to be able to pause the AnimationTimer whenever I want, so someone recommended using Timelines on one of my other Stackoverflow questions. However, it seems that you can only pause an AnimationTimer for a given amount of time once. For example, while the following code should pause the AnimationTimer for a total of 1 second over four equal intervals, it only pauses it once:

public Timeline createPauseTimerTimeline(AnimationTimer timer, Duration duration) {
    return new Timeline(
            new KeyFrame(Duration.ZERO, event -> timer.stop()),
            new KeyFrame(duration, event -> timer.start())
    );
}

// Inside some other method:
Timeline delay = createPauseTimerTimeline(timer, new Duration(250));
/* blah blah */
delay.playFromStart();
/* blah blah */
delay.playFromStart();
/* blah blah */
delay.playFromStart();
/* blah blah */
delay.playFromStart();
/* blah blah */

How can I fix the code so that I can pause the AnimationTimer for a set number of seconds whenever I want? Any help would be greatly appreciated.

John A.
  • 147
  • 4
  • 2
    Can you please provide a [minimal, complete and verifiable example](https://stackoverflow.com/help/minimal-reproducible-example) – Sai Dandem May 30 '19 at 03:55
  • `PauseTransition` maybe? https://stackoverflow.com/questions/9966136/javafx-periodic-background-task – SedJ601 May 30 '19 at 06:10
  • 2
    Animations are ran on the _JavaFX Application Thread_ but are launched and ran "asynchronously". When you start the animation the thread immediately returns and continues executing the rest of the method. But the animation can't begin processing until the FX thread is free to do the work, which doesn't happen until the FX thread returns from the method(s) it's currently executing. The net result here is that you only have an animation that runs for 250 milliseconds (the last play). However, without a proper example showing what you're trying to do it's difficult to provide a solution. – Slaw May 30 '19 at 06:39
  • 1
    @Sedrick I don't think that will make a difference if `playFromStart()` is called multiple times in the same method—with the expectation that the duration of the animation will stack. – Slaw May 30 '19 at 06:41

0 Answers0