Reputation: 411
I just switched to Java 11 (AdoptOpenJDK) so that my Java Swing application looks OK on high DPI displays at different system scaling settings.
It works OK on Windows. Regardless of the scaling value configured on the system, Java automatically detects it and uses it to scale the GUI accordingly.
However, on Linux the GUI does not consider the system scaling, and thus, it looks tiny on high-DPI displays.
After reading some posts here, I found I can indicate Java what scaling value to use. For example, if system scaling is 200%, then I can add the following command line argument to the java command used to launch the application.
-Dsun.java2d.uiScale=2.0
The GUI looks fine on Linux when I add the above command line option.
I also read I can set the GDK_SCALE environment variable.
However, I'd like to find a better solution. Ideally, one where I don't need to specify the scaling value to use. Does anybody know if this is possible?
If the above is not possible, then I guess my next step will be to come up with a command in Linux that returns what the current system scaling value is and use it to set the sun.java2d.uiScale
option.
Upvotes: 32
Views: 25932
Reputation: 24
You can use third party tool named jHiccup. It can automatically detects the scaling value of the system. Then, using it you can fix the GUI.
For example,
To get the scaling value
double dpiScaling = Toolkit.getDefaultToolkit().getScreenResolution() / 96.0;
Set the scaling factor
JHiccup.setScaleFactor(dpiScaling);
You can check out the jHiccup's documentation.
Reference - https://docs.azul.com/prime/jHiccup
Upvotes: -1
Reputation: 330
Outside of a set of pre made scales and math scaling for all gui components, you could use borderlayout to size components and gridbaglayout to position components then only fonts and images require math resize.
graphicsdevice and graphicsenvironment classes Re: java.awt.GraphicsConfiguration. getBounds()
Upvotes: 1
Reputation: 112
You can try this one:
-Dsun.java2d.ddscale=true
As the official documentation says:
Setting this flag to true enables hardware-accelerated scaling if the DirectDraw/Direct3D pipeline is in use. DirectDraw/Direct3D hardware scaling is disabled by default to avoid rendering artifacts in existing applications.
For more information, you can see the official Oracle documentation about system properties.
Upvotes: -4