ARTICLE AD BOX
The App is a Swing GUI app that display a JTable. Data is loaded from a file. The first column is an image rendered on the fly from the data in the file. This works in general. This is an old app initially made for Java 7 I'm updating.
The issue I'm facing is that the rendered image looks bad on displays that have Windows DPI scaling set above 100%. There is some form of poor upscaling going on.
Target is Java 11 so it should be aware of per monitor DPI scaling in theory from what I have read
The image is generated on the fly. It is a chemical structure image rendered by the indigo toolkit. But that is not really important to the issue, just as an explanation for the code fragment. Here is the current rendering code:
public synchronized void paintIcon(Component c, Graphics g, int x, int y) { Image image = getImage(); if (image == null) { return; } Insets insets = ((Container) c).getInsets(); x = insets.left; y = insets.top; GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); GraphicsConfiguration gc = device.getDefaultConfiguration(); AffineTransform transform = gc.getDefaultTransform(); // get cell size and scale for DPI scaling // Issue: scaleX and scaleY are always 1 int w = (int) ((c.getWidth() - x - insets.right) * transform.getScaleX()); int h = (int) ((c.getHeight() - y - insets.bottom) * transform.getScaleY()); logger.debug("Rendering Chemical Structure image: W: " + w + " H: " + h + " scaleX: " + transform.getScaleX() + " scaleY: " + transform.getScaleY()); if (w != width || h != height) { if (w < 16 || h < 16) { // 16 pixels is minimum size supported by indigo return; } width = w; height = h; indigo.setOption("render-image-size", w, h); indigo.setOption("image-resolution", 300); image = renderImage(); setImage(image); ImageObserver io = getImageObserver(); g.drawImage(image, x, y, w, h, io == null ? c : io); } } private Image renderImage() { try { Image image = null; if (structureData != null) { if (mol != null) { if (!mol.hasCoord()) { mol.layout(); } byte[] imageData = renderer.renderToBuffer(mol); ByteArrayInputStream bis = new ByteArrayInputStream(imageData); image = ImageIO.read(bis); //image = Toolkit.getDefaultToolkit().createImage(imageData); if (image == null) { return null; } } } return image; } catch (IOException e) { logger.catching(e); return null; } }The initial code did not have all this Graphics part. That was my addition according to research and yeah I admit it AI. Explains why I dislike AI, it simply does not work and the images are still somehow upscaled.
I also tried:
-Dsun.java2d.uiScale=1or
-Dsun.java2d.uiScale.enabled=trueBut both have no effect but also were recommendations to fix this issue.
I have also seen MultiResolutionImage but I prefer to just render the correct one.
How can I get the code to properly scale the image for displays with DPI scaling active?
