Class HiDPIUtils


  • public class HiDPIUtils
    extends java.lang.Object
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  HiDPIUtils.Painter  
    • Constructor Summary

      Constructors 
      Constructor Description
      HiDPIUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static float computeTextYCorrection​(java.awt.Graphics2D g)
      When painting text on HiDPI screens and the JRE scales, then the text is painted too far down on some operating systems.
      static java.awt.Graphics2D createGraphicsTextYCorrection​(java.awt.Graphics2D g)
      Creates a graphics object and applies Y correction to string drawing methods.
      static void drawStringUnderlineCharAtWithYCorrection​(javax.swing.JComponent c, java.awt.Graphics2D g, java.lang.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 void drawStringWithYCorrection​(javax.swing.JComponent c, java.awt.Graphics2D g, java.lang.String text, int x, int y)
      Applies Y correction and draws the given string at the specified location.
      static void paintAtScale1x​(java.awt.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 void paintAtScale1x​(java.awt.Graphics2D g, javax.swing.JComponent c, HiDPIUtils.Painter painter)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • HiDPIUtils

        public HiDPIUtils()
    • Method Detail

      • paintAtScale1x

        public static void paintAtScale1x​(java.awt.Graphics2D g,
                                          javax.swing.JComponent c,
                                          HiDPIUtils.Painter painter)
      • paintAtScale1x

        public static void paintAtScale1x​(java.awt.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

        public static float computeTextYCorrection​(java.awt.Graphics2D g)
        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 methods computes a correction value for the Y position.

      • drawStringWithYCorrection

        public static void drawStringWithYCorrection​(javax.swing.JComponent c,
                                                     java.awt.Graphics2D g,
                                                     java.lang.String text,
                                                     int x,
                                                     int y)
        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​(javax.swing.JComponent c,
                                                                    java.awt.Graphics2D g,
                                                                    java.lang.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

        public static java.awt.Graphics2D createGraphicsTextYCorrection​(java.awt.Graphics2D g)
        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.