From 535763aa127cb4937c35c2d40747897f73d8568b Mon Sep 17 00:00:00 2001 From: hneemann Date: Tue, 9 May 2017 19:06:43 +0200 Subject: [PATCH] scaling of swing icons --- src/main/java/de/neemann/gui/IconCreator.java | 9 +-- src/main/java/de/neemann/gui/Screen.java | 66 +++++++++++++++++-- 2 files changed, 63 insertions(+), 12 deletions(-) diff --git a/src/main/java/de/neemann/gui/IconCreator.java b/src/main/java/de/neemann/gui/IconCreator.java index b324b85b6..655106088 100644 --- a/src/main/java/de/neemann/gui/IconCreator.java +++ b/src/main/java/de/neemann/gui/IconCreator.java @@ -40,14 +40,7 @@ public final class IconCreator { throw new NullPointerException("recource " + name + " not found!"); } BufferedImage image = ImageIO.read(systemResource); - - final float scaling = Screen.getInstance().getScaling(); - if (scaling != 1) { - int w = (int) (image.getWidth() * scaling); - int h = (int) (image.getHeight() * scaling); - return image.getScaledInstance(w, h, BufferedImage.SCALE_SMOOTH); - } else - return image; + return Screen.getInstance().getScaledImage(image); } catch (IOException e) { throw new RuntimeException("Image " + name + " not found"); } diff --git a/src/main/java/de/neemann/gui/Screen.java b/src/main/java/de/neemann/gui/Screen.java index 4329a9784..22bf9e362 100644 --- a/src/main/java/de/neemann/gui/Screen.java +++ b/src/main/java/de/neemann/gui/Screen.java @@ -2,10 +2,12 @@ package de.neemann.gui; import javax.swing.*; import java.awt.*; +import java.awt.geom.AffineTransform; +import java.awt.image.BufferedImage; /** - * Class used to handle diifferent screen resolution by defining a new default font - * used by the GUI components. + * Class used to handle different screen resolution by defining a new default font + * used by the GUI components. Also all the icons are scaled. * Created by hneemann on 09.05.17. */ public final class Screen { @@ -33,13 +35,18 @@ public final class Screen { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); float s = screenSize.height / 90f; if (s > 12) { + scaling = s / 12; + size = s; font = font.deriveFont(s); for (Object key : javax.swing.UIManager.getLookAndFeel().getDefaults().keySet()) { if (key.toString().endsWith(".font")) javax.swing.UIManager.put(key, font); + if (key.toString().endsWith(".icon") || key.toString().endsWith("Icon")) { + Icon icon = UIManager.getIcon(key); + if (icon != null) + javax.swing.UIManager.put(key, new ScaleIcon(icon, scaling)); + } } - scaling = s / 12; - size = s; UIManager.put("ScrollBar.width", (int) (size * 17 / 12)); } } catch (HeadlessException e) { @@ -50,6 +57,41 @@ public final class Screen { this.font = font; } + private static final class ScaleIcon implements Icon { + private final Icon icon; + private final float scaling; + private final int width; + private final int height; + + private ScaleIcon(Icon icon, float scaling) { + this.icon = icon; + this.scaling = scaling; + width = (int) (icon.getIconWidth() * scaling); + height = (int) (icon.getIconHeight() * scaling); + } + + @Override + public void paintIcon(Component component, Graphics graphics, int x, int y) { + Graphics2D gr = (Graphics2D) graphics; + AffineTransform tr = gr.getTransform(); + gr.translate(x, y); + gr.scale(scaling, scaling); + gr.translate(-x, -y); + icon.paintIcon(component, gr, x, y); + gr.setTransform(tr); + } + + @Override + public int getIconWidth() { + return width; + } + + @Override + public int getIconHeight() { + return height; + } + } + /** * @return font size */ @@ -94,4 +136,20 @@ public final class Screen { return new Dimension((int) (dimension.width * scaling), (int) (dimension.height * scaling)); } + /** + * Get a scaled image + * + * @param image the original image + * @return the scaled image + */ + public Image getScaledImage(BufferedImage image) { + if (scaling == 1) + return image; + else { + int w = (int) (image.getWidth() * scaling); + int h = (int) (image.getHeight() * scaling); + return image.getScaledInstance(w, h, BufferedImage.SCALE_SMOOTH); + } + } + }