vito
1 year ago
4 changed files with 103 additions and 23 deletions
@ -0,0 +1,75 @@
|
||||
package com.fine.theme.icon.icons; |
||||
|
||||
import com.formdev.flatlaf.icons.FlatAnimatedIcon; |
||||
import com.formdev.flatlaf.util.ColorFunctions; |
||||
import org.jetbrains.annotations.Nullable; |
||||
|
||||
import javax.swing.AbstractButton; |
||||
import javax.swing.ButtonModel; |
||||
import javax.swing.JRadioButton; |
||||
import javax.swing.UIManager; |
||||
import java.awt.Color; |
||||
import java.awt.Component; |
||||
import java.awt.Graphics; |
||||
import java.awt.Graphics2D; |
||||
import java.awt.geom.Ellipse2D; |
||||
|
||||
/** |
||||
* RadioButton 图标,带动画 |
||||
* |
||||
* @author vito |
||||
* @since 11.0 |
||||
* Created on 2023/11/22 |
||||
*/ |
||||
public class AnimatedRadioButtonIcon |
||||
extends FlatAnimatedIcon { |
||||
private static final int SIZE = 16; |
||||
private static final int BORDER_SIZE = 2; |
||||
private static final int ON_SIZE = 8; |
||||
|
||||
private final Color offColor = UIManager.getColor("CheckBox.icon.borderColor"); |
||||
private final Color onColor = UIManager.getColor("CheckBox.icon.checkmarkColor"); |
||||
|
||||
public AnimatedRadioButtonIcon() { |
||||
super(SIZE, SIZE, null); |
||||
} |
||||
|
||||
@Override |
||||
public void paintIconAnimated(Component c, Graphics g, int x, int y, float animatedValue) { |
||||
Color color = ColorFunctions.mix(onColor, offColor, animatedValue); |
||||
|
||||
// border
|
||||
g.setColor(getBorderColor(c, color, onColor)); |
||||
g.fillOval(0, 0, SIZE, SIZE); |
||||
|
||||
// background
|
||||
g.setColor(c.getBackground()); |
||||
float onDiameter = SIZE - (BORDER_SIZE + (ON_SIZE - BORDER_SIZE) * (animatedValue)); |
||||
float xy = (SIZE - onDiameter) / 2f; |
||||
((Graphics2D) g).fill(new Ellipse2D.Float(xy, xy, onDiameter, onDiameter)); |
||||
|
||||
} |
||||
|
||||
@Nullable |
||||
private Color getBorderColor(Component c, Color enableColor, Color hoverColor) { |
||||
if (c instanceof AbstractButton) { |
||||
ButtonModel model = ((AbstractButton) c).getModel(); |
||||
|
||||
if (model.isRollover()) { |
||||
return hoverColor; |
||||
} |
||||
} |
||||
return enableColor; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public float getValue(Component c) { |
||||
return ((JRadioButton) c).isSelected() ? 1 : 0; |
||||
} |
||||
|
||||
@Override |
||||
public int getAnimationDuration() { |
||||
return 200; |
||||
} |
||||
} |
Loading…
Reference in new issue