-
Notifications
You must be signed in to change notification settings - Fork 1
/
BinaryImageDisplay.java
114 lines (97 loc) · 3.24 KB
/
BinaryImageDisplay.java
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
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.FileDialog;
public class BinaryImageDisplay {
private static final int DEFAULT_WIDTH = 640;
private static final int DEFAULT_HEIGHT = 480;
private static final String IMAGE_FORMAT = "PNG";
private JFrame window;
private ImagePanel imagePanel;
private FileDialog fileChooser;
private FileDialog fileSaver;
private File selectedFile;
ImageSorter sorter;
public BinaryImageDisplay() {
window = new JFrame("BinaryImage");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
window.setVisible(true);
imagePanel = new ImagePanel();
sorter = new ImageSorter();
fileChooser = new FileDialog(window, "Choose source data", FileDialog.LOAD);
fileChooser.setDirectory(System.getProperty("user.dir"));
fileSaver = new FileDialog(window, "Save image", FileDialog.SAVE);
fileSaver.setDirectory(System.getProperty("user.dir"));
JMenuBar menuBar = new JMenuBar();
window.setJMenuBar(menuBar);
JMenu menu = new JMenu("Menu");
menuBar.add(menu);
int modifierKey;
if (System.getProperty("os.name").equals("Mac OS X"))
modifierKey = ActionEvent.META_MASK;
else
modifierKey = ActionEvent.CTRL_MASK;
JMenuItem mntmChooseSourceData = new JMenuItem("Choose source data");
mntmChooseSourceData.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, modifierKey));
mntmChooseSourceData.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
selectFile();
}
});
menu.add(mntmChooseSourceData);
JMenuItem mntmSaveImage = new JMenuItem("Save image");
mntmSaveImage.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, modifierKey));
mntmSaveImage.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
saveImage();
}
});
menu.add(mntmSaveImage);
JMenuItem mntmQuit = new JMenuItem("Quit");
mntmQuit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, modifierKey));
mntmQuit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
menu.add(mntmQuit);
window.validate();
window.getContentPane().add(imagePanel);
}
private void selectFile() {
fileChooser.setVisible(true);
String filename = fileChooser.getDirectory() + fileChooser.getFile();
if (filename != null) {
selectedFile = new File(filename);
displayImage();
}
}
private void displayImage() {
ImageInterpreter rawData = new ImageInterpreter(selectedFile);
imagePanel.setPixelData(rawData.getPixelArray());
window.validate();
}
private void saveImage() {
BufferedImage currentDisplay = imagePanel.getImage();
fileSaver.setVisible(true);
String filename = fileSaver.getDirectory() + fileSaver.getFile();
if (filename != null) {
try {
ImageIO.write(currentDisplay, IMAGE_FORMAT, new File(filename));
} catch (IOException io) {
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
BinaryImageDisplay display = new BinaryImageDisplay();
display.selectFile();
}
}