/Users/lyon/j4p/src/face/ImageFileViewer.java
|
1 package face;
2
3 import java.awt.*;
4 import java.awt.event.*;
5
6 /**
7 * JPG/PPM file viewer.
8 *
9 * Uses ImageCanvas to displayed the custom xxxFile implementations.
10 *
11 */
12 class ImageFileViewer extends Frame implements WindowListener, ActionListener, MouseListener {
13
14 // control buttons
15
16 Button ExitButton;
17 ImageCanvas canvas;
18
19 public ImageFileViewer() {
20
21 // constructor--initialize frame
22
23 super("PPM/JPG File Viewer (Click on canvas to load image)");
24 setup();
25 }
26
27 // initialize frame contents (buttons, etc.)
28
29 public void setup() {
30
31 Panel CanvasPanel; // panel for drawing canvas
32 Panel ButtonPanel; // main button panel
33
34 setSize(500, 400);
35
36 setLayout(new BorderLayout());
37
38 this.setBackground(Color.gray);
39 this.setForeground(Color.black);
40
41 // initialize buttons, make frame responsible for actions
42
43 ExitButton = new Button("Exit");
44 ExitButton.addActionListener(this);
45
46 // initialize canvas
47
48 canvas = new ImageCanvas();
49 canvas.addMouseListener(this);
50 canvas.setSize(400, 300);
51 canvas.setBackground(Color.black);
52 canvas.setForeground(Color.white);
53
54 ButtonPanel = new Panel();
55
56 // buttons will be centered
57
58 ButtonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
59
60 // add buttons to button panel
61
62 ButtonPanel.add(ExitButton);
63
64 // add canvas to canvas panel centered
65
66 CanvasPanel = new Panel();
67 CanvasPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
68 CanvasPanel.add(canvas);
69
70 // put main panels into the frame
71
72 add("North", new Label(""));
73 add("South", ButtonPanel);
74 add("Center", CanvasPanel);
75
76 // frame is hidden until you show it
77
78 setVisible(true);
79
80 // care about window events
81
82 addWindowListener(this);
83 }
84
85 // under the new event handling model, you have to "implement" all
86 // of these window operations if you're interested in any of them.
87 // We only care about window close events.
88
89 public void windowDeiconified(WindowEvent event) {
90 }
91
92 public void windowIconified(WindowEvent event) {
93 }
94
95 public void windowActivated(WindowEvent event) {
96 }
97
98 public void windowDeactivated(WindowEvent event) {
99 }
100
101 public void windowOpened(WindowEvent event) {
102 }
103
104 public void windowClosed(WindowEvent event) {
105 }
106
107 public void windowClosing(WindowEvent event) {
108
109 // handle user closing frame
110
111 setVisible(false); // hide frame first...
112 dispose(); // release resources and destroy frame
113 System.exit(0); // exit program
114
115 }
116
117 public void mouseDragged(MouseEvent event) {
118
119 }
120
121
122 public void mousePressed(MouseEvent event) {
123
124 // show file dialog for loading PPM when the user clicks...
125
126 FileDialog f = new FileDialog(this, "Open a JPG/PPM image file",
127 FileDialog.LOAD);
128 f.setFile("*.*");
129 f.show();
130 if (f.getFile() == null) {
131 System.out.println("Image open cancelled.");
132 } else {
133 try {
134 ImageFileFormat file = null;
135 String temp = (f.getDirectory() + f.getFile()).toLowerCase();
136
137 temp = temp.substring(temp.lastIndexOf('.') + 1, temp.length());
138 if (temp.equals("jpg") || temp.equals("jpeg"))
139 file = new JPGFile(f.getDirectory() + f.getFile());
140 else if (temp.equals("ppm") || temp.equals("pnm"))
141 file = new PPMFile(f.getDirectory() + f.getFile());
142
143 setImage(file.getBytes(), file.getWidth(), file.getHeight());
144 } catch (Exception e) {
145 e.printStackTrace();
146 }
147 }
148
149
150 }
151
152 public void setImage(byte[] b, int w, int h) {
153 canvas.readImage(b, w, h);
154 }
155
156 public void setImage(int[] b, int w, int h) {
157 canvas.readImage(b, w, h);
158 }
159
160 public void setImage(double[] b, int w, int h) {
161 canvas.readImage(b, w, h);
162 }
163
164 public void mouseReleased(MouseEvent event) {
165 }
166
167 public void mouseMoved(MouseEvent event) {
168 }
169
170 public void mouseEntered(MouseEvent event) {
171 }
172
173 public void mouseExited(MouseEvent event) {
174 }
175
176 public void mouseClicked(MouseEvent event) {
177 }
178
179 public void actionPerformed(ActionEvent event) {
180
181 // handle pushbutton events--first get source of event
182
183 Object source = event.getSource();
184
185 if (source == ExitButton) {
186 setVisible(false); // hide frame first...
187 dispose(); // release resources and destroy frame
188 System.exit(0); // exit program
189 }
190 }
191
192
193 }