Interface AnimatedIcon

All Superinterfaces:
Icon
All Known Implementing Classes:
FlatAnimatedIcon

public interface AnimatedIcon extends Icon
Icon that automatically animates painting on component value changes.

getValue(Component) returns the value of the component. If the value changes, then paintIconAnimated(Component, Graphics, int, int, float) is invoked multiple times with animated value (from old value to new value).

Example for an animated icon:

 private class AnimatedMinimalTestIcon
     implements AnimatedIcon
 {
     @Override public int getIconWidth() { return 100; }
     @Override public int getIconHeight() { return 20; }

     @Override
     public void paintIconAnimated( Component c, Graphics g, int x, int y, float animatedValue ) {
         int w = getIconWidth();
         int h = getIconHeight();

         g.setColor( Color.red );
         g.drawRect( x, y, w - 1, h - 1 );
         g.fillRect( x, y, Math.round( w * animatedValue ), h );
     }

     @Override
     public float getValue( Component c ) {
         return ((AbstractButton)c).isSelected() ? 1 : 0;
     }
 }

 // sample usage
 JCheckBox checkBox = new JCheckBox( "test" );
 checkBox.setIcon( new AnimatedMinimalTestIcon() );
 
Animation works only if the component passed to paintIcon(Component, Graphics, int, int) is an instance of JComponent. A client property is set on the component to store the animation state.
  • Method Details

    • paintIcon

      default void paintIcon(Component c, Graphics g, int x, int y)
      Specified by:
      paintIcon in interface Icon
    • paintIconAnimated

      void paintIconAnimated(Component c, Graphics g, int x, int y, float animatedValue)
      Paints the icon for the given animated value.
      Parameters:
      c - the component that this icon belongs to
      g - the graphics context
      x - the x coordinate of the icon
      y - the y coordinate of the icon
      animatedValue - the animated value, which is either equal to what getValue(Component) returned, or somewhere between the previous value and the latest value that getValue(Component) returned
    • getValue

      float getValue(Component c)
      Gets the value of the component.

      This can be any value and depends on the component. If the value changes, then this class animates from the old value to the new one.

      For a toggle button this could be 0 for off and 1 for on.

    • isAnimationEnabled

      default boolean isAnimationEnabled()
      Returns whether animation is enabled for this icon (default is true).
    • getAnimationDuration

      default int getAnimationDuration()
      Returns the duration of the animation in milliseconds (default is 150).
    • getAnimationResolution

      default int getAnimationResolution()
      Returns the resolution of the animation in milliseconds (default is 10). Resolution is the amount of time between timing events.
    • getAnimationInterpolator

      default Animator.Interpolator getAnimationInterpolator()
      Returns the interpolator for the animation. Default is CubicBezierEasing.STANDARD_EASING.
    • getClientPropertyKey

      default Object getClientPropertyKey()
      Returns the client property key used to store the animation support.