-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathMainPanel.java
More file actions
182 lines (156 loc) · 4.93 KB
/
MainPanel.java
File metadata and controls
182 lines (156 loc) · 4.93 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@
package example;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Path2D;
import java.awt.image.BufferedImage;
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();
Image image = Optional.ofNullable(cl.getResource(path)).map(u -> {
Image img;
try (InputStream s = u.openStream()) {
img = ImageIO.read(s);
} catch (IOException ex) {
img = makeMissingImage();
}
return img;
}).orElseGet(MainPanel::makeMissingImage);
add(new ImagePanel(image));
setPreferredSize(new Dimension(320, 240));
}
private static Image makeMissingImage() {
Icon missingIcon = new MissingIcon();
int w = missingIcon.getIconWidth();
int h = missingIcon.getIconHeight();
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = bi.createGraphics();
missingIcon.paintIcon(null, g2, 0, 0);
g2.dispose();
return bi;
}
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 ImagePanel extends JPanel {
private transient RubberBandingListener rbl;
private final transient BasicStroke stroke = new BasicStroke(2f);
private final Path2D rubberBand = new Path2D.Double();
private final transient Image image;
protected ImagePanel(Image image) {
super();
this.image = image;
}
@Override public void updateUI() {
removeMouseListener(rbl);
removeMouseMotionListener(rbl);
super.updateUI();
rbl = new RubberBandingListener();
addMouseMotionListener(rbl);
addMouseListener(rbl);
}
@Override protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
int iw = image.getWidth(this);
int ih = image.getHeight(this);
Dimension dim = getSize();
int x = (dim.width - iw) / 2;
int y = (dim.height - ih) / 2;
g.drawImage(image, x, y, iw, ih, this);
g2.setPaint(Color.RED);
g2.fillOval(10, 10, 32, 32);
g2.setPaint(Color.GREEN);
g2.fillOval(50, 10, 32, 32);
g2.setPaint(Color.BLUE);
g2.fillOval(90, 10, 32, 32);
g2.setPaint(Color.PINK);
g2.fillOval(130, 10, 32, 32);
g2.setPaint(Color.CYAN);
g2.fillOval(170, 10, 32, 32);
g2.setPaint(Color.ORANGE);
g2.fillOval(210, 10, 32, 32);
g2.setXORMode(Color.PINK);
g2.fill(rubberBand);
g2.setPaintMode();
g2.setStroke(stroke);
g2.setPaint(Color.WHITE);
g2.draw(rubberBand);
g2.dispose();
}
protected Path2D getRubberBand() {
return rubberBand;
}
private final class RubberBandingListener extends MouseAdapter {
private final Point srcPoint = new Point();
@Override public void mouseDragged(MouseEvent e) {
Point dstPoint = e.getPoint();
Path2D rb = getRubberBand();
rb.reset();
rb.moveTo(srcPoint.x, srcPoint.y);
rb.lineTo(dstPoint.x, srcPoint.y);
rb.lineTo(dstPoint.x, dstPoint.y);
rb.lineTo(srcPoint.x, dstPoint.y);
rb.closePath();
e.getComponent().repaint();
}
@Override public void mouseReleased(MouseEvent e) {
e.getComponent().repaint();
}
@Override public void mousePressed(MouseEvent e) {
getRubberBand().reset();
srcPoint.setLocation(e.getPoint());
e.getComponent().repaint();
}
}
}
class MissingIcon implements Icon {
@Override public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g2 = (Graphics2D) g.create();
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;
}
}