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 setHiDPIPropertyName = "setHiDPI";
21 public static final String setHiDPIScalePropertyName = "setHiDPIScale";
23 private static boolean setHiDPI = false;
25 private static int setHiDPIScale = 0;
27 public static int dpi = 0;
29 public static int maxdimension = 0;
31 public static int width = 0;
33 public static int scale = 0;
35 private static boolean doneInit = false;
37 private static boolean allowScalePropertyArg = false;
41 isAMac = System.getProperty("os.name").indexOf("Mac") > -1;
42 isLinux = System.getProperty("os.name").toLowerCase()
43 .indexOf("linux") > -1;
44 isWindows = System.getProperty("os.name").toLowerCase()
45 .indexOf("windows") > -1;
48 private static void init()
55 String setHiDPIProperty = System.getProperty(setHiDPIPropertyName);
56 setHiDPI = setHiDPIProperty != null
57 && setHiDPIProperty.equalsIgnoreCase("true");
59 String setHiDPIScaleProperty = System
60 .getProperty(setHiDPIScalePropertyName);
61 if (setHiDPIScaleProperty != null)
65 setHiDPIScale = Integer.parseInt(setHiDPIScaleProperty);
66 } catch (NumberFormatException e)
68 System.err.println(setHiDPIScalePropertyName + " property give ("
69 + setHiDPIScaleProperty + ") but not parseable as integer");
73 // try and get screen resolution
76 dpi = Toolkit.getDefaultToolkit().getScreenResolution();
79 System.err.println("Cannot get screen resolution");
82 // try and get screen size height and width
85 int height = Toolkit.getDefaultToolkit().getScreenSize().height;
86 int width = Toolkit.getDefaultToolkit().getScreenSize().width;
87 // using maxdimension in case of portrait screens
88 maxdimension = Math.max(height, width);
91 System.err.println("Cannot get screen size height and width");
94 if (setHiDPI && setHiDPIScale > 0)
96 scale = setHiDPIScale;
100 // attempt at a formula for scaling based on screen dpi and maxdimension.
102 // will be an integer >=1
103 if (dpi > 0 && maxdimension > 0)
105 scale = Math.min(dpi / hidpiThreshold,
106 maxdimension / tallScreenThreshold) + 1;
108 else if (dpi == 0 && maxdimension > tallScreenThreshold)
110 // dpi couldn't be found but the screen has a large vertical pixel count
111 scale = maxdimension / tallScreenThreshold + 1;
113 /* do nothing if maxdimension == 0 -- might be just a small HD screen (e.g. Gemini PDA)
114 else if (maxdimension == 0 && dpi > hidpiThreshold)
120 // only make a change if scale is changed and other conditions (OS is linux)
121 // apply, or if setHiDPI has been specified
122 allowScalePropertyArg = (scale > 1 && isLinux) || setHiDPI;
124 if (allowScalePropertyArg)
126 System.out.println("boolean setHiDPI=" + setHiDPI);
127 System.out.println("DPI detected as " + dpi
128 + ". Scaling factor set to " + scale + ".");
134 public static void setHiDPIScale(int s)
137 allowScalePropertyArg = true;
141 public static String getScalePropertyArg()
144 // HiDPI setting. Just looking at Linux to start with. Test with Windows.
145 return allowScalePropertyArg ? "-D" + scalePropertyName + "=" + scale