AnimatedPainter: support independent animation of multiple values

This commit is contained in:
Karl Tauber
2021-11-22 15:42:47 +01:00
parent ccbf577f46
commit 3b489e8e1a
9 changed files with 325 additions and 122 deletions

View File

@@ -69,7 +69,13 @@ public abstract class FlatAbstractIcon
} }
} }
protected abstract void paintIcon( Component c, Graphics2D g2 ); /**
* Paint the icon at {@code [0,0]} location.
* <p>
* The given graphics context is scaled.
* Use unscaled coordinates, width and height for painting.
*/
protected abstract void paintIcon( Component c, Graphics2D g );
@Override @Override
public int getIconWidth() { public int getIconWidth() {

View File

@@ -21,6 +21,7 @@ import java.awt.Component;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import com.formdev.flatlaf.util.AnimatedIcon; import com.formdev.flatlaf.util.AnimatedIcon;
import com.formdev.flatlaf.util.AnimatedPainter;
/** /**
* Base class for animated icons that scales width and height, creates and initializes * Base class for animated icons that scales width and height, creates and initializes
@@ -30,7 +31,7 @@ import com.formdev.flatlaf.util.AnimatedIcon;
* <p> * <p>
* This class does not store any state information (needed for animation) in its instance. * This class does not store any state information (needed for animation) in its instance.
* Instead a client property is set on the painted component. * Instead a client property is set on the painted component.
* This makes it possible to use a share icon instance for multiple components. * This makes it possible to use a shared icon instance for multiple components.
* *
* @author Karl Tauber * @author Karl Tauber
*/ */
@@ -45,11 +46,34 @@ public abstract class FlatAnimatedIcon
@Override @Override
public void paintIcon( Component c, Graphics g, int x, int y ) { public void paintIcon( Component c, Graphics g, int x, int y ) {
super.paintIcon( c, g, x, y ); super.paintIcon( c, g, x, y );
AnimatedIcon.AnimationSupport.saveIconLocation( this, c, x, y ); AnimatedPainter.saveRepaintLocation( this, c, x, y );
} }
@Override @Override
protected void paintIcon( Component c, Graphics2D g ) { protected void paintIcon( Component c, Graphics2D g ) {
AnimatedIcon.AnimationSupport.paintIcon( this, c, g, 0, 0 ); paintWithAnimation( c, g, 0, 0, getIconWidth(), getIconHeight() );
} }
/**
* Delegates painting to {@link #paintIconAnimated(Component, Graphics2D, float[])}.
* Ignores the given bounds because {@code [x,y]} are always {@code [0,0]} and
* {@code [width,height]} are scaled, but painting code should use unscaled width
* and height because given graphics context is scaled.
*
* @since 2
*/
@Override
public void paintAnimated( Component c, Graphics2D g, int x, int y, int width, int height, float[] animatedValues ) {
paintIconAnimated( c, g, animatedValues );
}
/**
* Paint the icon at {@code 0,0} location.
* <p>
* The given graphics context is scaled.
* Use unscaled coordinates, width and height for painting.
*
* @since 2
*/
protected abstract void paintIconAnimated( Component c, Graphics2D g, float[] animatedValues );
} }

View File

