У меня есть приложение, которое будет использовать JLabel с ImageIcon с иконкой размером 32x32.
Теперь я хотел бы использовать изображение 64x64, загрузить его и изменить его размер до 32x32, если низкий DPI, в противном случаеиспользуйте его в качестве изображения с высоким разрешением.
Изменение размера легко, этот прием работает, например:
ImageIcon icon = ...
Image lowRes = icon.getImage().getScaledInstance(32, 32, Image.SCALE_SMOOTH);
return new ImageIcon(lowRes);
Однако мне не удается найти способ установить ImageIcon для обработки какизображение с высоким DPI.
Я пытался поиграться с MultiResolutionImage, но безуспешно.
РЕДАКТИРОВАТЬ: Попытка использовать MultiResolutionImage наивным способом:
private ImageIcon loadIcon(String iconName)
{
ImageIcon icon = new ImageIcon(getClass().getClassLoader()
.getResource("res/icons/toolbar/" + iconName));
BaseMultiResolutionImage baseMultiResolutionImage = new BaseMultiResolutionImage(
icon.getImage().getScaledInstance(32, 32, Image.SCALE_SMOOTH),
icon.getImage()
);
return new ImageIcon(baseMultiResolutionImage);
}
Stacktrace:
2019-06-11 14:00:45,962 ERROR [AWT-EventQueue-0] Catch.all - Uncaught exception on [AWT-EventQueue-0]: Invalid Image variant
java.lang.IllegalArgumentException: Invalid Image variant
at java.desktop/sun.awt.image.SurfaceManager.getManager(SurfaceManager.java:82)
at java.desktop/sun.java2d.SurfaceData.getSourceSurfaceData(SurfaceData.java:218)
at java.desktop/sun.java2d.pipe.DrawImage.renderImageCopy(DrawImage.java:572)
at java.desktop/sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:67)
at java.desktop/sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:1027)
at java.desktop/sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:3415)
at java.desktop/sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:3391)
at java.desktop/javax.swing.ImageIcon.paintIcon(ImageIcon.java:425)
at java.desktop/com.apple.laf.AquaButtonUI.paintIcon(AquaButtonUI.java:395)
at java.desktop/com.apple.laf.AquaButtonUI.paint(AquaButtonUI.java:304)
at java.desktop/javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
at java.desktop/javax.swing.JComponent.paintComponent(JComponent.java:797)
at java.desktop/javax.swing.JComponent.paint(JComponent.java:1074)
at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907)
at java.desktop/javax.swing.JComponent.paint(JComponent.java:1083)
at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907)
at java.desktop/javax.swing.JComponent.paint(JComponent.java:1083)
at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907)
at java.desktop/javax.swing.JComponent.paint(JComponent.java:1083)
at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907)
at java.desktop/javax.swing.JComponent.paint(JComponent.java:1083)
at java.desktop/javax.swing.JLayeredPane.paint(JLayeredPane.java:590)
at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907)
at java.desktop/javax.swing.JComponent.paintToOffscreen(JComponent.java:5262)
at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBufferedImpl(RepaintManager.java:1643)
at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1618)
at java.desktop/javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1556)
at java.desktop/javax.swing.RepaintManager.paint(RepaintManager.java:1323)
at java.desktop/javax.swing.JComponent.paint(JComponent.java:1060)
at java.desktop/java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
at java.desktop/sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78)
at java.desktop/sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115)
at java.desktop/java.awt.Container.paint(Container.java:2002)
at java.desktop/java.awt.Window.paint(Window.java:3926)
at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:876)
at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:848)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:848)
at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:823)
at java.desktop/javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:772)
at java.desktop/javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1884)