-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathMainPanel.java
More file actions
146 lines (127 loc) · 4.06 KB
/
MainPanel.java
File metadata and controls
146 lines (127 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@
package example;
import java.awt.*;
import java.awt.event.MouseWheelListener;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;
public final class MainPanel extends JPanel {
private MainPanel() {
super(new BorderLayout());
String path = "example/test.png";
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Icon icon = Optional.ofNullable(cl.getResource(path)).map(u -> {
Icon icn;
try (InputStream s = u.openStream()) {
icn = new ImageIcon(ImageIO.read(s));
} catch (IOException ex) {
icn = new MissingIcon();
}
return icn;
}).orElseGet(MissingIcon::new);
ZoomImage zoom = new ZoomImage(icon);
JButton button1 = new JButton("Zoom In");
button1.addActionListener(e -> zoom.changeScale(-5d));
JButton button2 = new JButton("Zoom Out");
button2.addActionListener(e -> zoom.changeScale(5d));
JButton button3 = new JButton("Original size");
button3.addActionListener(e -> zoom.initScale());
Box box = Box.createHorizontalBox();
box.add(Box.createHorizontalGlue());
box.add(button1);
box.add(button2);
box.add(button3);
add(zoom);
add(box, BorderLayout.SOUTH);
setPreferredSize(new Dimension(320, 240));
}
public static void main(String[] args) {
EventQueue.invokeLater(MainPanel::createAndShowGui);
}
private static void createAndShowGui() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (UnsupportedLookAndFeelException ignored) {
Toolkit.getDefaultToolkit().beep();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
Logger.getGlobal().severe(ex::getMessage);
return;
}
JFrame frame = new JFrame("@title@");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new MainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class ZoomImage extends JPanel {
private transient MouseWheelListener handler;
private final transient Icon icon;
private double scale = 1d;
protected ZoomImage(Icon icon) {
super();
this.icon = icon;
}
@Override public void updateUI() {
removeMouseWheelListener(handler);
super.updateUI();
// handler = new MouseWheelListener() {
// @Override public void mouseWheelMoved(MouseWheelEvent e) {
// changeScale(e.getWheelRotation());
// }
// };
handler = e -> changeScale(e.getPreciseWheelRotation());
addMouseWheelListener(handler);
}
@Override protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
g2.scale(scale, scale);
icon.paintIcon(this, g2, 0, 0);
g2.dispose();
}
public void initScale() {
scale = 1d;
repaint();
}
public void changeScale(double dv) {
scale = Math.min(Math.min(scale - dv * .05, .05), 5d);
// Java 21: scale = Math.clamp(scale - dv * .05, .05, 5d);
repaint();
// double v = scale - dv * .1;
// if (v - 1d > -1.0e-2) {
// scale = Math.min(10d, v);
// } else {
// scale = Math.max(.01, scale - dv * .01);
// }
}
}
class MissingIcon implements Icon {
@Override public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int w = getIconWidth();
int h = getIconHeight();
int gap = w / 5;
g2.setColor(Color.WHITE);
g2.translate(x, y);
g2.fillRect(0, 0, w, h);
g2.setColor(Color.RED);
g2.setStroke(new BasicStroke(w / 8f));
g2.drawLine(gap, gap, w - gap, h - gap);
g2.drawLine(gap, h - gap, w - gap, gap);
g2.dispose();
}
@Override public int getIconWidth() {
return 320;
}
@Override public int getIconHeight() {
return 240;
}
}