- compute useful baseline for horizontal orientation so that the track is vertically centered
- no baseline for vertical orientation
This commit is contained in:
Karl Tauber
2020-11-18 18:26:30 +01:00
parent 413b60e630
commit 8b8ed0b9ff
2 changed files with 34 additions and 0 deletions

View File

@@ -18,6 +18,7 @@ package com.formdev.flatlaf.ui;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
@@ -143,6 +144,22 @@ public class FlatSliderUI
return new FlatTrackListener();
}
@Override
public int getBaseline( JComponent c, int width, int height ) {
if( c == null )
throw new NullPointerException();
if( width < 0 || height < 0 )
throw new IllegalArgumentException();
// no baseline for vertical orientation
if( slider.getOrientation() == JSlider.VERTICAL )
return -1;
// compute a baseline so that the track is vertically centered
FontMetrics fm = slider.getFontMetrics( slider.getFont() );
return trackRect.y + Math.round( (trackRect.height - fm.getHeight()) / 2f ) + fm.getAscent() - 1;
}
@Override
public Dimension getPreferredHorizontalSize() {
return UIScale.scale( super.getPreferredHorizontalSize() );

View File

@@ -18,6 +18,7 @@ package com.formdev.flatlaf.jideoss.ui;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
@@ -141,6 +142,22 @@ public class FlatRangeSliderUI
disabledThumbBorderColor = null;
}
@Override
public int getBaseline( JComponent c, int width, int height ) {
if( c == null )
throw new NullPointerException();
if( width < 0 || height < 0 )
throw new IllegalArgumentException();
// no baseline for vertical orientation
if( slider.getOrientation() == JSlider.VERTICAL )
return -1;
// compute a baseline so that the track is vertically centered
FontMetrics fm = slider.getFontMetrics( slider.getFont() );
return trackRect.y + Math.round( (trackRect.height - fm.getHeight()) / 2f ) + fm.getAscent() - 1;
}
@Override
public Dimension getPreferredHorizontalSize() {
return UIScale.scale( super.getPreferredHorizontalSize() );