Class UIScale
Dimension and Insets.
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(int) 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.
Zooming
Zooming allows appliations to easily zoom their UI, if FlatLaf is active Laf. This is done by changing user scale factor and default font. There are methods to increase, decrease and reset zoom factor.
Note: Only standard Swing components are zoomed.
Custom components need to use scale(int) to zoom their UI.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidstatic FontUIResourceApplies a custom scale factor given in system property "flatlaf.uiScale" to the given font.static floatcomputeFontScaleFactor(Font font) For internal use only.static float[]static doubleReturns the system scale factor for the given graphics context.static doubleReturns the system scale factor for the given graphics configuration.static floatReturns the user scale factor (including zoom factor).static floatReturns the current zoom factor.static booleanReturns whether system scaling is enabled.static voidstatic floatscale(float value) Multiplies the given value by the user scale factor.static intscale(int value) Multiplies the given value by the user scale factor and rounds the result.static DimensionScales the given dimension with the user scale factor.static InsetsScales the given insets with the user scale factor.static intscale2(int value) Similar asscale(int)but always "rounds down".static voidIf user scale factor is not 1, scale the given graphics context by invokingGraphics2D.scale(double, double)with user scale factor.static voidsetSupportedZoomFactors(float[] supportedZoomFactors) static booleansetZoomFactor(float zoomFactor) Sets the zoom factor.static floatunscale(float value) Divides the given value by the user scale factor.static intunscale(int value) Divides the given value by the user scale factor and rounds the result.static booleanzoomIn()Increases zoom factor using next greater factor in supported factors array.static booleanzoomOut()Decreases zoom factor using next smaller factor in supported factors array.static booleanResets zoom factor to1.
-
Field Details
-
PROP_USER_SCALE_FACTOR
- Since:
- 3.7
- See Also:
-
PROP_ZOOM_FACTOR
- Since:
- 3.7
- See Also:
-
-
Constructor Details
-
UIScale
public UIScale()
-
-
Method Details
-
addPropertyChangeListener
-
removePropertyChangeListener
-
isSystemScalingEnabled
public static boolean isSystemScalingEnabled()Returns whether system scaling is enabled. -
getSystemScaleFactor
Returns the system scale factor for the given graphics context. -
getSystemScaleFactor
Returns the system scale factor for the given graphics configuration. -
computeFontScaleFactor
-
applyCustomScaleFactor
Applies a custom scale factor given in system property "flatlaf.uiScale" to the given font. -
getUserScaleFactor
public static float getUserScaleFactor()Returns the user scale factor (including zoom 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
If user scale factor is not 1, scale the given graphics context by invokingGraphics2D.scale(double, double)with user scale factor. -
scale
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
DimensionorDimensionUIResourceis returned, depending on whether the passed dimension implementsUIResource. -
scale
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
InsetsorInsetsUIResourceis returned, depending on whether the passed dimension implementsUIResource. -
getZoomFactor
public static float getZoomFactor()Returns the current zoom factor. Default is1.- Since:
- 3.7
-
setZoomFactor
public static boolean setZoomFactor(float zoomFactor) Sets the zoom factor. Also updates user scale factor and default font (if FlatLaf is active Laf).UI needs to be updated if zoom factor has changed. E.g.:
if( UIScale.setZoomFactor( newZoomFactor ) ) FlatLaf.updateUI();- Parameters:
zoomFactor- new zoom factor- Returns:
trueif zoom factor has changed- Since:
- 3.7
-
zoomIn
public static boolean zoomIn()Increases zoom factor using next greater factor in supported factors array.UI needs to be updated if zoom factor has changed. E.g.:
if( UIScale.zoomIn() ) FlatLaf.updateUI();- Returns:
trueif zoom factor has changed- Since:
- 3.7
- See Also:
-
zoomOut
public static boolean zoomOut()Decreases zoom factor using next smaller factor in supported factors array.UI needs to be updated if zoom factor has changed. E.g.:
if( UIScale.zoomOut() ) FlatLaf.updateUI();- Returns:
trueif zoom factor has changed- Since:
- 3.7
- See Also:
-
zoomReset
public static boolean zoomReset()Resets zoom factor to1.UI needs to be updated if zoom factor has changed. E.g.:
if( UIScale.zoomReset() ) FlatLaf.updateUI();- Returns:
trueif zoom factor has changed- Since:
- 3.7
-
getSupportedZoomFactors
-
setSupportedZoomFactors
-