@@ -25,26 +25,27 @@ import javax.swing.border.Border;
/** /**
* Border that automatically animates painting on component value changes. * Border that automatically animates painting on component value changes.
* <p> * <p>
* {@link #getValue(Component)} returns the value of the component. * {@link #getValues(Component)} returns the value(s) of the component.
* If the value changes, then {@link #paintAnimated(Component, Graphics2D, int, int, int, int, float)} * If the value(s) have changed, then {@link #paintAnimated(Component, Graphics2D, int, int, int, int, float[])}
* is invoked multiple times with animated value (from old value to new value). * is invoked multiple times with animated value(s) (from old value(s) to new value(s)).
* If {@link #getValues(Component)} returns multiple values, then each value gets its own independent animation.
* <p> * <p>
* Example for an animated border: * Example for an animated border:
* <pre> * <pre>
* private class AnimatedMinimalTestBorder * private class MyAnimatedBorder
* implements AnimatedBorder * implements AnimatedBorder
* { * {
* &#64;Override * &#64;Override
* public void paintAnimated( Component c, Graphics2D g, int x, int y, int width, int height, float animatedValue ) { * public void paintAnimated( Component c, Graphics2D g, int x, int y, int width, int height, float[] animatedValues ) {
* int lh = UIScale.scale( 2 ); * int lh = UIScale.scale( 2 );
* *
* g.setColor( Color.blue ); * g.setColor( Color.blue );
* g.fillRect( x, y + height - lh, Math.round( width * animatedValue ), lh ); * g.fillRect( x, y + height - lh, Math.round( width * animatedValues[0] ), lh );
* } * }
* *
* &#64;Override * &#64;Override
* public float getValue( Component c ) { * public float[] getValues( Component c ) {
* return c.isFocusOwner() ? 1 : 0; * return new float[] { c.isFocusOwner() ? 1 : 0 };
* } * }
* *
* &#64;Override * &#64;Override
@@ -57,7 +58,7 @@ import javax.swing.border.Border;
* *
* // sample usage * // sample usage
* JTextField textField = new JTextField(); * JTextField textField = new JTextField();
* textField.setBorder( new AnimatedMinimalTestBorder() ); * textField.setBorder( new MyAnimatedBorder() );
* </pre> * </pre>
* *
* Animation works only if the component passed to {@link #paintBorder(Component, Graphics, int, int, int, int)} * Animation works only if the component passed to {@link #paintBorder(Component, Graphics, int, int, int, int)}

View File

@@ -25,37 +25,35 @@ import javax.swing.JComponent;
/** /**
* Icon that automatically animates painting on component value changes. * Icon that automatically animates painting on component value changes.
* <p> * <p>
* {@link #getValue(Component)} returns the value of the component. * {@link #getValues(Component)} returns the value(s) of the component.
* If the value changes, then {@link #paintIconAnimated(Component, Graphics, int, int, float)} * If the value(s) have changed, then {@link #paintAnimated(Component, Graphics2D, int, int, int, int, float[])}
* is invoked multiple times with animated value (from old value to new value). * is invoked multiple times with animated value(s) (from old value(s) to new value(s)).
* If {@link #getValues(Component)} returns multiple values, then each value gets its own independent animation.
* <p> * <p>
* Example for an animated icon: * Example for an animated icon:
* <pre> * <pre>
* private class AnimatedMinimalTestIcon * private class MyAnimatedIcon
* implements AnimatedIcon * implements AnimatedIcon
* { * {
* &#64;Override public int getIconWidth() { return 100; } * &#64;Override public int getIconWidth() { return 100; }
* &#64;Override public int getIconHeight() { return 20; } * &#64;Override public int getIconHeight() { return 20; }
* *
* &#64;Override * &#64;Override
* public void paintIconAnimated( Component c, Graphics g, int x, int y, float animatedValue ) { * public void paintAnimated( Component c, Graphics2D g, int x, int y, int width, int height, float[] animatedValues ) {
* int w = getIconWidth();
* int h = getIconHeight();
*
* g.setColor( Color.red ); * g.setColor( Color.red );
* g.drawRect( x, y, w - 1, h - 1 ); * g.drawRect( x, y, width - 1, height - 1 );
* g.fillRect( x, y, Math.round( w * animatedValue ), h ); * g.fillRect( x, y, Math.round( width * animatedValues[0] ), height );
* } * }
* *
* &#64;Override * &#64;Override
* public float getValue( Component c ) { * public float[] getValues( Component c ) {
* return ((AbstractButton)c).isSelected() ? 1 : 0; * return new float[] { ((AbstractButton)c).isSelected() ? 1 : 0 };
* } * }
* } * }
* *
* // sample usage * // sample usage
* JCheckBox checkBox = new JCheckBox( "test" ); * JCheckBox checkBox = new JCheckBox( "test" );
* checkBox.setIcon( new AnimatedMinimalTestIcon() ); * checkBox.setIcon( new MyAnimatedIcon() );
* </pre> * </pre>
* *
* Animation works only if the component passed to {@link #paintIcon(Component, Graphics, int, int)} * Animation works only if the component passed to {@link #paintIcon(Component, Graphics, int, int)}
@@ -76,15 +74,13 @@ public interface AnimatedIcon
} }
/** /**
* Bridge method that is called from (new) superclass and delegates to * {@inheritDoc}
* {@link #paintIconAnimated(Component, Graphics, int, int, float)}.
* Necessary for API compatibility.
* *
* @since 2 * @since 2
*/ */
@Override @Override
default void paintAnimated( Component c, Graphics2D g, int x, int y, int width, int height, float animatedValue ) { default void paintAnimated( Component c, Graphics2D g, int x, int y, int width, int height, float[] animatedValues ) {
paintIconAnimated( c, g, x, y, animatedValue ); paintIconAnimated( c, g, x, y, animatedValues[0] );
} }
/** /**
@@ -97,22 +93,60 @@ public interface AnimatedIcon
* @param animatedValue the animated value, which is either equal to what {@link #getValue(Component)} * @param animatedValue the animated value, which is either equal to what {@link #getValue(Component)}
* returned, or somewhere between the previous value and the latest value * returned, or somewhere between the previous value and the latest value
* that {@link #getValue(Component)} returned * that {@link #getValue(Component)} returned
*
* @deprecated override {@link #paintAnimated(Component, Graphics2D, int, int, int, int, float[])} instead
*/ */
void paintIconAnimated( Component c, Graphics g, int x, int y, float animatedValue ); @Deprecated
default void paintIconAnimated( Component c, Graphics g, int x, int y, float animatedValue ) {
}
/**
* {@inheritDoc}
*
* @since 2
*/
@Override
default float[] getValues( Component c ) {
return new float[] { getValue( c ) };
}
/**
* Gets the value of the component.
* <p>
* 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.
* <p>
* For a toggle button this could be {@code 0} for off and {@code 1} for on.
*
* @deprecated override {@link #getValues(Component)} instead
*/
@Deprecated
default float getValue( Component c ) {
return 0;
}
//---- class AnimationSupport --------------------------------------------- //---- class AnimationSupport ---------------------------------------------
/** /**
* Animation support. * Animation support.
*/ */
@Deprecated
class AnimationSupport class AnimationSupport
{ {
/**
* @deprecated use {@link AnimatedPainter#paintWithAnimation(Component, Graphics, int, int, int, int)} instead
*/
@Deprecated
public static void paintIcon( AnimatedIcon icon, Component c, Graphics g, int x, int y ) { public static void paintIcon( AnimatedIcon icon, Component c, Graphics g, int x, int y ) {
AnimatedPainterSupport.paint( icon, c, g, x, y, icon.getIconWidth(), icon.getIconHeight() ); AnimatedPainterSupport.paint( icon, c, g, x, y, icon.getIconWidth(), icon.getIconHeight() );
} }
/**
* @deprecated use {@link AnimatedPainter#saveRepaintLocation(AnimatedPainter, Component, int, int)} instead
*/
@Deprecated
public static void saveIconLocation( AnimatedIcon icon, Component c, int x, int y ) { public static void saveIconLocation( AnimatedIcon icon, Component c, int x, int y ) {
AnimatedPainterSupport.saveLocation( icon, c, x, y ); AnimatedPainterSupport.saveRepaintLocation( icon, c, x, y );
} }
} }
} }

