3 import java.awt.HeadlessException;
4 import java.awt.Toolkit;
6 public class HiDPISetting
8 private static final int hidpiThreshold = 160;
10 private static final int hidpiMultiThreshold = 240;
12 private static final int bigScreenThreshold = 1400;
14 private static final String scalePropertyName = "sun.java2d.uiScale";
16 private static final boolean isLinux;
18 // private static final boolean isAMac;
20 // private static final boolean isWindows;
22 public static final String setHiDPIPropertyName = "setHiDPI";
24 public static final String setHiDPIScalePropertyName = "setHiDPIScale";
26 private static boolean setHiDPI = false;
28 private static int setHiDPIScale = 0;
30 public static int dpi = 0;
32 public static int mindimension = 0;
34 public static int width = 0;
36 public static int scale = 0;
38 private static boolean doneInit = false;
40 private static boolean allowScalePropertyArg = false;
44 String system = System.getProperty("os.name") == null ? null
45 : System.getProperty("os.name").toLowerCase();
48 isLinux = system.indexOf("linux") > -1;
49 // isAMac = system.indexOf("mac") > -1;
50 // isWindows = system.indexOf("windows") > -1;
55 // isAMac = isWindows = false;
59 private static void init()
66 // get and use command line property values first
67 String setHiDPIProperty = System.getProperty(setHiDPIPropertyName);
68 setHiDPI = setHiDPIProperty != null
69 && setHiDPIProperty.equalsIgnoreCase("true");
71 String setHiDPIScaleProperty = System
72 .getProperty(setHiDPIScalePropertyName);
73 if (setHiDPIScaleProperty != null)
77 setHiDPIScale = Integer.parseInt(setHiDPIScaleProperty);
78 } catch (NumberFormatException e)
80 System.err.println(setHiDPIScalePropertyName + " property give ("
81 + setHiDPIScaleProperty + ") but not parseable as integer");
84 if (setHiDPI && setHiDPIScale > 0)
86 setHiDPIScale(setHiDPIScale);
90 // check to see if the scale property has already been set by something else
92 String existingProperty = System.getProperty(scalePropertyName);
93 if (existingProperty != null)
97 int existingPropertyVal = Integer.parseInt(existingProperty);
98 System.out.println("Existing " + scalePropertyName + " is "
99 + existingPropertyVal);
100 if (existingPropertyVal > 1)
102 setHiDPIScale(existingPropertyVal);
105 } catch (NumberFormatException e)
107 System.out.println("Could not convert property " + scalePropertyName
108 + " vale '" + existingProperty + "' to number");
112 // Try and auto guess a good scale based on reported DPI (not trustworthy)
113 // and screen resolution (more trustworthy)
118 dpi = Toolkit.getDefaultToolkit().getScreenResolution();
119 } catch (HeadlessException e)
121 System.err.println("Cannot get screen resolution: " + e.getMessage());
124 // try and get screen size height and width
127 int height = Toolkit.getDefaultToolkit().getScreenSize().height;
128 int width = Toolkit.getDefaultToolkit().getScreenSize().width;
129 // using mindimension in case of portrait screens
130 mindimension = Math.min(height, width);
131 } catch (HeadlessException e)
134 "Cannot get screen size height and width:" + e.getMessage());
137 // attempt at a formula for scaling based on screen dpi and mindimension.
138 // scale will be an integer >=1. This formula is based on some testing and
141 // scale based on reported dpi. if dpi>hidpiThreshold then scale=2+multiples
142 // of hidpiMultiThreshold (else scale=1)
143 // (e.g. dpi of 110 scales 1. dpi of 120 scales 2. dpi of 360 scales 3)
144 int dpiScale = (dpi - hidpiThreshold > 0)
145 ? 2 + ((dpi - hidpiThreshold) / hidpiMultiThreshold)
148 int dimensionScale = 1 + (mindimension / bigScreenThreshold);
150 // choose larger of dimensionScale or dpiScale (most likely dimensionScale
151 // as dpiScale often misreported)
152 int autoScale = Math.max(dpiScale, dimensionScale);
154 // only make an automatic change if scale is changed and other conditions
155 // (OS is linux) apply, or if setHiDPI has been specified
156 if ((autoScale > 1 && isLinux) || setHiDPI)
158 setHiDPIScale(autoScale);
162 // looks like we're not doing any scaling
166 public static void setHiDPIScale(int s)
169 allowScalePropertyArg = true;
173 public static String getScalePropertyArg()
176 // HiDPI setting. Just looking at Linux to start with. Test with Windows.
177 return allowScalePropertyArg ? "-D" + scalePropertyName + "=" + scale