Class LifeTime

java.lang.Object
swingtree.animation.LifeTime

@Immutable public final class LifeTime extends Object
The lifetime is an immutable and thread safe value based object, which defines for how long an Animation should run. It consists of a delay, interval and duration as well as a unique id which ensures that two instances of this class are never equal.
You can create a new lifetime using the static factory methods of(long, TimeUnit), of(double, TimeUnit), of(long, TimeUnit, long, TimeUnit) and of(double, TimeUnit, double, TimeUnit).
Update an existing lifetime using the methods startingIn(long, TimeUnit) and withInterval(long, TimeUnit). Note that the default interval of a newly created lifetime is always 16 ms which corresponds to 60 fps.

This class is typically used to schedule animations. The most straight forward way would be to call UIFactoryMethods.animateFor(LifeTime) or UIFactoryMethods.animateFor(LifeTime, Component). But you may also schedule a style animation using UIForAnySwing.withTransitoryStyle(Event, LifeTime, AnimatedStyler) or UIForAnySwing.withTransitionalStyle(Val, LifeTime, AnimatedStyler).
Another use case is to schedule an animation through the component event delegate as part of your event handling code using AbstractDelegate.animateFor(LifeTime).
This may look like this:

  UI.button("I pop when you hover over me")
  .onMouseEnter( it -> it.animateFor(1, TimeUnit.SECONDS, state -> {
    it.style(state, conf -> conf
      .borderWidth( 10 * state.cycle() )
      .borderColor(UI.color(1,1,0,1-state.cycle()))
      .borderRadius( 100 * state.cycle() )
    );
  }))
  
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
     
    long
    Returns the delay after which the animation should start in the given time unit.
    long
    Returns the duration of the animation in the given time unit.
    long
    Returns the interval in the given time unit, which is a number that determines the delay between two consecutive animation steps.
    int
     
    static LifeTime
    of(double time, TimeUnit unit)
    Creates a new lifetime that will run for the given duration in the given time unit and without any start delay.
    static LifeTime
    of(double startDelay, TimeUnit startUnit, double duration, TimeUnit durationUnit)
    Creates a new lifetime that will start after the given delay and run for the given duration.
    static LifeTime
    of(long time, TimeUnit unit)
    Creates a new lifetime that will run for the given duration and without any start delay.
    static LifeTime
    of(long startDelay, TimeUnit startUnit, long duration, TimeUnit durationUnit)
    Creates a new lifetime that will start after the given delay and run for the given duration.
    startingIn(long delay, TimeUnit unit)
    Creates a new lifetime that will start after the given delay in the given time unit.
     
    withInterval(long interval, TimeUnit unit)
    Updates this lifetime with the given interval, which is a property that determines the delay between two consecutive animation steps.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • of

      public static LifeTime of(long time, TimeUnit unit)
      Creates a new lifetime that will run for the given duration and without any start delay.
      Parameters:
      time - The duration of the animation.
      unit - The time unit of the duration.
      Returns:
      A new lifetime that will start immediately and run for the given duration.
    • of

      public static LifeTime of(double time, TimeUnit unit)
      Creates a new lifetime that will run for the given duration in the given time unit and without any start delay.
      Contrary to the of(long, TimeUnit) method, this method uses a double type to allow for fractional time values.
      Parameters:
      time - The duration of the animation.
      unit - The time unit of the duration.
      Returns:
      A new lifetime that will start immediately and run for the given duration.
    • of

      public static LifeTime of(long startDelay, TimeUnit startUnit, long duration, TimeUnit durationUnit)
      Creates a new lifetime that will start after the given delay and run for the given duration.
      Parameters:
      startDelay - The delay after which the animation should start.
      startUnit - The time unit of the delay.
      duration - The duration of the animation.
      durationUnit - The time unit of the duration.
      Returns:
      A new lifetime that will start after the given delay and run for the given duration.
    • of

      public static LifeTime of(double startDelay, TimeUnit startUnit, double duration, TimeUnit durationUnit)
      Creates a new lifetime that will start after the given delay and run for the given duration. Contrary to the of(long, TimeUnit, long, TimeUnit) method, this method uses a double type to allow for fractional time values.
      Parameters:
      startDelay - The delay after which the animation should start.
      startUnit - The time unit of the delay.
      duration - The duration of the animation.
      durationUnit - The time unit of the duration.
      Returns:
      A new lifetime that will start after the given delay and run for the given duration.
    • startingIn

      public LifeTime startingIn(long delay, TimeUnit unit)
      Creates a new lifetime that will start after the given delay in the given time unit.
      Parameters:
      delay - The delay after which the animation should start.
      unit - The time unit of the delay.
      Returns:
      A new lifetime that will start after the given delay.
    • withInterval

      public LifeTime withInterval(long interval, TimeUnit unit)
      Updates this lifetime with the given interval, which is a property that determines the delay between two consecutive animation steps. You can think of it as the time between the heartbeats of the animation. The smaller the interval, the higher the refresh rate and the smoother the animation will look. However, the smaller the interval, the more CPU time will be used. The default interval is 16 ms which corresponds to 60 fps.
      If you want a custom interval default, you can configure it during library initialization through the SwingTree.initialiseUsing(SwingTreeConfigurator) method or change it at any other time using the SwingTree.setDefaultAnimationInterval(long) method.
      Parameters:
      interval - The interval in the given time unit.
      unit - The time unit of the interval, typically TimeUnit.MILLISECONDS.
      Returns:
      A new lifetime that will start after the given delay and run for the given duration.
    • getDurationIn

      public long getDurationIn(TimeUnit unit)
      Returns the duration of the animation in the given time unit.
      Parameters:
      unit - The time unit in which the duration should be returned.
      Returns:
      The duration of the animation.
    • getDelayIn

      public long getDelayIn(TimeUnit unit)
      Returns the delay after which the animation should start in the given time unit.
      Parameters:
      unit - The time unit in which the delay should be returned.
      Returns:
      The delay after which the animation should start.
    • getIntervalIn

      public long getIntervalIn(TimeUnit unit)
      Returns the interval in the given time unit, which is a number that determines the delay between two consecutive animation steps. You can think of it as the time between the heartbeats of an animation. The smaller the interval, the higher the refresh rate and the smoother the animation will look. However, the smaller the interval, the more CPU time will be used. The default interval is 16 ms which corresponds to 60 fps.
      If you want a custom interval default, you can configure it during library initialization through the SwingTree.initialiseUsing(SwingTreeConfigurator) method or change it at any other time using the SwingTree.setDefaultAnimationInterval(long) method.
      Parameters:
      unit - The time unit in which the interval should be returned.
      Returns:
      The interval in the given time unit.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object