View File

@@ -23,11 +23,12 @@ import javax.swing.JComponent;
import com.formdev.flatlaf.util.Animator.Interpolator; import com.formdev.flatlaf.util.Animator.Interpolator;
/** /**
* Painter that automatically animates painting on component value changes. * Painter that automatically animates painting on component value(s) changes.
* <p> * <p>
* {@link #getValue(Component)} returns the value of the component. * {@link #getValues(Component)} returns the value(s) of the component.
* If the value changes, then {@link #paintAnimated(Component, Graphics2D, int, int, int, int, float)} * If the value(s) have changed, then {@link #paintAnimated(Component, Graphics2D, int, int, int, int, float[])}
* is invoked multiple times with animated value (from old value to new value). * is invoked multiple times with animated value(s) (from old value(s) to new value(s)).
* If {@link #getValues(Component)} returns multiple values, then each value gets its own independent animation.
* <p> * <p>
* See {@link AnimatedBorder} or {@link AnimatedIcon} for examples. * See {@link AnimatedBorder} or {@link AnimatedIcon} for examples.
* <p> * <p>
@@ -42,11 +43,11 @@ public interface AnimatedPainter
{ {
/** /**
* Starts painting. * Starts painting.
* Either invokes {@link #paintAnimated(Component, Graphics2D, int, int, int, int, float)} * Either invokes {@link #paintAnimated(Component, Graphics2D, int, int, int, int, float[])}
* once to paint current value (see {@link #getValue(Component)}. Or if value has * once to paint current value(s) (see {@link #getValues(Component)}. Or if value(s) has
* changed, compared to last painting, then it starts an animation and invokes * changed, compared to last painting, then it starts an animation and invokes
* {@link #paintAnimated(Component, Graphics2D, int, int, int, int, float)} * {@link #paintAnimated(Component, Graphics2D, int, int, int, int, float[])}
* multiple times with animated value (from old value to new value). * multiple times with animated value(s) (from old value(s) to new value(s)).
* *
* @param c the component that this painter belongs to * @param c the component that this painter belongs to
* @param g the graphics context * @param g the graphics context
@@ -60,7 +61,7 @@ public interface AnimatedPainter
} }
/** /**
* Paints the given (animated) value. * Paints the given (animated) value(s).
* <p> * <p>
* Invoked from {@link #paintWithAnimation(Component, Graphics, int, int, int, int)}. * Invoked from {@link #paintWithAnimation(Component, Graphics, int, int, int, int)}.
* *
@@ -70,11 +71,11 @@ public interface AnimatedPainter
* @param y the y coordinate of the paint area * @param y the y coordinate of the paint area
* @param width the width of the paint area * @param width the width of the paint area
* @param height the height of the paint area * @param height the height of the paint area
* @param animatedValue the animated value, which is either equal to what {@link #getValue(Component)} * @param animatedValues the animated values, which are either equal to what {@link #getValues(Component)}
* returned, or somewhere between the previous value and the latest value * returned, or somewhere between the previous values and the latest values
* that {@link #getValue(Component)} returned * that {@link #getValues(Component)} returned
*/ */
void paintAnimated( Component c, Graphics2D g, int x, int y, int width, int height, float animatedValue ); void paintAnimated( Component c, Graphics2D g, int x, int y, int width, int height, float[] animatedValues );
/** /**
* Invoked from animator to repaint an area. * Invoked from animator to repaint an area.
@@ -91,14 +92,15 @@ public interface AnimatedPainter
} }
/** /**
* Gets the value of the component. * Gets the value(s) of the component.
* <p> * <p>
* This can be any value and depends on 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. * If the value(s) changes, then this class animates from the old value(s) to the new ones.
* If multiple values are returned, then each value gets its own independent animation.
* <p> * <p>
* For a toggle button this could be {@code 0} for off and {@code 1} for on. * For a toggle button this could be {@code 0} for off and {@code 1} for on.
*/ */
float getValue( Component c ); float[] getValues( Component c );
/** /**
* Returns whether animation is enabled for this painter (default is {@code true}). * Returns whether animation is enabled for this painter (default is {@code true}).
@@ -136,4 +138,14 @@ public interface AnimatedPainter
default Object getClientPropertyKey() { default Object getClientPropertyKey() {
return getClass(); return getClass();
} }
/**
* Saves location for repainting animated area with
* {@link AnimatedPainter#repaintDuringAnimation(Component, int, int, int, int)}.
* Only needed when graphics context passed to {@link #paintWithAnimation(Component, Graphics, int, int, int, int)}
* uses transformed location.
*/
static void saveRepaintLocation( AnimatedPainter painter, Component c, int x, int y ) {
AnimatedPainterSupport.saveRepaintLocation( painter, c, x, y );
}
} }

