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

1    package ip.gui.frames; 
2     
3    import futils.Futil; 
4    import j2d.ImageUtils; 
5    import graphics.NumImage; 
6    import gui.Clipper; 
7    import gui.ImageBean; 
8    import gui.ImageBeanInterface; 
9    import ip.gui.CommandLineInterpreter; 
10   import j2d.ShortImageBean; 
11    
12   import java.awt.*; 
13   import java.awt.event.ActionEvent; 
14   import java.awt.image.ColorModel; 
15   import java.io.File; 
16    
17   public class ImageFrame 
18           extends ShortCutFrame 
19           implements ImageFrameInterface { 
20    
21    
22       private ImageBeanInterface ib = new ImageBean(); 
23       public ShortImageBean shortImageBean = 
24               new ShortImageBean(); 
25    
26       private MenuBar menuBar = new MenuBar(); 
27       private Menu fileMenu = new Menu("File"); 
28    
29       private Menu openMenu = new Menu("Open"); 
30       private MenuItem openGif_mi = 
31               addMenuItem(getOpenMenu(), "[g]if..."); 
32       private MenuItem default_mi = 
33               addMenuItem(getOpenMenu(), "[d]efault image"); 
34       private MenuItem revert_mi = 
35               addMenuItem(getOpenMenu(), "[r]evert"); 
36       private MenuItem fitScreen_mi = 
37               addMenuItem(getOpenMenu(), "fitScreen"); 
38       CommandLineInterpreter 
39               cli = new CommandLineInterpreter(this); 
40       InfoFrame infoFrame 
41               = new InfoFrame(cli); 
42       private String dir = null; 
43    
44       public void actionPerformed(ActionEvent e) { 
45    
46           if (match(e, fitScreen_mi)) { 
47               ImageUtils.fitScreen(this); 
48               return; 
49           } 
50           if (match(e, openGif_mi)) { 
51               openGif(); 
52               return; 
53           } 
54           if (match(e, default_mi)) { 
55               grabNumImage(); 
56               return; 
57           } 
58           if (match(e, revert_mi)) { 
59               revert(); 
60               return; 
61           } 
62       } 
63    
64       public void setG(short r[][]) { 
65           shortImageBean.setG(r); 
66       } 
67    
68       public void setB(short r[][]) { 
69           shortImageBean.setB(r); 
70       } 
71    
72       public ImageFrame(String title) { 
73           super(title); 
74           initialize(); 
75       } 
76    
77       public ImageBeanInterface getImageBean() { 
78           return ib; 
79       } 
80    
81    
82       private void initialize() { 
83           initMenu(); 
84           grabNumImage(); 
85    
86           setVisible(true); 
87           setSize(getImageWidth(), getImageHeight()); 
88           repaint(); 
89       } 
90    
91    
92       public void grabNumImage() { 
93           ib.setFromFile(false); 
94           shortImageBean.setR(NumImage.getGray()); 
95           shortImageBean.setG(NumImage.getGray()); 
96           shortImageBean.setB(NumImage.getGray()); 
97           getClipper().setClipped(false); 
98           ib.setImage(shortImageBean); 
99           Rectangle r = getBounds(); 
100          repaint(0, 0, r.width, r.height); 
101      } 
102   
103      private void initMenu() { 
104          getFileMenu().add(getOpenMenu()); 
105          menuBar.add(getFileMenu()); 
106          setMenuBar(menuBar); 
107      } 
108   
109      public void setImageResize(Image i) { 
110          ib.setImage(i); 
111          setSize(ib.getImageWidth(), ib.getImageHeight()); 
112          setVisible(true); 
113          repaint(); 
114          image2Short(); 
115      } 
116   
117      public void setImage(Image i) { 
118          ib.setImage(i); 
119          image2Short(); 
120      } 
121   
122      public void setImageNoShort(Image img) { 
123          setImage(img); 
124          ImageUtils.waitForImage(this, img); 
125          setImageWidth(img.getWidth(this)); 
126          setImageHeight(img.getHeight(this)); 
127      } 
128   
129      public Image getImage() { 
130          ImageUtils.waitForImage(this, ib.getImage()); 
131          return ib.getImage(); 
132      } 
133   
134      public String getFileName() { 
135          return ib.getFile() + ""; 
136      } 
137   
138      public Clipper getClipper() { 
139          return shortImageBean.getClipper(); 
140      } 
141   
142   
143      /** 
144       short2Image - turn 3 short arrays, r, g and b into an image. 
145       */ 
146      public void short2Image() { 
147          Clipper clipper = getClipper(); 
148          clipper.setClipped(false); 
149          ib.setImage(shortImageBean); 
150          Rectangle r = getBounds(); 
151          repaint(0, 0, r.width, r.height); 
152      } 
153   
154      public void pels2Image(int pels[]) { 
155          setImageWidth(shortImageBean.getR().length); 
156          setImageHeight(shortImageBean.getR()[0].length); 
157          Image i = ImageUtils.getImage(pels, getImageWidth(), getImageHeight()); 
158          setImageNoShort(i); 
159          Rectangle r = getBounds(); 
160          repaint(0, 0, r.width, r.height); 
161      } 
162   
163   
164      /** 
165       image2Short - takes the private Image instance and 
166       makes 3 short arrays, r, g and b. 
167       */ 
168      public void image2Short() { 
169          if (getImage() == null) return; 
170          if (getImageWidth() < 0) return; 
171          if (getImageHeight() < 0) return; 
172          Image img = ib.getImage(); 
173          ImageUtils.waitForImage(this, img); 
174          shortImageBean.setImage(img); 
175      } 
176   
177   
178      public void openGif() { 
179          String fn = 
180                  Futil.getReadFileName(); 
181          if (fn == null) return; 
182          openGif(fn); 
183      } 
184   
185      public void setFtpFileName(String _fn) { 
186          setFileName(_fn); 
187          setImageComesFromFile(false); 
188      } 
189   
190      public void setFileName(String _fn) { 
191          File f = new File(_fn); 
192          if (!f.exists()) return; 
193          ib.setFile(f); 
194          infoFrame.println( 
195                  "File:" + _fn + 
196                  "\nis " + f.length() + " bytes long"); 
197          setImageComesFromFile(true); 
198   
199      } 
200   
201      public void openGif(String fn) { 
202          File f = new File(fn); 
203          if (!f.exists()) { 
204              grabNumImage(); 
205              return; 
206          } 
207   
208          setFileName(fn); 
209          ib.setImage( 
210                  Toolkit.getDefaultToolkit().getImage( 
211                          getFileName())); 
212          image2Short(); 
213          ImageUtils.waitForImage(this, ib.getImage()); 
214          setImageResize(ib.getImage()); 
215          setImageComesFromFile(true); 
216      } 
217   
218      public void revert() { 
219          if (isImageComesFromFile()) { 
220              getImageFromFile(); 
221          } else 
222              grabNumImage(); 
223   
224      } 
225   
226      private void getImageFromFile() { 
227          if (getFileName() == null) 
228              openGif(); 
229          else { 
230              setImage(Toolkit.getDefaultToolkit().getImage(getFileName())); 
231              setImageResize(getImage()); 
232          } 
233      } 
234   
235      public void revertNoResize() { 
236          Toolkit tk = Toolkit.getDefaultToolkit(); 
237          if (isImageComesFromFile()) { 
238              if (getFileName() == null) 
239                  openGif(); 
240              else { 
241                  setImage(tk.getImage(getFileName())); 
242                  setImage(getImage()); 
243              } 
244          } else 
245              grabNumImage(); 
246   
247      } 
248   
249      // Takes a packed RGB model and makes 
250      // the short arrays 
251      /* No usages found! 
252      public void int2Short(int pels[]) { 
253          int w = ib.getImageWidth(); 
254          int h = ib.getImageHeight(); 
255          ColorModel cm = ib.getColorModel(); 
256          sib.setR(new short[w][h]); 
257          sib.setG(new short[w][h]); 
258          sib.setB(new short[w][h]); 
259          int i = 0; 
260          for (int x = 0; x < w; x++) 
261              for (int y = 0; y < h; y++) { 
262                  i = x + y * w; 
263                  sib.getB()[x][y] = (short) cm.getBlue(pels[i]); 
264                  sib.getG()[x][y] = (short) cm.getGreen(pels[i]); 
265                  sib.getR()[x][y] = (short) cm.getRed(pels[i]); 
266              } 
267      } 
268      */ 
269   
270   
271      public static void main(String args[]) { 
272          ImageFrame imgFrm = 
273                  new ImageFrame("Image Frame"); 
274          imgFrm.openGif(); 
275      } 
276   
277      public void paint(Graphics g) { 
278          if (getImage() != null) { 
279              Dimension r = getSize(); 
280              g.drawImage(getImage(), 0, 0, r.width, r.height, 
281                      this); 
282          } 
283      } 
284   
285      public int getImageWidth() { 
286          return ib.getImageWidth(); 
287      } 
288   
289      public void setImageWidth(int imageWidth) { 
290          ib.setImageWidth(imageWidth); 
291      } 
292   
293      public int getImageHeight() { 
294          return ib.getImageHeight(); 
295      } 
296   
297      public void setImageHeight(int imageHeight) { 
298          ib.setImageHeight(imageHeight); 
299      } 
300   
301      public boolean isImageComesFromFile() { 
302          return ib.isFromFile(); 
303      } 
304   
305      public void setImageComesFromFile(boolean imageComesFromFile) { 
306          ib.setFromFile(imageComesFromFile); 
307      } 
308   
309      public ColorModel getCm() { 
310          return ib.getColorModel(); 
311      } 
312   
313      public Menu getFileMenu() { 
314          return fileMenu; 
315      } 
316   
317      public void setFileMenu(Menu fileMenu) { 
318          this.fileMenu = fileMenu; 
319      } 
320   
321      public Menu getOpenMenu() { 
322          return openMenu; 
323      } 
324   
325      public void setOpenMenu(Menu openMenu) { 
326          this.openMenu = openMenu; 
327      } 
328   
329      public String getDir() { 
330          return dir; 
331      } 
332   
333      public void setDir(String dir) { 
334          this.dir = dir; 
335      } 
336   
337      public ShortImageBean getShortImageBean() { 
338          return shortImageBean; 
339      } 
340   
341   
342  }