mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-29 16:01:19 -04:00
scaling of swing icons
This commit is contained in:
parent
378667ebb8
commit
535763aa12
@ -40,14 +40,7 @@ public final class IconCreator {
|
|||||||
throw new NullPointerException("recource " + name + " not found!");
|
throw new NullPointerException("recource " + name + " not found!");
|
||||||
}
|
}
|
||||||
BufferedImage image = ImageIO.read(systemResource);
|
BufferedImage image = ImageIO.read(systemResource);
|
||||||
|
return Screen.getInstance().getScaledImage(image);
|
||||||
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;
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException("Image " + name + " not found");
|
throw new RuntimeException("Image " + name + " not found");
|
||||||
}
|
}
|
||||||
|
@ -2,10 +2,12 @@ package de.neemann.gui;
|
|||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
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
|
* Class used to handle different screen resolution by defining a new default font
|
||||||
* used by the GUI components.
|
* used by the GUI components. Also all the icons are scaled.
|
||||||
* Created by hneemann on 09.05.17.
|
* Created by hneemann on 09.05.17.
|
||||||
*/
|
*/
|
||||||
public final class Screen {
|
public final class Screen {
|
||||||
@ -33,13 +35,18 @@ public final class Screen {
|
|||||||
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||||
float s = screenSize.height / 90f;
|
float s = screenSize.height / 90f;
|
||||||
if (s > 12) {
|
if (s > 12) {
|
||||||
|
scaling = s / 12;
|
||||||
|
size = s;
|
||||||
font = font.deriveFont(s);
|
font = font.deriveFont(s);
|
||||||
for (Object key : javax.swing.UIManager.getLookAndFeel().getDefaults().keySet()) {
|
for (Object key : javax.swing.UIManager.getLookAndFeel().getDefaults().keySet()) {
|
||||||
if (key.toString().endsWith(".font"))
|
if (key.toString().endsWith(".font"))
|
||||||
javax.swing.UIManager.put(key, 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));
|
UIManager.put("ScrollBar.width", (int) (size * 17 / 12));
|
||||||
}
|
}
|
||||||
} catch (HeadlessException e) {
|
} catch (HeadlessException e) {
|
||||||
@ -50,6 +57,41 @@ public final class Screen {
|
|||||||
this.font = font;
|
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
|
* @return font size
|
||||||
*/
|
*/
|
||||||
@ -94,4 +136,20 @@ public final class Screen {
|
|||||||
return new Dimension((int) (dimension.width * scaling), (int) (dimension.height * scaling));
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user