View File

@@ -49,23 +49,45 @@ class AnimatedPainterSupport
// paint without animation if animation is disabled or // paint without animation if animation is disabled or
// component is not a JComponent and therefore does not support // component is not a JComponent and therefore does not support
// client properties, which are required to keep animation state // client properties, which are required to keep animation state
paintImpl( painter, c, g, x, y, width, height, null ); painter.paintAnimated( c, (Graphics2D) g, x, y, width, height, painter.getValues( c ) );
return; return;
} }
// get component values
float values[] = painter.getValues( c );
JComponent jc = (JComponent) c; JComponent jc = (JComponent) c;
Object key = painter.getClientPropertyKey(); Object key = painter.getClientPropertyKey();
AnimatedPainterSupport as = (AnimatedPainterSupport) jc.getClientProperty( key ); AnimatedPainterSupport[] ass = (AnimatedPainterSupport[]) jc.getClientProperty( key );
if( as == null ) {
// painted first time --> do not animate, but remember current component value
as = new AnimatedPainterSupport();
as.startValue = as.targetValue = as.animatedValue = painter.getValue( c );
jc.putClientProperty( key, as );
} else {
// get component value
float value = painter.getValue( c );
if( value != as.targetValue ) { // check whether length of values array has changed
if( ass != null && ass.length != values.length ) {
// cancel all running animations
for( int i = 0; i < ass.length; i++ ) {
AnimatedPainterSupport as = ass[i];
if( as.animator != null )
as.animator.cancel();
}
ass = null;
}
if( ass == null ) {
ass = new AnimatedPainterSupport[values.length];
jc.putClientProperty( key, ass );
}
float[] animatedValues = new float[ass.length];
for( int i = 0; i < ass.length; i++ ) {
AnimatedPainterSupport as = ass[i];
float value = values[i];
if( as == null ) {
// painted first time --> do not animate, but remember current component value
as = new AnimatedPainterSupport();
as.startValue = as.targetValue = as.animatedValue = value;
ass[i] = as;
} else if( value != as.targetValue ) {
// value changed --> (re)start animation // value changed --> (re)start animation
if( as.animator == null ) { if( as.animator == null ) {
@@ -110,35 +132,33 @@ class AnimatedPainterSupport
as.targetValue = value; as.targetValue = value;
as.animator.start(); as.animator.start();
} }
as.x = x;
as.y = y;
as.width = width;
as.height = height;
animatedValues[i] = as.animatedValue;
} }
as.x = x; painter.paintAnimated( c, (Graphics2D) g, x, y, width, height, animatedValues );
as.y = y;
as.width = width;
as.height = height;
paintImpl( painter, c, g, x, y, width, height, as );
}
private static void paintImpl( AnimatedPainter painter, Component c, Graphics g,
int x, int y, int width, int height, AnimatedPainterSupport as )
{
float value = (as != null) ? as.animatedValue : painter.getValue( c );
painter.paintAnimated( c, (Graphics2D) g, x, y, width, height, value );
} }
private static boolean isAnimationEnabled( AnimatedPainter painter, Component c ) { private static boolean isAnimationEnabled( AnimatedPainter painter, Component c ) {
return Animator.useAnimation() && painter.isAnimationEnabled() && c instanceof JComponent; return Animator.useAnimation() && painter.isAnimationEnabled() && c instanceof JComponent;
} }
static void saveLocation( AnimatedPainter painter, Component c, int x, int y ) { static void saveRepaintLocation( AnimatedPainter painter, Component c, int x, int y ) {
if( !isAnimationEnabled( painter, c ) ) if( !isAnimationEnabled( painter, c ) )
return; return;
AnimatedPainterSupport as = (AnimatedPainterSupport) ((JComponent)c).getClientProperty( painter.getClientPropertyKey() ); AnimatedPainterSupport[] ass = (AnimatedPainterSupport[]) ((JComponent)c).getClientProperty( painter.getClientPropertyKey() );
if( as != null ) { if( ass != null ) {
as.x = x; for( int i = 0; i < ass.length; i++ ) {
as.y = y; AnimatedPainterSupport as = ass[i];
as.x = x;
as.y = y;
}
} }
} }
} }

View File

