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

1    package ip.gui.frames; 
2     
3    // here is the frame that should always be on 
4    // top. 
5    // To add frames, insert them below TopFrame. 
6    // 8/23/98 - DL 
7     
8    import futils.Futil; 
9    import gui.ImageBeanInterface; 
10   import ip.gui.BenchMark; 
11   import ip.gui.MorphUtils; 
12   import j2d.ImageUtils; 
13   import j2d.ShortImageBean; 
14   import math.Mat2; 
15   import math.MatFloat; 
16   import utils.SystemUtils; 
17   import utils.Timer; 
18    
19   import java.awt.*; 
20   import java.awt.event.ActionEvent; 
21   import java.io.File; 
22   import java.lang.reflect.Method; 
23   import java.util.Enumeration; 
24   import java.util.Properties; 
25   import java.util.Vector; 
26       // ip.gui.frames.TopFrame 
27   public class TopFrame 
28           extends WaveletFrame { 
29    
30    
31       private Menu reflectionMenu = getMenu("cutils.reflection"); 
32       private Menu diffractionMenu = getMenu("diffraction"); 
33       private Menu movieMenu = getMenu("movies"); 
34       private Menu utilMenu = getMenu("utilities"); 
35       private Menu effectsMenu = getMenu("effects"); 
36    
37       private MenuItem processDiffractionImages_mi = 
38               addMenuItem(diffractionMenu, "process linear images"); 
39       private MenuItem processDiffractionImage_mi = 
40               addMenuItem(diffractionMenu, "process edge image"); 
41       private MenuItem processCylindrical_mi = 
42               addMenuItem(diffractionMenu, "process cylindrical images"); 
43       private MenuItem threeDImageCylindrical_mi = 
44               addMenuItem(diffractionMenu, "display dgt file"); 
45    
46    
47       private MenuItem animateFrame_mi = 
48               addMenuItem(movieMenu, "Show Animate Frame"); 
49       private MenuItem makeMovie_mi = 
50               addMenuItem(movieMenu, "makeMovie"); 
51    
52       private MenuItem saveAsxyz_mi = 
53               addMenuItem(saveMenu, "save as x y z c"); 
54    
55       private MenuItem threeDImage_mi = 
56               addMenuItem(effectsMenu, "threeDImage"); 
57       private MenuItem mandelbrot_mi = 
58               addMenuItem(effectsMenu, "mandelbrot"); 
59    
60       private MenuItem html_mi = 
61               addMenuItem(utilMenu, "[T-h]tml generator..."); 
62       private MenuItem prototype_mi = 
63               addMenuItem(utilMenu, "prototype generator..."); 
64       private MenuItem benchMark_mi = 
65               addMenuItem(utilMenu, "bench mark"); 
66       private MenuItem rename_mi = 
67               addMenuItem(utilMenu, "rename"); 
68       private MenuItem ls_mi = 
69               addMenuItem(utilMenu, "ls"); 
70       private MenuItem commandLine_mi = 
71               addMenuItem(utilMenu, "command line"); 
72       private MenuItem goslab_mi = 
73               addMenuItem(utilMenu, "GOSLAB"); 
74       private MenuItem j2d_mi = 
75               addMenuItem(utilMenu, "j2d"); 
76       private MenuItem printMethods_mi = 
77               addMenuItem(reflectionMenu, "printMethods"); 
78       private MenuItem systemInfo_mi = 
79               addMenuItem(reflectionMenu, "systemInfo"); 
80    
81       public TopFrame(String title) { 
82           super(title); 
83           init(); 
84       } 
85    
86       public TopFrame(String title, ShortImageBean nsib) { 
87           super(title); 
88           init(); 
89           super.setImageResize(nsib.getImage()); 
90       } 
91    
92       public TopFrame(String title, Image img) { 
93           super(title); 
94           init(); 
95           super.setImageResize(img); 
96       } 
97    
98       public TopFrame(String title, 
99                       short _r[][], 
100                      short _g[][], 
101                      short _b[][]) { 
102          super(title); 
103          init(); 
104          shortImageBean.setR(_r); 
105          setG(_g); 
106          setB(_b); 
107          setSize(shortImageBean.getR().length, shortImageBean.getR()[0].length); 
108          short2Image(); 
109      } 
110   
111      private void init() { 
112          utilMenu.add(reflectionMenu); 
113          getFileMenu().add(effectsMenu); 
114          getFileMenu().add(diffractionMenu); 
115          getFileMenu().add(movieMenu); 
116          getFileMenu().add(utilMenu); 
117          commandLine(); 
118          positionInfoFrame(); 
119   
120   
121      } 
122   
123      private static String args[] = 
124              {""}; 
125   
126      public void cpus() { 
127          net.rmi.rmiimage.CPUArrayFrame.main(args); 
128      } 
129   
130      public static void mandelbrot() { 
131          String title = "OpenLifting"; 
132          ip.raul.MyOpenFrame of = 
133                  new ip.raul.MyOpenFrame(title); 
134          of.setVisible(true); 
135          of.setSize(64, 64); 
136          of.mandelbrot(); 
137      } 
138   
139      public void tracer() { 
140          Timer t = new Timer(); 
141          t.start(); 
142          graphics.raytracers.raytracer.tracer.Scene scene 
143                  = new graphics.raytracers.raytracer.tracer.Scene( 
144                          new Dimension(getImageWidth(), getImageHeight())); 
145          pels2Image( 
146                  scene.render()); 
147          t.print("render done"); 
148      } 
149   
150      public void animateMorph() { 
151          ip.raul.MorphLog.main(args); 
152      } 
153   
154      public void play() { 
155          sound.UlawCodec.playFromFile(); 
156      } 
157   
158      public void goslab() { 
159          new 
160                  gui.goslab.GOSLAB(); 
161   
162      } 
163   
164      public void j2d() { 
165          j2d.Main.main(null); 
166      } 
167   
168      public static void imageMorph() { 
169          ip.hak.ImageMorph.main(args); 
170      } 
171   
172      public static void sketchFrame() { 
173          bookExamples.ch26Graphics.draw2d.SketchFrame.main(args); 
174      } 
175   
176      public static void spiral() { 
177          bookExamples.ch26Graphics.draw2d.Spiral.main(args); 
178      } 
179   
180      public void preview2d() { 
181          graphics.raytracers.raytracer.tracer.Scene scene 
182                  = new graphics.raytracers.raytracer.tracer.Scene( 
183                          new Dimension(getImageWidth(), getImageHeight())); 
184          scene.preview(); 
185      } 
186   
187      public void preview3d() { 
188          //ip.raul.Application.main(args); 
189      } 
190   
191      public void quote() { 
192   
193          String args[] = 
194                  {""}; 
195          net.stocks.QuoteFrame.main(args); 
196      } 
197   
198      public void positionInfoFrame() { 
199          Rectangle r = getBounds(); 
200          Dimension d = r.getSize(); 
201          infoFrame.setLocation(0, 2 * d.height); 
202      } 
203   
204      public static void main(String args[]) { 
205          String title = "Kahindu by D. Lyon"; 
206          if (args.length == 1) 
207              title = args[0]; 
208          TopFrame tf = 
209                  new TopFrame(title); 
210          tf.setVisible(true); 
211          tf.setSize(64, 64); 
212   
213      } 
214   
215      public void actionPerformed(ActionEvent e) { 
216          if (match(e, mandelbrot_mi)) { 
217              mandelbrot(); 
218              return; 
219          } 
220          if (match(e, goslab_mi)) { 
221              goslab(); 
222              return; 
223          } 
224          if (match(e, j2d_mi)) { 
225              j2d(); 
226              this.negate(); 
227              return; 
228          } 
229          if (match(e, prototype_mi)) { 
230              prototype(); 
231              return; 
232          } 
233          if (match(e, html_mi)) { 
234              html(); 
235              return; 
236          } 
237          if (match(e, animateFrame_mi)) { 
238              af.setVisible(true); 
239              return; 
240          } 
241          if (match(e, commandLine_mi)) { 
242              commandLine(); 
243              return; 
244          } 
245          if (match(e, processCylindrical_mi)) { 
246              processCylindrical(); 
247              return; 
248          } 
249          if (match(e, threeDImageCylindrical_mi)) { 
250              threeDImageCylindrical(super.getImageBean()); 
251              return; 
252          } 
253          if (match(e, benchMark_mi)) { 
254              benchMark(); 
255              return; 
256          } 
257          if (match(e, printMethods_mi)) { 
258              printMethods(); 
259              return; 
260          } 
261          if (match(e, rename_mi)) { 
262              rename(); 
263              return; 
264          } 
265          if (match(e, makeMovie_mi)) { 
266              makeMovie(); 
267              return; 
268          } 
269          if (match(e, saveAsxyz_mi)) { 
270              saveAsxyz(); 
271              return; 
272          } 
273          if (match(e, processDiffractionImages_mi)) { 
274              processDiffractionImages(); 
275              return; 
276          } 
277          if (match(e, processDiffractionImage_mi)) { 
278              processDiffractionImage(); 
279              return; 
280          } 
281          if (match(e, ls_mi)) { 
282              ls(); 
283              return; 
284          } 
285          if (match(e, threeDImage_mi)) { 
286              threeDImage(); 
287              return; 
288          } 
289          if (match(e, systemInfo_mi)) { 
290              systemInfo(); 
291              return; 
292          } 
293          super.actionPerformed(e); 
294      } 
295   
296      public void processDiffractionImage() { 
297          erode(MorphUtils.getKh()); 
298          //System.out.println("Erode h done..."); 
299          roberts2(); 
300          //System.out.println("Robert2 done..."); 
301          close(MorphUtils.getKsquare()); 
302          //System.out.println("Close square done..."); 
303          skeleton(); 
304          //System.out.println("Skeleton done..."); 
305          //double ip[]={16.0, 16.0, 16.0, 16.0, 0.0}; 
306          //doit(ip); 
307   
308      } 
309   
310      public void prototype() { 
311          futils.ProtoType.main(args); 
312      } 
313   
314      public void html() { 
315          gui.htmlconverter.J2Html.main(args); 
316      } 
317   
318      public static void threeDImageCylindrical( 
319              ImageBeanInterface ib) { 
320          float radius[][] = new float[20][50]; 
321          MatFloat mf = new MatFloat(radius); 
322          String fn = Futil.getReadFileName(); 
323          mf.readAsgz(fn); 
324          radius = mf.f; 
325   
326          ImageUtils.waitForImage( 
327                  null, ib.getImage()); 
328          graphics.ddd.MainFrame.image3D( 
329                  ib.getImage(), radius); 
330      } 
331   
332      public String[] getFileNames() { 
333          FileDialog fd = new FileDialog(new Frame(), "select file"); 
334          fd.show(); 
335          setDir(fd.getDirectory()); 
336          FileFilter files = new FileFilter(); 
337          File f = new File(getDir()); 
338          String fn[] = f.list(files); 
339          for (int i = 0; i < fn.length; i++) 
340              fn[i] = getDir() + fn[i]; 
341          return fn; 
342      } 
343   
344      public void processCylindrical() { 
345          //x and y are used as looping variables when parsing each image 
346          int y = 0; 
347          int x = 0; 
348          int steps = 35;//the number of y values from an image (taken at equal intervals) 
349          int NumberOfImages; 
350          String files[] = getFileNames(); 
351          NumberOfImages = files.length; 
352          float radius[][] = new float[steps][NumberOfImages]; 
353          //according to the next four variables, 
354          //the images will only be searched in the specified region 
355          int ymin = 0; 
356          int ymax = 234; 
357          int xmin = 140; 
358          int xmax = 200; 
359   
360          //the next two variables, after parsing all the images, 
361          //will hold the most left and most right values of the radius. 
362          //useful for scaling the object 
363          float maxr = 0; 
364          float minr = 10000; 
365          System.out.println("ymin=" + ymin); 
366          System.out.println("ymax=" + ymax); 
367          System.out.println("xmin=" + xmin); 
368          System.out.println("xmax=" + xmax); 
369   
370          for (int i = 0; i < NumberOfImages; i++) { 
371              openGif(files[i]); 
372              processDiffractionImage(); 
373              System.out.println(i); 
374              try { 
375                  for (y = 0; y < steps; y++) { 
376                      for (x = xmin; x < xmax; x++) { 
377                          if (shortImageBean.getR()[x][ymin + y * ((ymax - ymin) / steps)] == 0) continue; 
378                          radius[y][i] = x; 
379                          if (x > maxr) maxr = x; 
380                          if (x < minr) minr = x; 
381                          break; 
382                      } 
383                      if (x == xmax) radius[y][i] = 0; 
384                  } 
385              } catch (Exception e) { 
386                  System.out.println(e + "x,y=" + x + "," + y); 
387              } 
388          } 
389   
390          //scaling section 
391          //values of r[][] less than zero will not be triangulated later on 
392          System.out.println("min=" + minr); 
393          System.out.println("max=" + maxr); 
394          for (int i = 0; i < NumberOfImages; i++) { 
395              for (y = 0; y < steps; y++) { 
396                  radius[y][i] = radius[y][i] - minr; 
397                  radius[y][i] = ((radius[y][i]) / (maxr - minr)); 
398              } 
399          } 
400   
401          MatFloat mf = new MatFloat(radius); 
402          String fn = Futil.getWriteFile("flt.gz file").toString(); 
403          mf.saveAsgz(fn); 
404          mf.readAsgz(fn); 
405          graphics.ddd.MainFrame.image3D(getImage(), radius); 
406      } 
407   
408      public void processMovieImage(double t) { 
409          System.out.println("t=" + t); 
410          polarTransform(0.5, t * 2); 
411          fishEye((getImageWidth() / 2), 
412                  (getImageHeight() / 2), 2 * t + 2.1); 
413          sqrt(t); 
414      } 
415   
416      AnimateFrame af = new AnimateFrame(); 
417   
418      public void makeMovie() { 
419          String files[] = getFileNames(); 
420          for (int i = 0; i < files.length; i++) { 
421              openGif(files[i]); 
422              processMovieImage(i / (double) files.length); 
423              int outFileNumber = i + files.length; 
424              String outFileName = getDir() + "eclaire" + outFileNumber + ".GIF"; 
425              System.out.println("reading:" + files[i]); 
426              System.out.println("writing:" + outFileName); 
427              saveAsGif(outFileName); 
428          } 
429      } 
430   
431      public void processDiffractionImages() { 
432          int y = 0; 
433          int x = 0; 
434          String files[] = getFileNames(); 
435          short zimage[][] = new short[files.length][256]; 
436          for (int i = 0; i < files.length; i++) { 
437              openGif(files[i]); 
438              processDiffractionImage(); 
439              try { 
440                  for (y = 0; y < getImageHeight(); y++) 
441                      for (x = 0; x < getImageWidth(); x++) { 
442                          if (shortImageBean.getR()[x][y] == 0) continue; 
443                          zimage[i][y] = (short) x; 
444                      } 
445              } catch (Exception e) { 
446                  System.out.println(e + "x,y=" + x + "," + y); 
447              } 
448          } 
449          short[][] r = Mat2.copyArray(zimage); 
450          shortImageBean.setR(r); 
451          setImageWidth(files.length); 
452          int height1 = getImageHeight(); 
453          setImageHeight(height1); 
454          shortImageBean.copyRedToGreenAndBlue(); 
455          short2Image(); 
456      } 
457   
458      public void ls() { 
459          String files[] = getFileNames(); 
460          for (int i = 0; i < files.length; i++) { 
461              File f = new File(files[i]); 
462              String fn = f.getName() + "\t\t\t"; 
463              if (f.canRead()) 
464                  fn = fn + "r"; 
465              else 
466                  fn = fn + "-"; 
467              if (f.canWrite()) 
468                  fn = fn + "w"; 
469              else 
470                  fn = fn + "-"; 
471              fn = fn + "\t" + f.length(); 
472              System.out.println(fn); 
473          } 
474      } 
475   
476      public void rename() { 
477          String files[] = getFileNames(); 
478          for (int i = 0; i < files.length; i++) { 
479              File f = new File(files[i]); 
480              String fn = f.getName(); 
481              File f2 = new File(getDir() + "d" + fn); 
482              if (fn.length() > 13) 
483                  f.renameTo(f2); 
484          } 
485      } 
486   
487      public void systemInfo() { 
488          System.out.println(getTitle()); 
489          computeInfo(); 
490   
491      } 
492   
493      private void computeInfo() { 
494          SystemUtils.printProp("java.version"); 
495          SystemUtils.printProp("java.class.version"); 
496          getOsArch(); 
497          SystemUtils.printProp("os.name"); 
498          SystemUtils.printProp("os.version"); 
499          SystemUtils.printProp("java.vendor"); 
500          SystemUtils.printProp("java.vendor.url"); 
501          SystemUtils.getUserHome(); 
502          SystemUtils.userDir(); 
503          SystemUtils.home(); 
504           
505      } 
506   
507      private void getOsArch() { 
508          SystemUtils.printProp("os.arch"); 
509          free(); 
510      } 
511   
512      public static void printProps(InfoFrame infoFrame1) { 
513          Properties props = System.getProperties(); 
514          for (Enumeration e = 
515                  props.propertyNames(); e.hasMoreElements();) { 
516              String key = (String) e.nextElement(); 
517              infoFrame1.println(key + " = " + (String) (props.get(key))); 
518          } 
519      } 
520   
521      public void threeDImage() { 
522          if (getChild() == null) { 
523              image3d(shortImageBean.getImage(), shortImageBean.getR()); 
524              return; 
525          } 
526          image3d(getChild().getImage(), shortImageBean.getR()); 
527   
528      } 
529   
530      public static void image3d(Image img, short i[][]) { 
531          //graphics.ddd.MainFrame.image3D(img, i); 
532          ip.raul.SnowManFrame.image3D(img, i); 
533      } 
534   
535      public void printMethods(Method methods[]) { 
536          for (int i = 0; i < methods.length; i++) { 
537              infoFrame.print(methods[i] + "\t"); 
538              if (i % 4 == 0) System.out.println(); 
539          } 
540      } 
541   
542      public void printMethodNames(Method ma[]) { 
543          int length = 0; 
544          for (int i = 0; i < ma.length; i++) { 
545              infoFrame.print(ma[i].getName() + "   "); 
546              length += ma[i].getName().length() + 1; 
547              if (length > 70) { 
548                  infoFrame.print("\n.."); 
549                  length = 0; 
550              } 
551          } 
552          infoFrame.println(); 
553      } 
554   
555      public static void drawTest() { 
556          String args[] = {""}; 
557          bookExamples.ch26Graphics.draw2d.DrawTest.main(args); 
558      } 
559   
560      public Method[] getMethodsWithNoArguments() { 
561          Vector v = new Vector(); 
562          Class c = this.getClass(); 
563          Method ma[] = c.getMethods(); 
564          for (int i = 0; i < ma.length; i++) { 
565              Class ca[] = ma[i].getParameterTypes(); 
566              if (ca.length == 0) 
567                  v.addElement(ma[i]); 
568          } 
569          ma = new Method[v.size()]; 
570          for (int i = 0; i < v.size(); i++) 
571              ma[i] = ((Method) v.elementAt(i)); 
572          return ma; 
573      } 
574   
575      public void printMethods() { 
576          printMethodNames(getMethodsWithNoArguments()); 
577      } 
578   
579   
580      public void commandLine() { 
581          free(); 
582          println("Type 'help' to get help"); 
583   
584      } 
585   
586      Timer t = new Timer(); 
587   
588      public void startTime() { 
589          t.start(); 
590      } 
591   
592      public void printTime() { 
593          println("Command executed in " + 
594                  t.getElapsedTime() + 
595                  " seconds"); 
596      } 
597   
598      public void free() { 
599          futils.Exec e = new futils.Exec(); 
600          println(e.getPrintRamString()); 
601      } 
602   
603      /* The following will not compile on a mac! 
604   
605      public void printAllMethods() { 
606         Class c = this.getClass(); 
607         printMethodNamesAndParameters(getAllMethods()); 
608   
609      } 
610       public void printMethodNamesAndParameters(Method ma[]) { 
611   
612         for (int i=0; i < ma.length; i++) { 
613             Class ca[] = ma[i].getParameterTypes(); 
614             infoFrame.print(ma[i].getName()+"("); 
615   
616             if (ca != null)  { 
617               for (int j=0; j < ca.length;j++) { 
618                 infoFrame.print(ca[j].getName()+","); 
619                 if (ca[j] instanceof Object) 
620                     printMethodNamesAndParameters(ca[j].getMethods()); 
621               } 
622   
623               infoFrame.print(")"); 
624             } 
625             infoFrame.print("\n.."); 
626         } 
627      } 
628      */ 
629      public Method[] getAllMethods() { 
630          Vector v = new Vector(); 
631          Class c = this.getClass(); 
632          Method ma[] = c.getMethods(); 
633          for (int i = 0; i < ma.length; i++) 
634              v.addElement(ma[i]); 
635   
636          ma = new Method[v.size()]; 
637          for (int i = 0; i < v.size(); i++) 
638              ma[i] = ((Method) v.elementAt(i)); 
639          return ma; 
640      } 
641   
642      public void rmic() { 
643          net.rmi.utils.Compile.rmic(); 
644          println("stubs created"); 
645      } 
646   
647      public void server() { 
648          println("running server...ta da!"); 
649          net.rmi.rmiimage.Server.run(); 
650      } 
651   
652      public void help() { 
653          infoFrame.println( 
654   
655                  "Consulting and teaching services are available\n\n" + 
656                  "Web Page: <http://www.DocJava.com> \n" + 
657                  "Internet: lyon@DocJava.com\n" + 
658                  "\nType a method name and carrage return\n" + 
659                  "and the method will be invoked\n" + 
660                  "'quit' - exits from CLI\n" + 
661                  "'printMethods' will print the methods\n" 
662          ); 
663          this.gabor(); 
664      } 
665   
666      public void benchMark() { 
667          BenchMark bm = new BenchMark(); 
668          bm.run(this); 
669      } 
670  } 
671   
672