CheckBox and RadioButton: the icon size no longer includes the focusWidth and paints the outline focus border outside of the icon; this change makes sure that the check/radio icons are painted at the same position independent of the focusWidth

This commit is contained in:
Karl Tauber
2019-09-15 23:05:15 +02:00
parent eff9c50571
commit 89b6a4e453
2 changed files with 22 additions and 23 deletions

View File

@@ -16,7 +16,6 @@
package com.formdev.flatlaf.icons; package com.formdev.flatlaf.icons;
import static com.formdev.flatlaf.util.UIScale.*;
import java.awt.BasicStroke; import java.awt.BasicStroke;
import java.awt.Color; import java.awt.Color;
import java.awt.Component; import java.awt.Component;
@@ -29,6 +28,10 @@ import com.formdev.flatlaf.ui.FlatButtonUI;
/** /**
* Icon for {@link javax.swing.JCheckBox}. * Icon for {@link javax.swing.JCheckBox}.
* *
* Note: If Component.focusWidth is greater than zero, then the outline focus border
* is painted outside of the icon bounds. Make sure that the checkbox
* has margins, which are equal or greater than focusWidth.
*
* @uiDefault Component.focusWidth int * @uiDefault Component.focusWidth int
* @uiDefault Component.focusColor Color * @uiDefault Component.focusColor Color
* @uiDefault CheckBox.icon.borderColor Color * @uiDefault CheckBox.icon.borderColor Color
@@ -73,10 +76,10 @@ public class FlatCheckBoxIcon
protected final Color checkmarkColor = UIManager.getColor( "CheckBox.icon.checkmarkColor" ); protected final Color checkmarkColor = UIManager.getColor( "CheckBox.icon.checkmarkColor" );
protected final Color disabledCheckmarkColor = UIManager.getColor( "CheckBox.icon.disabledCheckmarkColor" ); protected final Color disabledCheckmarkColor = UIManager.getColor( "CheckBox.icon.disabledCheckmarkColor" );
protected final int iconSize = 15 + (focusWidth * 2); static final int ICON_SIZE = 15;
public FlatCheckBoxIcon() { public FlatCheckBoxIcon() {
super( 0, 0, null ); super( ICON_SIZE, ICON_SIZE, null );
} }
@Override @Override
@@ -84,7 +87,7 @@ public class FlatCheckBoxIcon
boolean selected = (c instanceof AbstractButton) ? ((AbstractButton)c).isSelected() : false; boolean selected = (c instanceof AbstractButton) ? ((AbstractButton)c).isSelected() : false;
// paint focused border // paint focused border
if( c.hasFocus() ) { if( c.hasFocus() && focusWidth > 0 ) {
g2.setColor( focusColor ); g2.setColor( focusColor );
paintFocusBorder( g2 ); paintFocusBorder( g2 );
} }
@@ -115,15 +118,17 @@ public class FlatCheckBoxIcon
} }
protected void paintFocusBorder( Graphics2D g2 ) { protected void paintFocusBorder( Graphics2D g2 ) {
g2.fillRoundRect( 1, 0, iconSize - 1, iconSize - 1, 8, 8 ); // the outline focus border is painted outside of the icon
int wh = ICON_SIZE - 1 + (focusWidth * 2);
g2.fillRoundRect( -focusWidth + 1, -focusWidth, wh, wh, 8, 8 );
} }
protected void paintBorder( Graphics2D g2 ) { protected void paintBorder( Graphics2D g2 ) {
g2.fillRoundRect( focusWidth + 1, focusWidth, 14, 14, 4, 4 ); g2.fillRoundRect( 1, 0, 14, 14, 4, 4 );
} }
protected void paintBackground( Graphics2D g2 ) { protected void paintBackground( Graphics2D g2 ) {
g2.fillRoundRect( focusWidth + 2, focusWidth + 1, 12, 12, 4, 4 ); g2.fillRoundRect( 2, 1, 12, 12, 4, 4 );
} }
protected void paintCheckmark( Graphics2D g2 ) { protected void paintCheckmark( Graphics2D g2 ) {
@@ -132,19 +137,7 @@ public class FlatCheckBoxIcon
path.lineTo( 6.6f, 10f ); path.lineTo( 6.6f, 10f );
path.lineTo( 11.25f, 3.5f ); path.lineTo( 11.25f, 3.5f );
g2.translate( focusWidth, focusWidth );
g2.setStroke( new BasicStroke( 1.9f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND ) ); g2.setStroke( new BasicStroke( 1.9f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND ) );
g2.draw( path ); g2.draw( path );
g2.translate( -focusWidth, -focusWidth );
}
@Override
public int getIconWidth() {
return scale( iconSize );
}
@Override
public int getIconHeight() {
return scale( iconSize );
} }
} }

View File

@@ -23,6 +23,10 @@ import com.formdev.flatlaf.ui.FlatUIUtils;
/** /**
* Icon for {@link javax.swing.JRadioButton}. * Icon for {@link javax.swing.JRadioButton}.
* *
* Note: If Component.focusWidth is greater than zero, then the outline focus border
* is painted outside of the icon bounds. Make sure that the radiobutton
* has margins, which are equal or greater than focusWidth.
*
* @uiDefault RadioButton.icon.centerDiameter int * @uiDefault RadioButton.icon.centerDiameter int
* *
* @author Karl Tauber * @author Karl Tauber
@@ -34,22 +38,24 @@ public class FlatRadioButtonIcon
@Override @Override
protected void paintFocusBorder( Graphics2D g2 ) { protected void paintFocusBorder( Graphics2D g2 ) {
g2.fillOval( 0, 0, iconSize, iconSize ); // the outline focus border is painted outside of the icon
int wh = ICON_SIZE + (focusWidth * 2);
g2.fillOval( -focusWidth, -focusWidth, wh, wh );
} }
@Override @Override
protected void paintBorder( Graphics2D g2 ) { protected void paintBorder( Graphics2D g2 ) {
g2.fillOval( focusWidth, focusWidth, 15, 15 ); g2.fillOval( 0, 0, 15, 15 );
} }
@Override @Override
protected void paintBackground( Graphics2D g2 ) { protected void paintBackground( Graphics2D g2 ) {
g2.fillOval( focusWidth + 1, focusWidth + 1, 13, 13 ); g2.fillOval( 1, 1, 13, 13 );
} }
@Override @Override
protected void paintCheckmark( Graphics2D g2 ) { protected void paintCheckmark( Graphics2D g2 ) {
float xy = (iconSize - centerDiameter) / 2f; float xy = (ICON_SIZE - centerDiameter) / 2f;
g2.fill( new Ellipse2D.Float( xy, xy, centerDiameter, centerDiameter ) ); g2.fill( new Ellipse2D.Float( xy, xy, centerDiameter, centerDiameter ) );
} }
} }