How to get Java 9 to use higher resolution images on a HiDPI display?

I downloaded and installed the latest version of early access Java 9, and was glad to find that the Java Swing application on Windows now displays components in the correct size. My clients with HiDPI displays no longer need to use my app with half the intended width and height.

However, I noticed that in my Swing app, my own icons simply scale to double the width and height, making them look like jaggy.

I have a complete set of all the icons, both with the usual size (for example, foobar.png) and with double width / height (for example, foobar@2x.png ). I use Apple's naming conventions.

How would I like to get Java 9 to easily find and use the higher resolution version of the image when it is available, without having to manually encode it?

Sample code always makes the question clearer, just as Java 9 has an icon class with several permissions that I could use to compile and run the following code?

import javax.swing.*; public class ScratchSpace { public static void main(String[] args) throws Exception { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame("I work in HiDPI!"); // can I replace the following line with some magical new method that automatically loads the same icon at multiple resolutions // so that Swing chooses the correct one when rendering, depending on the current scale of the current display? Icon icon = new IconWithMultipleSizes("foobar.png"); JLabel label = new JLabel("I'm a label with an icon.", icon, SwingConstants.LEFT); frame.setContentPane(label); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); }); } } 

I think there should be such a class, but I cannot find it.

+5
source share
2 answers

After examining the Java 9 source code, my conclusion is that although the class class MultiResolutionImage exists in the Java API, it is not yet used by java.awt.Toolkit on Windows. The code for creating Java 9 instances of MultiResolutionImage is present in a specific instance of java.awt.Toolkit for OS X.

My guess is that it is still on the Oracle task list as part of adding HiDPI support for Java 9.

+1
source

In this section, OpenJDK describes the API for java.awt.image. java.awt.Graphics is designed to obtain rights based on DPI metrics.

Until then, I assume that you can check the DPI scale factor, and then draw an image with the resolution of this DPI scale. But that would be unpleasant.

However, it looks like this other page has a class that can distinguish between images with multiple resolutions, you just need to import it.

I am still looking at this second link to see how I can get it to work, but it looks promising.

0
source

Source: https://habr.com/ru/post/1257924/


All Articles