3 import java.awt.Toolkit;
5 public class HiDPISetting
7 private static final int hidpiThreshold = 130;
9 private static final int tallScreenThreshold = 1300;
11 private static final String scalePropertyName = "sun.java2d.uiScale";
13 private static final boolean isAMac;
15 private static final boolean isLinux;
17 private static final boolean isWindows;
19 public static final String forceHiDPISettingPropertyName = "forceHiDPISetting";
21 public static final String forceHiDPISettingScalePropertyName = "forceHiDPISettingScale";
23 private static final boolean forceHiDPISetting;
25 private static final int forceHiDPISettingScale;
27 public static int dpi = 0;
29 public static int height = 0;
31 public static int scale = 0;
33 private static boolean doneInit = false;
35 private static boolean allowScalePropertyArg = false;
39 isAMac = System.getProperty("os.name").indexOf("Mac") > -1;
40 isLinux = System.getProperty("os.name").toLowerCase()
41 .indexOf("linux") > -1;
42 isWindows = System.getProperty("os.name").toLowerCase()
43 .indexOf("windows") > -1;
45 String forceHiDPISettingProperty = System
46 .getProperty(forceHiDPISettingPropertyName);
47 forceHiDPISetting = forceHiDPISettingProperty != null
48 && forceHiDPISettingProperty.equalsIgnoreCase("true");
50 int tryForceHiDPISettingScale = 0;
51 String forceHiDPISettingScaleProperty = System
52 .getProperty(forceHiDPISettingScalePropertyName);
53 if (forceHiDPISettingScaleProperty != null)
57 tryForceHiDPISettingScale = Integer
58 .parseInt(forceHiDPISettingScaleProperty);
59 } catch (NumberFormatException e)
61 System.err.println(forceHiDPISettingScalePropertyName
62 + " property give (" + forceHiDPISettingScaleProperty
63 + ") but not parseable as integer");
66 forceHiDPISettingScale = tryForceHiDPISettingScale;
76 // try and get screen resolution
79 dpi = Toolkit.getDefaultToolkit().getScreenResolution();
82 System.err.println("Cannot get screen resolution");
85 // try and get screen size height
88 height = Toolkit.getDefaultToolkit().getScreenSize().height;
91 System.err.println("Cannot get screen size height");
94 if (forceHiDPISetting && forceHiDPISettingScale > 0)
96 scale = forceHiDPISettingScale;
100 // attempt at a formula for scaling based on screen dpi and height. scale
101 // will be an integer >=1
102 scale = Math.min(dpi / hidpiThreshold, height / tallScreenThreshold)
106 allowScalePropertyArg = (scale > 1 && isLinux) || forceHiDPISetting;
108 if (allowScalePropertyArg)
110 System.out.println("boolean forceHiDPISetting=" + forceHiDPISetting);
111 System.out.println("DPI detected as " + dpi
112 + ". Scaling factor set to " + scale + ".");
118 public static synchronized String getScalePropertyArg()
120 // HiDPI setting. Just looking at Linux to start with. Test with Windows.
121 return allowScalePropertyArg ? "-D" + scalePropertyName + "=" + scale