/Users/lyon/j4p/src/ip/gui/frames/SimpleImageFrame.java

1    package ip.gui.frames; 
2     
3    import java.awt.*; 
4    import java.awt.event.ActionEvent; 
5    import java.awt.event.ActionListener; 
6    import java.awt.image.ColorModel; 
7    import java.awt.image.MemoryImageSource; 
8    import java.awt.image.PixelGrabber; 
9    import java.io.File; 
10    
11   public class SimpleImageFrame 
12           extends Frame 
13           implements ActionListener { 
14    
15    
16       private Image image; 
17       private ColorModel cm = ColorModel.getRGBdefault(); 
18       private boolean imageComesFromFile = false; 
19    
20       public short r[][]; 
21       public short g[][]; 
22       public short b[][]; 
23    
24       public int width = 128; 
25       public int height = 128; 
26    
27   // A default file name..set to null 
28   // to start with file open dialog. 
29   // Set to string to start with an image. 
30   // Use a fully qualified path name, in quotes. 
31       private String fileName = null; 
32    
33       private MenuBar menuBar = new MenuBar(); 
34       Menu fileMenu = new Menu("File"); 
35    
36       Menu openMenu = new Menu("Open"); 
37       MenuItem openGif_mi = addMenuItem(openMenu, "ip.gif or jpeg"); 
38       MenuItem revert_mi = addMenuItem(openMenu, "revert"); 
39       MenuItem fitScreen_mi = addMenuItem(openMenu, "fitScreen"); 
40    
41    
42       public void fitScreen() { 
43           Toolkit tk = Toolkit.getDefaultToolkit(); 
44           Dimension d 
45                   = tk.getScreenSize(); 
46           setSize(d.width, d.height); 
47       } 
48    
49       public MenuItem addMenuItem(Menu aMenu, String itemName) { 
50           MenuItem mi = new MenuItem(itemName); 
51           aMenu.add(mi); 
52           mi.addActionListener(this); 
53           return (mi); 
54       } 
55    
56       public void actionPerformed(ActionEvent e) { 
57           Object o = e.getSource(); 
58    
59           if (o == fitScreen_mi) { 
60               fitScreen(); 
61               return; 
62           } 
63           if (o == openGif_mi) { 
64               openGif(); 
65               return; 
66           } 
67           if (o == revert_mi) { 
68               revert(); 
69               return; 
70           } 
71       } 
72    
73       public SimpleImageFrame(String title) { 
74           super(title); 
75           initialize(); 
76       } 
77    
78    
79       private void initialize() { 
80           initMenu(); 
81           show(); 
82           setSize(width, height); 
83           repaint(); 
84       } 
85    
86    
87       private void initMenu() { 
88           fileMenu.add(openMenu); 
89           menuBar.add(fileMenu); 
90           setMenuBar(menuBar); 
91       } 
92    
93       protected String getReadFileName() { 
94           FileDialog fd = new 
95                   FileDialog(this, "select a file"); 
96           fd.show(); 
97           String file_name = fd.getFile(); 
98           if (fd.getFile() == null) return null; 
99           String path_name = fd.getDirectory(); 
100          String file_string = path_name + file_name; 
101          System.out.println("Opening file: " + file_string); 
102          fd.dispose(); 
103          return file_string; 
104      } 
105   
106      public void setImageResize(Image i) { 
107          setImage(i); 
108   
109          setSize(width, height); 
110          setVisible(true); 
111          repaint(); 
112      } 
113   
114      public void setImage(Image i) { 
115          image = i; 
116          waitForImage(this, i); 
117          width = i.getWidth(this); 
118          height = i.getHeight(this); 
119          image2Short(); 
120      } 
121   
122      public void setImageNoShort(Image i) { 
123          image = i; 
124          waitForImage(this, i); 
125          width = i.getWidth(this); 
126          height = i.getHeight(this); 
127      } 
128   
129      public Image getImage() { 
130          waitForImage(this, image); 
131          return image; 
132      } 
133   
134      public String getFileName() { 
135          return fileName; 
136      } 
137   
138      /** 
139       short2Image - turn 3 short arrays, r, g and b into an image. 
140       */ 
141      public void short2Image() { 
142          width = r.length; 
143          height = r[0].length; 
144          Toolkit tk = Toolkit.getDefaultToolkit(); 
145          int pels[] = new int[width * height]; 
146          for (int x = 0; x < width; x++) 
147              for (int y = 0; y < height; y++) { 
148                  pels[x + y * width] = 
149                          0xff000000 
150                          | (r[x][y] << 16) 
151                          | (g[x][y] << 8) 
152                          | b[x][y]; 
153              } 
154          Image i = tk.createImage( 
155                  new MemoryImageSource( 
156                          width, 
157                          height, 
158                          cm, 
159                          pels, 0, 
160                          width)); 
161          setImageNoShort(i); 
162          Rectangle r = getBounds(); 
163          repaint(0, 0, r.width, r.height); 
164      } 
165   
166      public void pels2Image(int pels[]) { 
167          width = r.length; 
168          height = r[0].length; 
169          Toolkit tk = Toolkit.getDefaultToolkit(); 
170          Image i = tk.createImage( 
171                  new MemoryImageSource( 
172                          width, 
173                          height, 
174                          cm, 
175                          pels, 0, 
176                          width)); 
177          setImageNoShort(i); 
178          Rectangle r = getBounds(); 
179          repaint(0, 0, r.width, r.height); 
180      } 
181   
182      /** 
183       image2Short - takes the private Image instance and 
184       makes 3 short arrays, r, g and b. 
185       */ 
186      public void image2Short() { 
187          r = new short[width][height]; 
188          g = new short[width][height]; 
189          b = new short[width][height]; 
190   
191          int pels[] = new int[width * height]; 
192          cm = ColorModel.getRGBdefault(); 
193   
194          PixelGrabber grabber = 
195                  new PixelGrabber( 
196                          image, 0, 0, 
197                          width, height, pels, 0, width); 
198   
199          try { 
200              grabber.grabPixels(); 
201          } catch (InterruptedException e) { 
202          } 
203          ; 
204          int i = 0; 
205          for (int x = 0; x < width; x++) 
206              for (int y = 0; y < height; y++) { 
207                  i = x + y * width; 
208                  b[x][y] = (short) cm.getBlue(pels[i]); 
209                  g[x][y] = (short) cm.getGreen(pels[i]); 
210                  r[x][y] = (short) cm.getRed(pels[i]); 
211              } 
212      } 
213   
214   
215      static void waitForImage(Component component, 
216                               Image image) { 
217          MediaTracker tracker = new MediaTracker(component); 
218          try { 
219              tracker.addImage(image, 0); 
220              tracker.waitForID(0); 
221              if (!tracker.checkID(0)) 
222                  System.out.println("Load failure!"); 
223          } catch (InterruptedException e) { 
224          } 
225      } 
226   
227      public void openGif() { 
228          String fn = getReadFileName(); 
229          if (fn == null) return; 
230          openGif(fn); 
231      } 
232   
233      public void setFileName(String _fn) { 
234          File f = new File(_fn); 
235          if (f.exists()) { 
236              fileName = _fn; 
237              System.out.println( 
238                      "File:" + fileName + 
239                      "\nis " + f.length() + " bytes long"); 
240          } 
241          imageComesFromFile = true; 
242   
243      } 
244   
245      public void openGif(String fn) { 
246          File f = new File(fn); 
247   
248          fileName = fn; 
249          image = Toolkit.getDefaultToolkit().getImage( 
250                  fileName); 
251          setImageResize(image); 
252          imageComesFromFile = true; 
253      } 
254   
255      public void revert() { 
256          Toolkit tk = Toolkit.getDefaultToolkit(); 
257          if (fileName == null) 
258              openGif(); 
259          else { 
260              image = 
261                      tk.getImage(fileName); 
262              setImageResize(image); 
263          } 
264   
265      } 
266   
267      public void revertNoResize() { 
268          Toolkit tk = Toolkit.getDefaultToolkit(); 
269          if (fileName == null) 
270              openGif(); 
271          else { 
272              image = 
273                      tk.getImage(fileName); 
274              setImage(image); 
275          } 
276   
277      } 
278   
279      // Takes a packed RGB model and makes 
280      // the short arrays 
281      public void int2Short(int pels[]) { 
282          System.out.println("The width and height are" 
283                  + width + "," + height); 
284          r = new short[width][height]; 
285          g = new short[width][height]; 
286          b = new short[width][height]; 
287   
288          cm = ColorModel.getRGBdefault(); 
289          int i = 0; 
290          for (int x = 0; x < width; x++) 
291              for (int y = 0; y < height; y++) { 
292                  i = x + y * width; 
293                  b[x][y] = (short) cm.getBlue(pels[i]); 
294                  g[x][y] = (short) cm.getGreen(pels[i]); 
295                  r[x][y] = (short) cm.getRed(pels[i]); 
296              } 
297      } 
298   
299   
300      public static void main(String args[]) { 
301          SimpleImageFrame imgFrm = 
302                  new SimpleImageFrame("SimpleImageFrame"); 
303          imgFrm.openGif(); 
304      } 
305   
306      public void paint(Graphics g) { 
307          if (image != null) { 
308              Rectangle r = getBounds(); 
309              g.drawImage(image, 0, 0, r.width, r.height, 
310                      this); 
311          } 
312      } 
313  }