scaling of swing icons

This commit is contained in:
hneemann 2017-05-09 19:06:43 +02:00
parent 378667ebb8
commit 535763aa12
2 changed files with 63 additions and 12 deletions

View File

@ -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");
}

View File

@ -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);
}
}
}