FlatLaf icon indicating copy to clipboard operation
FlatLaf copied to clipboard

AnimatedBorder

Open DevCharly opened this issue 4 years ago • 0 comments

This PR adds interface AnimatedBorder (extends javax.swing.border.Border) that automatically animates painting on component value changes.

This is similar to AnimatedIcon (see PR #222)

The cool thing is that the animation is done in the border and it is not necessary to change anything in the UI delegates. This is not yet used, but intended for future animates (issue #66).

The test application FlatAnimatedBorderTest includes (experimental) animated borders for text fields and a minimal example:

animated-border-200

Same as above but using 1000ms duration to better see the animation:

animated-border-1000

Here is the code for a minimal example:

private class AnimatedMinimalTestBorder
    implements AnimatedBorder
{
    @Override
    public void paintBorderAnimated( Component c, Graphics g, int x, int y, int width, int height, float animatedValue ) {
        int lh = UIScale.scale( 2 );

        g.setColor( Color.blue );
        g.fillRect( x, y + height - lh, Math.round( width * animatedValue ), lh );
    }

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

    @Override
    public Insets getBorderInsets( Component c ) {
        return UIScale.scale( new Insets( 4, 4, 4, 4 ) );
    }

    @Override public boolean isBorderOpaque() { return false; }
}

// sample usage
JTextField textField = new JTextField();
textField.setBorder( new AnimatedMinimalTestBorder() );

DevCharly avatar Jul 14 '21 13:07 DevCharly