@@ -217,7 +217,8 @@ public class FlatAnimatedBorderTest
} }
@Override @Override
public void paintAnimated( Component c, Graphics2D g, int x, int y, int width, int height, float animatedValue ) { public void paintAnimated( Component c, Graphics2D g, int x, int y, int width, int height, float[] animatedValues ) {
float animatedValue = animatedValues[0];
FlatUIUtils.setRenderingHints( g ); FlatUIUtils.setRenderingHints( g );
// border width is 1 if not focused and 2 if focused // border width is 1 if not focused and 2 if focused
@@ -242,8 +243,8 @@ public class FlatAnimatedBorderTest
} }
@Override @Override
public float getValue( Component c ) { public float[] getValues( Component c ) {
return FlatUIUtils.isPermanentFocusOwner( c ) ? 1 : 0; return new float[] { FlatUIUtils.isPermanentFocusOwner( c ) ? 1 : 0 };
} }
@Override @Override
@@ -271,7 +272,8 @@ public class FlatAnimatedBorderTest
} }
@Override @Override
public void paintAnimated( Component c, Graphics2D g, int x, int y, int width, int height, float animatedValue ) { public void paintAnimated( Component c, Graphics2D g, int x, int y, int width, int height, float[] animatedValues ) {
float animatedValue = animatedValues[0];
FlatUIUtils.setRenderingHints( g ); FlatUIUtils.setRenderingHints( g );
// use paintAtScale1x() for consistent line thickness when scaled // use paintAtScale1x() for consistent line thickness when scaled
@@ -312,8 +314,8 @@ public class FlatAnimatedBorderTest
} }
@Override @Override
public float getValue( Component c ) { public float[] getValues( Component c ) {
return FlatUIUtils.isPermanentFocusOwner( c ) ? 1 : 0; return new float[] { FlatUIUtils.isPermanentFocusOwner( c ) ? 1 : 0 };
} }
@Override @Override
@@ -338,9 +340,10 @@ public class FlatAnimatedBorderTest
private static final float LABEL_FONT_SCALE = 0.75f; private static final float LABEL_FONT_SCALE = 0.75f;
@Override @Override
public void paintAnimated( Component c, Graphics2D g, int x, int y, int width, int height, float animatedValue ) { public void paintAnimated( Component c, Graphics2D g, int x, int y, int width, int height, float[] animatedValues ) {
super.paintAnimated( c, g, x, y, width, height, animatedValue ); super.paintAnimated( c, g, x, y, width, height, animatedValues );
float animatedValue = animatedValues[0];
JComponent jc = (JComponent) c; JComponent jc = (JComponent) c;
String label = (String) jc.getClientProperty( LABEL_TEXT_KEY ); String label = (String) jc.getClientProperty( LABEL_TEXT_KEY );
if( label == null ) if( label == null )
@@ -398,7 +401,8 @@ public class FlatAnimatedBorderTest
implements AnimatedBorder implements AnimatedBorder
{ {
@Override @Override
public void paintAnimated( Component c, Graphics2D g, int x, int y, int width, int height, float animatedValue ) { public void paintAnimated( Component c, Graphics2D g, int x, int y, int width, int height, float[] animatedValues ) {
float animatedValue = animatedValues[0];
int lh = UIScale.scale( 2 ); int lh = UIScale.scale( 2 );
g.setColor( Color.blue ); g.setColor( Color.blue );
@@ -411,8 +415,8 @@ public class FlatAnimatedBorderTest
} }
@Override @Override
public float getValue( Component c ) { public float[] getValues( Component c ) {
return FlatUIUtils.isPermanentFocusOwner( c ) ? 1 : 0; return new float[] { FlatUIUtils.isPermanentFocusOwner( c ) ? 1 : 0 };
} }
@Override @Override

View File

@@ -18,13 +18,14 @@ package com.formdev.flatlaf.testing;
import java.awt.Color; import java.awt.Color;
import java.awt.Component; import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D; import java.awt.geom.Ellipse2D;
import java.awt.geom.Path2D;
import javax.swing.*; import javax.swing.*;
import com.formdev.flatlaf.icons.FlatAnimatedIcon; import com.formdev.flatlaf.icons.FlatAnimatedIcon;
import com.formdev.flatlaf.util.AnimatedIcon; import com.formdev.flatlaf.util.AnimatedIcon;
import com.formdev.flatlaf.util.ColorFunctions; import com.formdev.flatlaf.util.ColorFunctions;
import com.formdev.flatlaf.util.UIScale;
import net.miginfocom.swing.*; import net.miginfocom.swing.*;
/** /**
@@ -38,6 +39,7 @@ public class FlatAnimatedIconTest
private static final Color CHART_RADIO_BUTTON_3 = Color.green; private static final Color CHART_RADIO_BUTTON_3 = Color.green;
private static final Color CHART_CHECK_BOX_1 = Color.magenta; private static final Color CHART_CHECK_BOX_1 = Color.magenta;
private static final Color CHART_CHECK_BOX_2 = Color.orange; private static final Color CHART_CHECK_BOX_2 = Color.orange;
private static final Color[] CHART_SWITCH_EX = new Color[] { Color.red, Color.green, Color.blue };
private static final String CHART_COLOR_KEY = "chartColor"; private static final String CHART_COLOR_KEY = "chartColor";
@@ -57,6 +59,7 @@ public class FlatAnimatedIconTest
radioButton3.setIcon( radioIcon ); radioButton3.setIcon( radioIcon );
checkBox1.setIcon( new AnimatedSwitchIcon() ); checkBox1.setIcon( new AnimatedSwitchIcon() );
checkBox3.setIcon( new AnimatedSwitchIconEx() );
checkBox2.setIcon( new AnimatedMinimalTestIcon() ); checkBox2.setIcon( new AnimatedMinimalTestIcon() );
radioButton1.putClientProperty( CHART_COLOR_KEY, CHART_RADIO_BUTTON_1 ); radioButton1.putClientProperty( CHART_COLOR_KEY, CHART_RADIO_BUTTON_1 );
@@ -83,6 +86,7 @@ public class FlatAnimatedIconTest
radioButton3ChartColor = new FlatAnimatorTest.JChartColor(); radioButton3ChartColor = new FlatAnimatorTest.JChartColor();
checkBox1 = new JCheckBox(); checkBox1 = new JCheckBox();
checkBox1ChartColor = new FlatAnimatorTest.JChartColor(); checkBox1ChartColor = new FlatAnimatorTest.JChartColor();
checkBox3 = new JCheckBox();
checkBox2 = new JCheckBox(); checkBox2 = new JCheckBox();
checkBox2ChartColor = new FlatAnimatorTest.JChartColor(); checkBox2ChartColor = new FlatAnimatorTest.JChartColor();
durationLabel = new JLabel(); durationLabel = new JLabel();
@@ -101,6 +105,7 @@ public class FlatAnimatedIconTest
"[]para" + "[]para" +
"[]" + "[]" +
"[]" + "[]" +
"[]" +
"[grow]" + "[grow]" +
"[]")); "[]"));
@@ -109,7 +114,7 @@ public class FlatAnimatedIconTest
radioButton1.setSelected(true); radioButton1.setSelected(true);
add(radioButton1, "cell 0 0"); add(radioButton1, "cell 0 0");
add(radioButton1ChartColor, "cell 1 0"); add(radioButton1ChartColor, "cell 1 0");
add(lineChartPanel, "cell 2 0 1 6"); add(lineChartPanel, "cell 2 0 1 7");
//---- radioButton2 ---- //---- radioButton2 ----
radioButton2.setText("radio 2"); radioButton2.setText("radio 2");
@@ -126,18 +131,22 @@ public class FlatAnimatedIconTest
add(checkBox1, "cell 0 3"); add(checkBox1, "cell 0 3");
add(checkBox1ChartColor, "cell 1 3"); add(checkBox1ChartColor, "cell 1 3");
//---- checkBox3 ----
checkBox3.setText("switch ex");
add(checkBox3, "cell 0 4");
//---- checkBox2 ---- //---- checkBox2 ----
checkBox2.setText("minimal"); checkBox2.setText("minimal");
add(checkBox2, "cell 0 4"); add(checkBox2, "cell 0 5");
add(checkBox2ChartColor, "cell 1 4"); add(checkBox2ChartColor, "cell 1 5");
//---- durationLabel ---- //---- durationLabel ----
durationLabel.setText("Duration:"); durationLabel.setText("Duration:");
add(durationLabel, "cell 0 6 3 1"); add(durationLabel, "cell 0 7 3 1");
//---- durationField ---- //---- durationField ----
durationField.setModel(new SpinnerNumberModel(200, 100, null, 50)); durationField.setModel(new SpinnerNumberModel(200, 100, null, 50));
add(durationField, "cell 0 6 3 1"); add(durationField, "cell 0 7 3 1");
//---- buttonGroup1 ---- //---- buttonGroup1 ----
ButtonGroup buttonGroup1 = new ButtonGroup(); ButtonGroup buttonGroup1 = new ButtonGroup();
@@ -157,6 +166,7 @@ public class FlatAnimatedIconTest
private FlatAnimatorTest.JChartColor radioButton3ChartColor; private FlatAnimatorTest.JChartColor radioButton3ChartColor;
private JCheckBox checkBox1; private JCheckBox checkBox1;
private FlatAnimatorTest.JChartColor checkBox1ChartColor; private FlatAnimatorTest.JChartColor checkBox1ChartColor;
private JCheckBox checkBox3;
private JCheckBox checkBox2; private JCheckBox checkBox2;
private FlatAnimatorTest.JChartColor checkBox2ChartColor; private FlatAnimatorTest.JChartColor checkBox2ChartColor;
private JLabel durationLabel; private JLabel durationLabel;
@@ -185,7 +195,8 @@ public class FlatAnimatedIconTest
} }
@Override @Override
public void paintIconAnimated( Component c, Graphics g, int x, int y, float animatedValue ) { public void paintIconAnimated( Component c, Graphics2D g, float[] animatedValues ) {
float animatedValue = animatedValues[0];
Color color = ColorFunctions.mix( onColor, offColor, animatedValue ); Color color = ColorFunctions.mix( onColor, offColor, animatedValue );
// border // border
@@ -201,7 +212,7 @@ public class FlatAnimatedIconTest
float dotDiameter = DOT_SIZE * animatedValue; float dotDiameter = DOT_SIZE * animatedValue;
float xy = (SIZE - dotDiameter) / 2f; float xy = (SIZE - dotDiameter) / 2f;
g.setColor( color ); g.setColor( color );
((Graphics2D)g).fill( new Ellipse2D.Float( xy, xy, dotDiameter, dotDiameter ) ); g.fill( new Ellipse2D.Float( xy, xy, dotDiameter, dotDiameter ) );
if( animatedValue != 0 && animatedValue != 1 ) { if( animatedValue != 0 && animatedValue != 1 ) {
Color chartColor = (Color) ((JComponent)c).getClientProperty( CHART_COLOR_KEY ); Color chartColor = (Color) ((JComponent)c).getClientProperty( CHART_COLOR_KEY );
@@ -210,8 +221,8 @@ public class FlatAnimatedIconTest
} }
@Override @Override
public float getValue( Component c ) { public float[] getValues( Component c ) {
return ((JRadioButton)c).isSelected() ? 1 : 0; return new float[] { ((JRadioButton)c).isSelected() ? 1 : 0 };
} }
@Override @Override
@@ -222,7 +233,7 @@ public class FlatAnimatedIconTest
//---- class AnimatedSwitchIcon ------------------------------------------- //---- class AnimatedSwitchIcon -------------------------------------------
public class AnimatedSwitchIcon private class AnimatedSwitchIcon
extends FlatAnimatedIcon extends FlatAnimatedIcon
{ {
private final Color offColor = Color.lightGray; private final Color offColor = Color.lightGray;
@@ -233,17 +244,20 @@ public class FlatAnimatedIconTest
} }
@Override @Override
public void paintIconAnimated( Component c, Graphics g, int x, int y, float animatedValue ) { public void paintIconAnimated( Component c, Graphics2D g, float[] animatedValues ) {
float animatedValue = animatedValues[0];
Color color = ColorFunctions.mix( onColor, offColor, animatedValue ); Color color = ColorFunctions.mix( onColor, offColor, animatedValue );
// paint track
g.setColor( color ); g.setColor( color );
g.fillRoundRect( x, y, width, height, height, height ); g.fillRoundRect( 0, 0, width, height, height, height );
// paint thumb
int thumbSize = height - 4; int thumbSize = height - 4;
float thumbX = x + 2 + ((width - 4 - thumbSize) * animatedValue); float thumbX = 2 + ((width - 4 - thumbSize) * animatedValue);
int thumbY = y + 2; int thumbY = 2;
g.setColor( Color.white ); g.setColor( Color.white );
((Graphics2D)g).fill( new Ellipse2D.Float( thumbX, thumbY, thumbSize, thumbSize ) ); g.fill( new Ellipse2D.Float( thumbX, thumbY, thumbSize, thumbSize ) );
if( animatedValue != 0 && animatedValue != 1 ) { if( animatedValue != 0 && animatedValue != 1 ) {
Color chartColor = (Color) ((JComponent)c).getClientProperty( CHART_COLOR_KEY ); Color chartColor = (Color) ((JComponent)c).getClientProperty( CHART_COLOR_KEY );
@@ -252,8 +266,91 @@ public class FlatAnimatedIconTest
} }
@Override @Override
public float getValue( Component c ) { public float[] getValues( Component c ) {
return ((AbstractButton)c).isSelected() ? 1 : 0; return new float[] { ((AbstractButton)c).isSelected() ? 1 : 0 };
}
@Override
public int getAnimationDuration() {
return (Integer) durationField.getValue();
}
}
//---- class AnimatedSwitchIconEx -----------------------------------------
private static final int HW = 8;
private class AnimatedSwitchIconEx
extends FlatAnimatedIcon
{
private final Color offColor = Color.lightGray;
private final Color onColor = Color.red;
private final Color hoverColor = new Color( 0x4400cc00, true );
private final Color pressedColor = new Color( 0x440000cc, true );
public AnimatedSwitchIconEx() {
super( 28 + HW, 16 + HW, null );
}
@Override
public void paintIconAnimated( Component c, Graphics2D g, float[] animatedValues ) {
Color color = ColorFunctions.mix( onColor, offColor, animatedValues[0] );
int hw2 = HW / 2;
int x = hw2;
int y = hw2;
int width = this.width - HW;
int height = this.height - HW;
// paint track
g.setColor( color );
g.fillRoundRect( x, y, width, height, height, height );
// paint thumb
int thumbSize = height - 4;
float thumbX = x + 2 + ((width - 4 - thumbSize) * animatedValues[0]);
int thumbY = y + 2;
g.setColor( Color.white );
g.fill( new Ellipse2D.Float( thumbX, thumbY, thumbSize, thumbSize ) );
// paint hover
if( animatedValues[1] > 0 ) {
g.setColor( hoverColor );
paintHoverOrPressed( g, thumbX, thumbY, thumbSize, animatedValues[1] );
}
// paint pressed
if( animatedValues[2] > 0 ) {
g.setColor( pressedColor );
paintHoverOrPressed( g, thumbX, thumbY, thumbSize, animatedValues[2] );
}
for( int i = 0; i < animatedValues.length; i++ ) {
float animatedValue = animatedValues[i];
if( animatedValue != 0 && animatedValue != 1 )
lineChartPanel.lineChart.addValue( animatedValue, CHART_SWITCH_EX[i] );
}
}
private void paintHoverOrPressed( Graphics2D g, float thumbX, int thumbY, int thumbSize, float animatedValue ) {
float hw = (HW + 4) * animatedValue;
Path2D path = new Path2D.Float( Path2D.WIND_EVEN_ODD );
path.append( new Ellipse2D.Float( thumbX - (hw / 2), thumbY - (hw / 2),
thumbSize + hw, thumbSize + hw ), false );
path.append( new Ellipse2D.Float( thumbX, thumbY, thumbSize, thumbSize ), false );
g.fill( path );
}
@Override
public float[] getValues( Component c ) {
AbstractButton b = (AbstractButton) c;
ButtonModel bm = b.getModel();
return new float[] {
b.isSelected() ? 1 : 0,
bm.isRollover() ? 1 : 0,
bm.isPressed() ? 1 : 0,
};
} }
@Override @Override
@@ -272,22 +369,21 @@ public class FlatAnimatedIconTest
{ {
@Override @Override
public int getIconWidth() { public int getIconWidth() {
return 100; return UIScale.scale( 50 );
} }
@Override @Override
public int getIconHeight() { public int getIconHeight() {
return 20; return UIScale.scale( 16 );
} }
@Override @Override
public void paintIconAnimated( Component c, Graphics g, int x, int y, float animatedValue ) { public void paintAnimated( Component c, Graphics2D g, int x, int y, int width, int height, float[] animatedValues ) {
int w = getIconWidth(); float animatedValue = animatedValues[0];
int h = getIconHeight();
g.setColor( Color.red ); g.setColor( Color.red );
g.drawRect( x, y, w - 1, h - 1 ); g.drawRect( x, y, width - 1, height - 1 );
g.fillRect( x, y, Math.round( w * animatedValue ), h ); g.fillRect( x, y, Math.round( width * animatedValue ), height );
if( animatedValue != 0 && animatedValue != 1 ) { if( animatedValue != 0 && animatedValue != 1 ) {
Color chartColor = (Color) ((JComponent)c).getClientProperty( CHART_COLOR_KEY ); Color chartColor = (Color) ((JComponent)c).getClientProperty( CHART_COLOR_KEY );
@@ -296,8 +392,8 @@ public class FlatAnimatedIconTest
} }
@Override @Override
public float getValue( Component c ) { public float[] getValues( Component c ) {
return ((AbstractButton)c).isSelected() ? 1 : 0; return new float[] { ((AbstractButton)c).isSelected() ? 1 : 0 };
} }
@Override @Override

View File

@@ -6,7 +6,7 @@ new FormModel {
add( new FormContainer( "com.formdev.flatlaf.testing.FlatTestPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) { add( new FormContainer( "com.formdev.flatlaf.testing.FlatTestPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
"$layoutConstraints": "insets dialog,hidemode 3" "$layoutConstraints": "insets dialog,hidemode 3"
"$columnConstraints": "[][fill]para[grow,fill]" "$columnConstraints": "[][fill]para[grow,fill]"
"$rowConstraints": "[][][]para[][][grow][]" "$rowConstraints": "[][][]para[][][][grow][]"
} ) { } ) {
name: "this" name: "this"
add( new FormComponent( "javax.swing.JRadioButton" ) { add( new FormComponent( "javax.swing.JRadioButton" ) {
@@ -25,7 +25,7 @@ new FormModel {
add( new FormComponent( "com.formdev.flatlaf.testing.FlatAnimatorTest$LineChartPanel" ) { add( new FormComponent( "com.formdev.flatlaf.testing.FlatAnimatorTest$LineChartPanel" ) {
name: "lineChartPanel" name: "lineChartPanel"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 2 0 1 6" "value": "cell 2 0 1 7"
} ) } )
add( new FormComponent( "javax.swing.JRadioButton" ) { add( new FormComponent( "javax.swing.JRadioButton" ) {
name: "radioButton2" name: "radioButton2"
@@ -62,22 +62,28 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 3" "value": "cell 1 3"
} ) } )
add( new FormComponent( "javax.swing.JCheckBox" ) {
name: "checkBox3"
"text": "switch ex"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 4"
} )
add( new FormComponent( "javax.swing.JCheckBox" ) { add( new FormComponent( "javax.swing.JCheckBox" ) {
name: "checkBox2" name: "checkBox2"
"text": "minimal" "text": "minimal"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 4" "value": "cell 0 5"
} ) } )
add( new FormComponent( "com.formdev.flatlaf.testing.FlatAnimatorTest$JChartColor" ) { add( new FormComponent( "com.formdev.flatlaf.testing.FlatAnimatorTest$JChartColor" ) {
name: "checkBox2ChartColor" name: "checkBox2ChartColor"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 4" "value": "cell 1 5"
} ) } )
add( new FormComponent( "javax.swing.JLabel" ) { add( new FormComponent( "javax.swing.JLabel" ) {
name: "durationLabel" name: "durationLabel"
"text": "Duration:" "text": "Duration:"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 6 3 1" "value": "cell 0 7 3 1"
} ) } )
add( new FormComponent( "javax.swing.JSpinner" ) { add( new FormComponent( "javax.swing.JSpinner" ) {
name: "durationField" name: "durationField"
@@ -87,7 +93,7 @@ new FormModel {
value: 200 value: 200
} }
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 6 3 1" "value": "cell 0 7 3 1"
} ) } )
}, new FormLayoutConstraints( null ) { }, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 0 ) "location": new java.awt.Point( 0, 0 )