Package com.formdev.flatlaf.util
Class UIScale
- java.lang.Object
-
- com.formdev.flatlaf.util.UIScale
-
public class UIScale extends java.lang.Object
This class handles scaling in Swing UIs. It computes user scaling factor based on font size and provides methods to scale integer, float,Dimension
andInsets
. This class is look and feel independent.Two scaling modes are supported by FlatLaf for HiDPI displays:
1) system scaling mode
This mode is supported since Java 9 on all platforms and in some Java 8 VMs (e.g. Apple and JetBrains). The JRE determines the scale factor per-display and adds a scaling transformation to the graphics object. E.g. invokesjava.awt.Graphics2D.scale( 1.5, 1.5 )
for 150%. So the JRE does the scaling itself. E.g. when you draw a 10px line, a 15px line is drawn on screen. The scale factor may be different for each connected display. The scale factor may change for a window when moving the window from one display to another one.2) user scaling mode
This mode is mainly for Java 8 compatibility, but is also used on Linux or if the default font is changed. The user scale factor is computed based on the used font. The JRE does not scale anything. So we have to invokescale(float)
where necessary. There is only one user scale factor for all displays. The user scale factor may change if the active LaF, "defaultFont" or "Label.font" has changed. If system scaling mode is available the user scale factor is usually 1, but may be larger on Linux or if the default font is changed.
-
-
Field Summary
Fields Modifier and Type Field Description private static java.beans.PropertyChangeSupport
changeSupport
private static boolean
DEBUG
private static boolean
initialized
private static java.lang.Boolean
jreHiDPI
private static float
scaleFactor
-
Constructor Summary
Constructors Constructor Description UIScale()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addPropertyChangeListener(java.beans.PropertyChangeListener listener)
static javax.swing.plaf.FontUIResource
applyCustomScaleFactor(javax.swing.plaf.FontUIResource font)
Applies a custom scale factor given in system property "flatlaf.uiScale" to the given font.static float
computeFontScaleFactor(java.awt.Font font)
For internal use only.private static float
computeScaleFactor(java.awt.Font font)
private static float
getCustomScaleFactor()
Get custom scale factor specified in system property "flatlaf.uiScale".static double
getSystemScaleFactor(java.awt.Graphics2D g)
Returns the system scale factor for the given graphics context.static double
getSystemScaleFactor(java.awt.GraphicsConfiguration gc)
Returns the system scale factor for the given graphics configuration.static float
getUserScaleFactor()
Returns the user scale factor.private static void
initialize()
static boolean
isSystemScalingEnabled()
Returns whether system scaling is enabled.private static boolean
isUserScalingEnabled()
private static float
parseScaleFactor(java.lang.String s)
Similar to sun.java2d.SunGraphicsEnvironment.getScaleFactor(String)static void
removePropertyChangeListener(java.beans.PropertyChangeListener listener)
static float
scale(float value)
Multiplies the given value by the user scale factor.static int
scale(int value)
Multiplies the given value by the user scale factor and rounds the result.static java.awt.Dimension
scale(java.awt.Dimension dimension)
Scales the given dimension with the user scale factor.static java.awt.Insets
scale(java.awt.Insets insets)
Scales the given insets with the user scale factor.static int
scale2(int value)
Similar asscale(int)
but always "rounds down".static void
scaleGraphics(java.awt.Graphics2D g)
If user scale factor is not 1, scale the given graphics context by invokingGraphics2D.scale(double, double)
with user scale factor.private static void
setUserScaleFactor(float scaleFactor, boolean normalize)
Sets the user scale factor.static float
unscale(float value)
Divides the given value by the user scale factor.static int
unscale(int value)
Divides the given value by the user scale factor and rounds the result.private static void
updateScaleFactor()
-
-
-
Field Detail
-
DEBUG
private static final boolean DEBUG
- See Also:
- Constant Field Values
-
changeSupport
private static java.beans.PropertyChangeSupport changeSupport
-
jreHiDPI
private static java.lang.Boolean jreHiDPI
-
scaleFactor
private static float scaleFactor
-
initialized
private static boolean initialized
-
-
Method Detail
-
addPropertyChangeListener
public static void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
-
removePropertyChangeListener
public static void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
-
isSystemScalingEnabled
public static boolean isSystemScalingEnabled()
Returns whether system scaling is enabled.
-
getSystemScaleFactor
public static double getSystemScaleFactor(java.awt.Graphics2D g)
Returns the system scale factor for the given graphics context.
-
getSystemScaleFactor
public static double getSystemScaleFactor(java.awt.GraphicsConfiguration gc)
Returns the system scale factor for the given graphics configuration.
-
initialize
private static void initialize()
-
updateScaleFactor
private static void updateScaleFactor()
-
computeFontScaleFactor
public static float computeFontScaleFactor(java.awt.Font font)
For internal use only.- Since:
- 2
-
computeScaleFactor
private static float computeScaleFactor(java.awt.Font font)
-
isUserScalingEnabled
private static boolean isUserScalingEnabled()
-
applyCustomScaleFactor
public static javax.swing.plaf.FontUIResource applyCustomScaleFactor(javax.swing.plaf.FontUIResource font)
Applies a custom scale factor given in system property "flatlaf.uiScale" to the given font.
-
getCustomScaleFactor
private static float getCustomScaleFactor()
Get custom scale factor specified in system property "flatlaf.uiScale".
-
parseScaleFactor
private static float parseScaleFactor(java.lang.String s)
Similar to sun.java2d.SunGraphicsEnvironment.getScaleFactor(String)
-
getUserScaleFactor
public static float getUserScaleFactor()
Returns the user scale factor.
-
setUserScaleFactor
private static void setUserScaleFactor(float scaleFactor, boolean normalize)
Sets the user scale factor.
-
scale
public static float scale(float value)
Multiplies the given value by the user scale factor.
-
scale
public static int scale(int value)
Multiplies the given value by the user scale factor and rounds the result.
-
scale2
public static int scale2(int value)
Similar asscale(int)
but always "rounds down".For use in special cases.
scale(int)
is the preferred method.
-
unscale
public static float unscale(float value)
Divides the given value by the user scale factor.
-
unscale
public static int unscale(int value)
Divides the given value by the user scale factor and rounds the result.
-
scaleGraphics
public static void scaleGraphics(java.awt.Graphics2D g)
If user scale factor is not 1, scale the given graphics context by invokingGraphics2D.scale(double, double)
with user scale factor.
-
scale
public static java.awt.Dimension scale(java.awt.Dimension dimension)
Scales the given dimension with the user scale factor.If user scale factor is 1, then the given dimension is simply returned. Otherwise, a new instance of
Dimension
orDimensionUIResource
is returned, depending on whether the passed dimension implementsUIResource
.
-
scale
public static java.awt.Insets scale(java.awt.Insets insets)
Scales the given insets with the user scale factor.If user scale factor is 1, then the given insets is simply returned. Otherwise, a new instance of
Insets
orInsetsUIResource
is returned, depending on whether the passed dimension implementsUIResource
.
-
-