FlatBorder: moved scaling from getter methods to paintBorder() and getBorderInsets()

This commit is contained in:
Karl Tauber
2020-05-14 23:35:11 +02:00
parent 3bbc9517af
commit 578d445ecb
4 changed files with 37 additions and 25 deletions

View File

@@ -73,16 +73,16 @@ public class FlatBorder
FlatUIUtils.setRenderingHints( g2 ); FlatUIUtils.setRenderingHints( g2 );
boolean isCellEditor = isTableCellEditor( c ); boolean isCellEditor = isTableCellEditor( c );
float focusWidth = isCellEditor ? 0 : getFocusWidth( c ); float focusWidth = isCellEditor ? 0 : scale( (float) getFocusWidth( c ) );
float borderWidth = getBorderWidth( c ); float borderWidth = scale( (float) getBorderWidth( c ) );
float arc = isCellEditor ? 0 : getArc( c ); float arc = isCellEditor ? 0 : scale( (float) getArc( c ) );
if( isFocused( c ) ) { if( isFocused( c ) ) {
float innerFocusWidth = !(c instanceof JScrollPane) ? this.innerFocusWidth : 0; float innerFocusWidth = !(c instanceof JScrollPane) ? this.innerFocusWidth : 0;
g2.setColor( getFocusColor( c ) ); g2.setColor( getFocusColor( c ) );
FlatUIUtils.paintComponentOuterBorder( g2, x, y, width, height, focusWidth, FlatUIUtils.paintComponentOuterBorder( g2, x, y, width, height, focusWidth,
getLineWidth( c ) + scale( innerFocusWidth ), arc ); scale( (float) getLineWidth( c ) ) + scale( innerFocusWidth ), arc );
} }
g2.setPaint( getBorderColor( c ) ); g2.setPaint( getBorderColor( c ) );
@@ -153,7 +153,8 @@ public class FlatBorder
@Override @Override
public Insets getBorderInsets( Component c, Insets insets ) { public Insets getBorderInsets( Component c, Insets insets ) {
boolean isCellEditor = isTableCellEditor( c ); boolean isCellEditor = isTableCellEditor( c );
float ow = (isCellEditor ? 0 : getFocusWidth( c )) + getLineWidth( c ); float focusWidth = isCellEditor ? 0 : scale( (float) getFocusWidth( c ) );
float ow = focusWidth + scale( (float) getLineWidth( c ) );
insets = super.getBorderInsets( c, insets ); insets = super.getBorderInsets( c, insets );
insets.top = Math.round( scale( (float) insets.top ) + ow ); insets.top = Math.round( scale( (float) insets.top ) + ow );
@@ -163,19 +164,33 @@ public class FlatBorder
return insets; return insets;
} }
protected float getFocusWidth( Component c ) { /**
return scale( (float) focusWidth ); * Returns the (unscaled) thickness of the outer focus border.
*/
protected int getFocusWidth( Component c ) {
return focusWidth;
} }
protected float getLineWidth( Component c ) { /**
return scale( 1f ); * Returns the (unscaled) line thickness used to compute the border insets.
* This may be different to {@link #getBorderWidth}.
*/
protected int getLineWidth( Component c ) {
return 1;
} }
protected float getBorderWidth( Component c ) { /**
* Returns the (unscaled) line thickness used to paint the border.
* This may be different to {@link #getLineWidth}.
*/
protected int getBorderWidth( Component c ) {
return getLineWidth( c ); return getLineWidth( c );
} }
protected float getArc( Component c ) { /**
* Returns the (unscaled) arc diameter of the border.
*/
protected int getArc( Component c ) {
return 0; return 0;
} }
} }

View File

@@ -16,7 +16,6 @@
package com.formdev.flatlaf.ui; package com.formdev.flatlaf.ui;
import static com.formdev.flatlaf.util.UIScale.scale;
import java.awt.Color; import java.awt.Color;
import java.awt.Component; import java.awt.Component;
import java.awt.GradientPaint; import java.awt.GradientPaint;
@@ -124,21 +123,21 @@ public class FlatButtonBorder
} }
@Override @Override
protected float getFocusWidth( Component c ) { protected int getFocusWidth( Component c ) {
return FlatToggleButtonUI.isTabButton( c ) ? 0 : super.getFocusWidth( c ); return FlatToggleButtonUI.isTabButton( c ) ? 0 : super.getFocusWidth( c );
} }
@Override @Override
protected float getBorderWidth( Component c ) { protected int getBorderWidth( Component c ) {
return FlatButtonUI.isDefaultButton( c ) ? scale( (float) defaultBorderWidth ) : super.getBorderWidth( c ); return FlatButtonUI.isDefaultButton( c ) ? defaultBorderWidth : super.getBorderWidth( c );
} }
@Override @Override
protected float getArc( Component c ) { protected int getArc( Component c ) {
switch( FlatButtonUI.getButtonType( c ) ) { switch( FlatButtonUI.getButtonType( c ) ) {
case FlatButtonUI.TYPE_SQUARE: return 0; case FlatButtonUI.TYPE_SQUARE: return 0;
case FlatButtonUI.TYPE_ROUND_RECT: return Float.MAX_VALUE; case FlatButtonUI.TYPE_ROUND_RECT: return Short.MAX_VALUE;
default: return scale( (float) arc ); default: return arc;
} }
} }
} }

View File

@@ -16,7 +16,6 @@
package com.formdev.flatlaf.ui; package com.formdev.flatlaf.ui;
import static com.formdev.flatlaf.util.UIScale.scale;
import java.awt.Component; import java.awt.Component;
import javax.swing.UIManager; import javax.swing.UIManager;
@@ -33,7 +32,7 @@ public class FlatRoundBorder
protected final int arc = UIManager.getInt( "Component.arc" ); protected final int arc = UIManager.getInt( "Component.arc" );
@Override @Override
protected float getArc( Component c ) { protected int getArc( Component c ) {
return scale( (float) arc ); return arc;
} }
} }

View File

@@ -16,14 +16,13 @@
package com.formdev.flatlaf.ui; package com.formdev.flatlaf.ui;
import static com.formdev.flatlaf.util.UIScale.scale;
import java.awt.Component; import java.awt.Component;
import javax.swing.UIManager; import javax.swing.UIManager;
/** /**
* Border for various text components (e.g. {@link javax.swing.JTextField}). * Border for various text components (e.g. {@link javax.swing.JTextField}).
* *
* @uiDefault Component.arc int * @uiDefault TextComponent.arc int
* *
* @author Karl Tauber * @author Karl Tauber
*/ */
@@ -33,7 +32,7 @@ public class FlatTextBorder
protected final int arc = UIManager.getInt( "TextComponent.arc" ); protected final int arc = UIManager.getInt( "TextComponent.arc" );
@Override @Override
protected float getArc( Component c ) { protected int getArc( Component c ) {
return scale( (float) arc ); return arc;
} }
} }