AnimatedBorder:

- support repainting only necessary region while animating
- use AbstractBorder in test app and fixed insets
This commit is contained in:
Karl Tauber
2021-07-14 14:41:35 +02:00
parent b2245e2246
commit e4fa2e28ea
2 changed files with 41 additions and 5 deletions

View File

@@ -65,6 +65,7 @@ import com.formdev.flatlaf.util.Animator.Interpolator;
* A client property is set on the component to store the animation state. * A client property is set on the component to store the animation state.
* *
* @author Karl Tauber * @author Karl Tauber
* @since 1.5
*/ */
public interface AnimatedBorder public interface AnimatedBorder
extends Border extends Border
@@ -89,6 +90,20 @@ public interface AnimatedBorder
*/ */
void paintBorderAnimated( Component c, Graphics g, int x, int y, int width, int height, float animatedValue ); void paintBorderAnimated( Component c, Graphics g, int x, int y, int width, int height, float animatedValue );
/**
* Repaint the animated part of the border.
* <p>
* Useful to limit the repaint region. E.g. if only the bottom border is animated.
* If more than one border side is animated (e.g. bottom and right side), then it
* makes no sense to do separate repaints because the Swing repaint manager unions
* the regions and the whole component is repainted.
* <p>
* The default implementation repaints the whole component.
*/
default void repaintBorder( Component c, int x, int y, int width, int height ) {
c.repaint( x, y, width, height );
}
/** /**
* Gets the value of the component. * Gets the value of the component.
* <p> * <p>
@@ -197,7 +212,7 @@ public interface AnimatedBorder
as2.fraction = fraction; as2.fraction = fraction;
// repaint border // repaint border
c.repaint( as2.x, as2.y, as2.width, as2.height ); border.repaintBorder( c, as2.x, as2.y, as2.width, as2.height );
}, () -> { }, () -> {
as2.startValue = as2.animatedValue = as2.targetValue; as2.startValue = as2.animatedValue = as2.targetValue;
as2.animator = null; as2.animator = null;

View File

@@ -23,7 +23,7 @@ import java.awt.Graphics2D;
import java.awt.Insets; import java.awt.Insets;
import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D;
import javax.swing.*; import javax.swing.*;
import com.formdev.flatlaf.ui.FlatMarginBorder; import javax.swing.border.AbstractBorder;
import com.formdev.flatlaf.ui.FlatUIUtils; import com.formdev.flatlaf.ui.FlatUIUtils;
import com.formdev.flatlaf.util.AnimatedBorder; import com.formdev.flatlaf.util.AnimatedBorder;
import com.formdev.flatlaf.util.ColorFunctions; import com.formdev.flatlaf.util.ColorFunctions;
@@ -133,7 +133,7 @@ public class FlatAnimatedBorderTest
* - animates focus indicator color and border width * - animates focus indicator color and border width
*/ */
private class AnimatedFocusFadeBorder private class AnimatedFocusFadeBorder
extends FlatMarginBorder extends AbstractBorder
implements AnimatedBorder implements AnimatedBorder
{ {
// needed because otherwise the empty paint method in superclass // needed because otherwise the empty paint method in superclass
@@ -155,6 +155,13 @@ public class FlatAnimatedBorderTest
FlatUIUtils.paintComponentBorder( (Graphics2D) g, x, y, width, height, 0, lw, 0 ); FlatUIUtils.paintComponentBorder( (Graphics2D) g, x, y, width, height, 0, lw, 0 );
} }
@Override
public Insets getBorderInsets( Component c, Insets insets ) {
insets.top = insets.bottom = UIScale.scale( 3 );
insets.left = insets.right = UIScale.scale( 7 );
return insets;
}
@Override @Override
public float getValue( Component c ) { public float getValue( Component c ) {
return FlatUIUtils.isPermanentFocusOwner( c ) ? 1 : 0; return FlatUIUtils.isPermanentFocusOwner( c ) ? 1 : 0;
@@ -174,7 +181,7 @@ public class FlatAnimatedBorderTest
* - animates focus indicator at bottom * - animates focus indicator at bottom
*/ */
private class AnimatedMaterialBorder private class AnimatedMaterialBorder
extends FlatMarginBorder extends AbstractBorder
implements AnimatedBorder implements AnimatedBorder
{ {
// needed because otherwise the empty paint method in superclass // needed because otherwise the empty paint method in superclass
@@ -206,6 +213,20 @@ public class FlatAnimatedBorderTest
} ); } );
} }
@Override
public void repaintBorder( Component c, int x, int y, int width, int height ) {
// limit repaint to bottom border
int lh = UIScale.scale( 2 );
c.repaint( x, y + height - lh, width, lh );
}
@Override
public Insets getBorderInsets( Component c, Insets insets ) {
insets.top = insets.bottom = UIScale.scale( 3 );
insets.left = insets.right = UIScale.scale( 7 );
return insets;
}
@Override @Override
public float getValue( Component c ) { public float getValue( Component c ) {
return FlatUIUtils.isPermanentFocusOwner( c ) ? 1 : 0; return FlatUIUtils.isPermanentFocusOwner( c ) ? 1 : 0;
@@ -245,7 +266,7 @@ public class FlatAnimatedBorderTest
@Override @Override
public Insets getBorderInsets( Component c ) { public Insets getBorderInsets( Component c ) {
return UIScale.scale( new Insets( 4, 4, 4, 4 ) ); return UIScale.scale( new Insets( 3, 7, 3, 7 ) );
} }
@Override @Override