Interface AnimatedIcon

  • All Superinterfaces:
    javax.swing.Icon
    All Known Implementing Classes:
    FlatAnimatedIcon

    public interface AnimatedIcon
    extends javax.swing.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.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Interface Description
      static class  AnimatedIcon.AnimationSupport
      Animation support class that stores the animation state and implements the animation.
    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      default int getAnimationDuration()
      Returns the duration of the animation in milliseconds (default is 150).
      default Animator.Interpolator getAnimationInterpolator()
      Returns the interpolator for the animation.
      default int getAnimationResolution()
      Returns the resolution of the animation in milliseconds (default is 10).
      default java.lang.Object getClientPropertyKey()
      Returns the client property key used to store the animation support.
      float getValue​(java.awt.Component c)
      Gets the value of the component.
      default boolean isAnimationEnabled()
      Returns whether animation is enabled for this icon (default is true).
      default void paintIcon​(java.awt.Component c, java.awt.Graphics g, int x, int y)  
      void paintIconAnimated​(java.awt.Component c, java.awt.Graphics g, int x, int y, float animatedValue)
      Paints the icon for the given animated value.
      • Methods inherited from interface javax.swing.Icon

        getIconHeight, getIconWidth
    • Method Detail

      • paintIcon

        default void paintIcon​(java.awt.Component c,
                               java.awt.Graphics g,
                               int x,
                               int y)
        Specified by:
        paintIcon in interface javax.swing.Icon
      • paintIconAnimated

        void paintIconAnimated​(java.awt.Component c,
                               java.awt.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​(java.awt.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.
      • getClientPropertyKey

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