Class HiDPIUtils
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfacestatic classA repaint manager that fixes a problem in Swing when repainting components at some scale factors (e.g.static interface -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddDirtyRegion(JComponent c, int x, int y, int width, int height, HiDPIUtils.DirtyRegionCallback callback) Similar torepaint(Component, int, int, int, int), but invokes callback instead of invokingComponent.repaint(int, int, int, int).static floatWhen painting text on HiDPI screens and the JRE scales, then the text is painted too far down on some operating systems.static Graphics2DCreates a graphics object and applies Y correction to string drawing methods.static voiddrawStringUnderlineCharAtWithYCorrection(JComponent c, Graphics2D g, String text, int underlinedIndex, int x, int y) Applies Y correction and draws the given string at the specified location underlining the specified character.static voiddrawStringWithYCorrection(JComponent c, Graphics2D g, String text, int x, int y) Applies Y correction and draws the given string at the specified location.static voidInstalls aHiDPIUtils.HiDPIRepaintManageron Windows when running in Java 9+, but only if default repaint manager is currently installed.static voidpaintAtScale1x(Graphics2D g, int x, int y, int width, int height, HiDPIUtils.Painter painter) Paint at system scale factor 1x to avoid rounding issues at 125%, 150% and 175% scaling.static voidpaintAtScale1x(Graphics2D g, JComponent c, HiDPIUtils.Painter painter) static voidRepaints the given component.static voidRepaints the given component area.static voidRepaints the given component area.
-
Constructor Details
-
HiDPIUtils
public HiDPIUtils()
-
-
Method Details
-
paintAtScale1x
-
paintAtScale1x
public static void paintAtScale1x(Graphics2D g, int x, int y, int width, int height, HiDPIUtils.Painter painter) Paint at system scale factor 1x to avoid rounding issues at 125%, 150% and 175% scaling.Scales the given Graphics2D down to 100% and invokes the given painter passing scaled x, y, width and height.
Uses the same scaling calculation as the JRE uses.
-
computeTextYCorrection
When painting text on HiDPI screens and the JRE scales, then the text is painted too far down on some operating systems. The higher the system scale factor is, the more.This method computes a correction value for the Y position.
-
drawStringWithYCorrection
Applies Y correction and draws the given string at the specified location. The provided component is used to query text properties and anti-aliasing hints.Use this method instead of
Graphics.drawString(String, int, int)for correct anti-aliasing.Replacement for
SwingUtilities2.drawString(). -
drawStringUnderlineCharAtWithYCorrection
public static void drawStringUnderlineCharAtWithYCorrection(JComponent c, Graphics2D g, String text, int underlinedIndex, int x, int y) Applies Y correction and draws the given string at the specified location underlining the specified character. The provided component is used to query text properties and anti-aliasing hints.Replacement for
SwingUtilities2.drawStringUnderlineCharAt(). -
createGraphicsTextYCorrection
Creates a graphics object and applies Y correction to string drawing methods. If no Y correction is necessary, the passed in graphics object is returned. -
repaint
Repaints the given component.See
repaint(Component, int, int, int, int)for more details.- Since:
- 3.5
-
repaint
Repaints the given component area.See
repaint(Component, int, int, int, int)for more details.- Since:
- 3.5
-
repaint
Repaints the given component area.Invokes
Component.repaint(int, int, int, int)on the given component,Use this method, instead of
Component.repaint(...), to fix a problem in Swing when using scale factors that end on .25 or .75 (e.g. 1.25, 1.75, 2.25, etc) and repainting single components, which may not repaint right and/or bottom 1px edge of component.The problem may occur under following conditions:
- using Java 9 or later
- system scale factor is 125%, 175%, 225%, ... (Windows only; Java on macOS and Linux does not support fractional scale factors)
- repaint whole component or right/bottom area of component
- component is opaque; or component is contained in a opaque container that has same right/bottom bounds as component
- component has bounds that Java/Swing scales different when repainting components
- Since:
- 3.5
-
installHiDPIRepaintManager
public static void installHiDPIRepaintManager()Installs aHiDPIUtils.HiDPIRepaintManageron Windows when running in Java 9+, but only if default repaint manager is currently installed.Invoke once on application startup. Compatible with all/other LaFs.
- Since:
- 3.5
-
addDirtyRegion
public static void addDirtyRegion(JComponent c, int x, int y, int width, int height, HiDPIUtils.DirtyRegionCallback callback) Similar torepaint(Component, int, int, int, int), but invokes callback instead of invokingComponent.repaint(int, int, int, int).For use in custom repaint managers.
- Since:
- 3.5
-