- use Component.focusColor for thumb if slider is focused
- use round thumb only if not painting ticks or labels
- if thumb is round, paint track before thumb in same color as thumb
This commit is contained in:
Karl Tauber
2019-08-27 00:12:43 +02:00
parent 165def6c81
commit ed55789744
2 changed files with 34 additions and 8 deletions

View File

@@ -45,6 +45,7 @@ import com.formdev.flatlaf.util.UIScale;
* @uiDefault Slider.verticalSize Dimension preferred vertical size; width is ignored; computed slider width is used * @uiDefault Slider.verticalSize Dimension preferred vertical size; width is ignored; computed slider width is used
* @uiDefault Slider.minimumHorizontalSize Dimension height is ignored; computed slider height is used * @uiDefault Slider.minimumHorizontalSize Dimension height is ignored; computed slider height is used
* @uiDefault Slider.minimumVerticalSize Dimension width is ignored; computed slider width is used * @uiDefault Slider.minimumVerticalSize Dimension width is ignored; computed slider width is used
* @uiDefault Component.focusColor Color
* *
* @author Karl Tauber * @author Karl Tauber
*/ */
@@ -57,6 +58,7 @@ public class FlatSliderUI
private Color trackColor; private Color trackColor;
private Color thumbColor; private Color thumbColor;
private Color disabledForeground; private Color disabledForeground;
private Color focusColor;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
return new FlatSliderUI(); return new FlatSliderUI();
@@ -76,6 +78,7 @@ public class FlatSliderUI
trackColor = UIManager.getColor( "Slider.trackColor" ); trackColor = UIManager.getColor( "Slider.trackColor" );
thumbColor = UIManager.getColor( "Slider.thumbColor" ); thumbColor = UIManager.getColor( "Slider.thumbColor" );
disabledForeground = UIManager.getColor( "Slider.disabledForeground" ); disabledForeground = UIManager.getColor( "Slider.disabledForeground" );
focusColor = UIManager.getColor( "Component.focusColor" );
} }
@Override @Override
@@ -122,25 +125,44 @@ public class FlatSliderUI
@Override @Override
public void paintTrack( Graphics g ) { public void paintTrack( Graphics g ) {
g.setColor( slider.isEnabled() ? trackColor : disabledForeground ); boolean enabled = slider.isEnabled();
float tw = UIScale.scale( (float) trackWidth ); float tw = UIScale.scale( (float) trackWidth );
float arc = tw; float arc = tw;
RoundRectangle2D coloredTrack = null;
RoundRectangle2D track;
if( slider.getOrientation() == JSlider.HORIZONTAL ) { if( slider.getOrientation() == JSlider.HORIZONTAL ) {
float y = trackRect.y + (trackRect.height - tw) / 2f; float y = trackRect.y + (trackRect.height - tw) / 2f;
((Graphics2D)g).fill( new RoundRectangle2D.Float( trackRect.x, y, trackRect.width, tw, arc, arc ) ); if( enabled && isRoundThumb() ) {
int cw = thumbRect.x + (thumbRect.width / 2) - trackRect.x;
coloredTrack = new RoundRectangle2D.Float( trackRect.x, y, cw, tw, arc, arc );
track = new RoundRectangle2D.Float( trackRect.x + cw, y, trackRect.width - cw, tw, arc, arc );
} else
track = new RoundRectangle2D.Float( trackRect.x, y, trackRect.width, tw, arc, arc );
} else { } else {
float x = trackRect.x + (trackRect.width - tw) / 2f; float x = trackRect.x + (trackRect.width - tw) / 2f;
((Graphics2D)g).fill( new RoundRectangle2D.Float( x, trackRect.y, tw, trackRect.height, arc, arc ) ); if( enabled && isRoundThumb() ) {
int ch = thumbRect.y + (thumbRect.height / 2) - trackRect.y;
track = new RoundRectangle2D.Float( x, trackRect.y, tw, ch, arc, arc );
coloredTrack = new RoundRectangle2D.Float( x, trackRect.y + ch, tw, trackRect.height - ch, arc, arc );
} else
track = new RoundRectangle2D.Float( x, trackRect.y, tw, trackRect.height, arc, arc );
} }
if( coloredTrack != null ) {
g.setColor( slider.hasFocus() ? focusColor : thumbColor );
((Graphics2D)g).fill( coloredTrack );
}
g.setColor( enabled ? trackColor : disabledForeground );
((Graphics2D)g).fill( track );
} }
@Override @Override
public void paintThumb( Graphics g ) { public void paintThumb( Graphics g ) {
g.setColor( slider.isEnabled() ? thumbColor : disabledForeground ); g.setColor( slider.hasFocus() ? focusColor : (slider.isEnabled() ? thumbColor : disabledForeground) );
if( !slider.getPaintTicks() ) if( isRoundThumb() )
g.fillOval( thumbRect.x, thumbRect.y, thumbRect.width, thumbRect.height ); g.fillOval( thumbRect.x, thumbRect.y, thumbRect.width, thumbRect.height );
else { else {
double w = thumbRect.width; double w = thumbRect.width;
@@ -173,4 +195,8 @@ public class FlatSliderUI
} }
} }
} }
private boolean isRoundThumb() {
return !slider.getPaintTicks() && !slider.getPaintLabels();
}
} }

View File

@@ -111,8 +111,8 @@ ScrollPane.background=@@ScrollBar.track
#---- Slider ---- #---- Slider ----
Slider.focusInsets=0,0,0,0 Slider.focusInsets=0,0,0,0
Slider.trackWidth=2 Slider.trackWidth=3
Slider.thumbWidth=10 Slider.thumbWidth=11
#---- TextArea ---- #---- TextArea ----