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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
package bjc.imgchain;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import bjc.imgchain.pipeline.Pipeline;
import bjc.imgchain.pipeline.PipelinePicker;
import bjc.imgchain.utils.Utils;
public class ImgViewer extends JInternalFrame {
private final class ReloadImageListener implements ActionListener {
private final File img;
ReloadImageListener(File img) {
this.img = img;
}
@Override
public void actionPerformed(ActionEvent ev) {
try {
icon.setImage(ImageIO.read(img));
} catch (IOException e) {
String msg = String.format("Error: Could not load image %s", img.getPath());
System.out.printf("%s\n", msg);
e.printStackTrace();
JOptionPane.showInternalMessageDialog(ImgViewer.this, msg, "Error loading image",
JOptionPane.ERROR_MESSAGE);
}
}
}
private final class ChangeImageListener implements ActionListener {
public ChangeImageListener() {
// TODO Auto-generated constructor stub
}
@Override
public void actionPerformed(ActionEvent ev) {
JFileChooser jfc = new JFileChooser();
jfc.setMultiSelectionEnabled(false);
int res = jfc.showOpenDialog(ImgViewer.this);
if (res != JFileChooser.APPROVE_OPTION) {
return;
}
try {
File tmp = jfc.getSelectedFile();
icon.setImage(ImageIO.read(tmp));
img = tmp;
setTitle("Image Viewer - " + img.getName());
} catch (IOException e) {
String msg = String.format("Error: Could not load image %s", img.getPath());
System.out.printf("%s\n", msg);
e.printStackTrace();
JOptionPane.showInternalMessageDialog(null, msg, "Error loading image",
JOptionPane.ERROR_MESSAGE);
}
}
}
private static final long serialVersionUID = -4727584150861526435L;
private File img;
private boolean initted;
private ImageIcon icon;
private ImgChain desktop;
private JLabel lab;
public ImgViewer(ImgChain desk, File img) {
super("Image Viewer - " + img.getName(), false, true, true, true);
initted = false;
this.img = img;
this.desktop = desk;
initted = setupGUI(img);
}
private boolean setupGUI(File img) {
setSize(320, 240);
setLayout(new GridLayout(1, 1));
JMenuBar bar = setupMenubar(img);
setJMenuBar(bar);
lab = loadLabel(img);
if (lab == null) {
return false;
}
add(lab);
return true;
}
private JMenuBar setupMenubar(File img) {
JMenuBar bar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic('F');
JMenuItem changeImage = new JMenuItem("Load Image from Disk");
changeImage.setMnemonic('L');
changeImage.addActionListener(new ChangeImageListener());
JMenuItem reloadImage = new JMenuItem("Reload Image");
reloadImage.setMnemonic('R');
reloadImage.addActionListener(new ReloadImageListener(img));
JMenuItem saveImage = new JMenuItem("Save Image");
saveImage.setMnemonic('S');
saveImage.addActionListener((ev) -> {
JFileChooser jfc = new JFileChooser();
jfc.setMultiSelectionEnabled(false);
int res = jfc.showSaveDialog(ImgViewer.this);
if (res != JFileChooser.APPROVE_OPTION) {
return;
}
try {
File tmp = jfc.getSelectedFile();
ImageIO.write(Utils.toBuffered(icon.getImage()), "PNG", tmp);
} catch (IOException e) {
String msg = String.format("Error: Could not save image %s", img.getPath());
System.out.printf("%s\n", msg);
e.printStackTrace();
JOptionPane.showInternalMessageDialog(null, msg, "Error saving image",
JOptionPane.ERROR_MESSAGE);
}
});
JMenuItem storeImage = new JMenuItem("Stash Image to Memory");
storeImage.setMnemonic('T');
storeImage.addActionListener((ev) -> {
String inp = JOptionPane.showInternalInputDialog(this, "Enter name to store image under");
if (inp == null) return;
inp = inp.trim();
if (inp.equals("")) {
return;
}
desktop.addImage(inp, icon.getImage());
});
JMenuItem recallImage = new JMenuItem("Recall Image from Memory");
recallImage.setMnemonic('F');
recallImage.addActionListener((ev) -> {
ImgPicker pick = new ImgPicker();
pick.pack();
pick.setVisible(true);
if (pick.imageName == null) {
System.out.println("WARN: picked null image");
return;
}
Image imag = ImgChain.chan.imageRepo.get(pick.imageName);
icon.setImage(imag);
lab.repaint();
});
fileMenu.add(changeImage);
fileMenu.add(reloadImage);
fileMenu.addSeparator();
fileMenu.add(storeImage);
JMenu editMenu = new JMenu("Edit Image");
editMenu.setMnemonic('E');
JMenuItem applyPipe = new JMenuItem("Apply Pipeline");
applyPipe.setMnemonic('A');
applyPipe.addActionListener((ev) -> {
PipelinePicker pick = new PipelinePicker();
pick.pack();
pick.setVisible(true);
if (pick.pipeName == null) {
System.out.println("WARN: picked null pipeline");
return;
}
Pipeline pipeline = ImgChain.chan.pipelineRepo.get(pick.pipeName);
BufferedImage bufimg = Utils.toBuffered(icon.getImage());
Image processed = pipeline.process(bufimg);
Utils.displayImage(processed, "Pipeline Results");
});
editMenu.addSeparator();
editMenu.add(applyPipe);
bar.add(fileMenu);
bar.add(editMenu);
return bar;
}
private JLabel loadLabel(File img) {
JLabel lab = null;
try {
URL imgURL = img.toURI().toURL();
icon = new ImageIcon(imgURL);
setSize(icon.getIconWidth(), icon.getIconHeight());
lab = new JLabel(icon);
} catch (MalformedURLException e) {
String msg = String.format("Error: Could not load image %s", img.getPath());
System.out.printf("%s\n", msg);
e.printStackTrace();
JOptionPane.showInternalMessageDialog(this, msg, "Error loading image",
JOptionPane.ERROR_MESSAGE);
}
return lab;
}
public boolean isInitialized() {
return initted;
}
}
|