-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageFileViewer.java
More file actions
179 lines (127 loc) · 4.27 KB
/
Copy pathImageFileViewer.java
File metadata and controls
179 lines (127 loc) · 4.27 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
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
/**
* JPG/PPM file viewer.
*
* Uses ImageCanvas to displayed the custom xxxFile implementations.
*
*/
class ImageFileViewer extends Frame implements WindowListener, ActionListener, MouseListener {
// control buttons
Button ExitButton;
ImageCanvas canvas;
public ImageFileViewer() {
// constructor--initialize frame
super("PPM/JPG File Viewer (Click on canvas to load image)");
setup();
}
// initialize frame contents (buttons, etc.)
public void setup() {
Panel CanvasPanel; // panel for drawing canvas
Panel ButtonPanel; // main button panel
setSize(500,400);
setLayout(new BorderLayout());
this.setBackground(Color.gray);
this.setForeground(Color.black);
// initialize buttons, make frame responsible for actions
ExitButton = new Button("Exit");
ExitButton.addActionListener(this);
// initialize canvas
canvas = new ImageCanvas();
canvas.addMouseListener(this);
canvas.setSize(400, 300);
canvas.setBackground(Color.black);
canvas.setForeground(Color.white);
ButtonPanel = new Panel();
// buttons will be centered
ButtonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
// add buttons to button panel
ButtonPanel.add(ExitButton);
// add canvas to canvas panel centered
CanvasPanel = new Panel();
CanvasPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
CanvasPanel.add(canvas);
// put main panels into the frame
add("North", new Label(""));
add("South", ButtonPanel);
add("Center", CanvasPanel);
// frame is hidden until you show it
setVisible(true);
// care about window events
addWindowListener(this);
}
// under the new event handling model, you have to "implement" all
// of these window operations if you're interested in any of them.
// We only care about window close events.
public void windowDeiconified(WindowEvent event) {
}
public void windowIconified(WindowEvent event) {
}
public void windowActivated(WindowEvent event) {
}
public void windowDeactivated(WindowEvent event) {
}
public void windowOpened(WindowEvent event) {
}
public void windowClosed(WindowEvent event) {
}
public void windowClosing(WindowEvent event) {
// handle user closing frame
setVisible(false); // hide frame first...
dispose(); // release resources and destroy frame
System.exit(0); // exit program
}
public void mouseDragged(MouseEvent event) {
}
public void mousePressed(MouseEvent event) {
// show file dialog for loading PPM when the user clicks...
FileDialog f = new FileDialog(this, "Open a JPG/PPM image file",
FileDialog.LOAD);
f.setFile("*.*");
f.show();
if (f.getFile() == null) {
System.out.println("Image open cancelled.");
}
else {
try {
xxxFile file = null;
String temp = (f.getDirectory()+f.getFile()).toLowerCase();
temp = temp.substring(temp.lastIndexOf('.')+1,temp.length());
if (temp.equals("jpg") || temp.equals("jpeg"))
file = new JPGFile(f.getDirectory() + f.getFile());
else if (temp.equals("ppm") || temp.equals("pnm"))
file = new PPMFile(f.getDirectory() + f.getFile());
setImage(file.getBytes(), file.getWidth(), file.getHeight());
} catch (Exception e) { e.printStackTrace(); }
}
}
public void setImage(byte[] b, int w, int h) {
canvas.readImage(b, w, h);
}
public void setImage(int[] b, int w, int h) {
canvas.readImage(b, w, h);
}
public void setImage(double[] b, int w, int h) {
canvas.readImage(b, w, h);
}
public void mouseReleased(MouseEvent event) {
}
public void mouseMoved(MouseEvent event) {
}
public void mouseEntered(MouseEvent event) {
}
public void mouseExited(MouseEvent event) {
}
public void mouseClicked(MouseEvent event) {
}
public void actionPerformed(ActionEvent event) {
// handle pushbutton events--first get source of event
Object source = event.getSource();
if (source == ExitButton) {
setVisible(false); // hide frame first...
dispose(); // release resources and destroy frame
System.exit(0); // exit program
}
}
}