Class AnimationDispatcher

java.lang.Object
swingtree.animation.AnimationDispatcher

public final class AnimationDispatcher extends Object
An API for creating an Animation and defining how it should be executed. Instances of this class are intended to be created and used either by the UI API or the user event delegation API (see ComponentDelegate).
The UI API can be used like so:

    UI.animateFor( 100, TimeUnit.MILLISECONDS ) // returns an Animate instance
       .until( it -> it.progress() >= 0.75 && someOtherCondition() )
       .go( status -> {
          // do something
          someComponent.setValue( it.progress() );
          // ...
          someComponent.repaint();
       });
   
The user event delegation API can be used like this:

       panel()
       .onMouseClick( it -> {
           it.animateFor( 100, TimeUnit.MILLISECONDS )
           .goOnce( status -> {
               int width = (int) (100 * state.progress());
               it.getComponent().setSize( width, 100 );
           });
       })
   
  • Method Details

    • animateFor

      public static AnimationDispatcher animateFor(LifeTime lifeTime)
      Creates an AnimationDispatcher instance which allows you to define the stop condition for an animation as well as an Animation that will be executed when passed to the go(Animation) method.
      Parameters:
      lifeTime - The schedule that defines when the animation should be executed and for how long.
      Returns:
      An AnimationDispatcher instance that can be used to define how the animation should be executed.
    • animateFor

      public static AnimationDispatcher animateFor(LifeTime lifeTime, Stride stride)
      Creates an AnimationDispatcher instance which allows you to define the stop condition for an animation as well as an Animation that will be executed when passed to the go(Animation) method.
      Parameters:
      lifeTime - The schedule that defines when the animation should be executed and for how long.
      stride - The stride of the animation, i.e. whether it should be executed progressively or regressively.
      Returns:
      An AnimationDispatcher instance that can be used to define how the animation should be executed.
    • animateFor

      public static AnimationDispatcher animateFor(LifeTime lifeTime, Component component)
      Creates an AnimationDispatcher instance which allows you to define the stop condition for an animation as well as an Animation that will be executed when passed to the go(Animation) method.
      Parameters:
      lifeTime - The schedule that defines when the animation should be executed and for how long.
      component - The component that should be repainted after each animation step.
      Returns:
      An AnimationDispatcher instance that can be used to define how the animation should be executed.
    • animateFor

      public static AnimationDispatcher animateFor(LifeTime lifeTime, Stride stride, Component component)
      Creates an AnimationDispatcher instance which allows you to define the stop condition for an animation as well as an Animation that will be executed when passed to the go(Animation) method.
      Parameters:
      lifeTime - The schedule that defines when the animation should be executed and for how long.
      stride - The stride of the animation, i.e. whether it should be executed progressively or regressively. See Stride for more information.
      component - The component that should be repainted after each animation step.
      Returns:
      An AnimationDispatcher instance that can be used to define how the animation should be executed.
    • until

      public AnimationDispatcher until(Predicate<AnimationStatus> shouldStop)
      Use this to define a stop condition for the animation.
      Parameters:
      shouldStop - The stop condition for the animation, i.e. the animation will be executed until this condition is true.
      Returns:
      A new AnimationDispatcher instance that will be executed until the given stop condition is true.
    • asLongAs

      public AnimationDispatcher asLongAs(Predicate<AnimationStatus> shouldRun)
      Use this to define a running condition for the animation.
      Parameters:
      shouldRun - The running condition for the animation, i.e. the animation will be executed as long as this condition is true.
      Returns:
      A new AnimationDispatcher instance that will be executed as long as the given running condition is true.
    • go

      public void go(Animation animation)
      Runs the given animation based on the stop condition defined by until(Predicate) or asLongAs(Predicate). If no stop condition was defined, the animation will be executed once. If you want to run an animation forever, simply pass status -> true to the asLongAs(Predicate) method, or status -> false to the until(Predicate) method.
      Parameters:
      animation - The animation that should be executed.
    • goWithOffset

      public void goWithOffset(long offset, TimeUnit unit, Animation animation)
      Runs the given animation based on a time offset in the given time unit and the stop condition defined by until(Predicate) or asLongAs(Predicate). If no stop condition was defined, the animation will be executed once. If you want to run an animation forever, simply pass status -> true to the asLongAs(Predicate) method, or status -> false to the until(Predicate) method.

      This method is useful in cases where you want an animation to start in the future, or somewhere in the middle of their lifespan progress (see AnimationStatus.progress()).

      Parameters:
      offset - The offset in the given time unit after which the animation should be executed. This number may also be negative, in which case the animation will be executed immediately, and with a AnimationStatus.progress() value that is advanced according to the offset.
      unit - The time unit in which the offset is specified.
      animation - The animation that should be executed.