Class Animator

java.lang.Object
swingtree.animation.Animator

public class Animator 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.schedule( 100, TimeUnit.MILLISECONDS ) // returns an Animate instance
       .until( it -> it.progress() >= 0.75 && someOtherCondition() )
       .go( state -> {
          // 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( state -> {
               int width = (int) (100 * state.progress());
               it.getComponent().setSize( width, 100 );
           });
       })
   
  • Method Details

    • animateFor

      public static Animator animateFor(LifeTime lifeTime)
      Creates an Animator 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 Animator instance that can be used to define how the animation should be executed.
    • animateFor

      public static Animator animateFor(LifeTime lifeTime, Stride stride)
      Creates an Animator 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 Animator instance that can be used to define how the animation should be executed.
    • animateFor

      public static Animator animateFor(LifeTime lifeTime, Component component)
      Creates an Animator 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 Animator instance that can be used to define how the animation should be executed.
    • animateFor

      public static Animator animateFor(LifeTime lifeTime, Stride stride, Component component)
      Creates an Animator 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 Animator instance that can be used to define how the animation should be executed.
    • until

      public Animator until(Predicate<AnimationState> 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 Animator instance that will be executed until the given stop condition is true.
    • asLongAs

      public Animator asLongAs(Predicate<AnimationState> 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 Animator 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 state -> true to the asLongAs(Predicate) method, or state -> 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 state -> true to the asLongAs(Predicate) method, or state -> 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 AnimationState.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 AnimationState.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.