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

1    package ip.gui.frames; 
2     
3    import j2d.ImageUtils; 
4    import utils.Print; 
5     
6    import java.awt.*; 
7    import java.awt.event.ActionEvent; 
8    import java.net.URL; 
9     
10   public class GrabFrame extends FilterFrame { 
11    
12       private SnellWlx sw; 
13       private String dataHome = "http://show.docjava.com:8086/book/cgij/code/data/"; 
14       private String imagesHome = 
15               dataHome + "images/gifs/"; 
16       private String imagesColor = 
17               dataHome + "images/color/"; 
18       private String audioHome = 
19               dataHome + "audio/"; 
20    
21    
22       private Menu grabMenu = getMenu("Grab"); 
23       private Menu netMenu = getMenu("ip.net"); 
24       private MenuItem building_mi = addMenuItem(netMenu, "BUILDING.GIF"); 
25       private MenuItem plane_mi = addMenuItem(netMenu, "airplane.JPEG"); 
26       private MenuItem baboon_mi = addMenuItem(netMenu, "baboon.JPEG"); 
27       private MenuItem lena_mi = addMenuItem(netMenu, "lena.JPEG"); 
28       private MenuItem peppers_mi = addMenuItem(netMenu, "peppers.JPEG"); 
29       private MenuItem spaceMusic_mi = addMenuItem(netMenu, 
30               "spacemusic.au"); 
31    
32       private MenuItem testPattern_mi = addMenuItem(grabMenu, 
33               "[T]est Patterns"); 
34    
35       private MenuItem grabTestPattern_mi = addMenuItem(grabMenu, 
36               "[E-G]rab Patterns"); 
37    
38       public void actionPerformed(ActionEvent e) { 
39    
40           if (match(e, spaceMusic_mi)) { 
41               netSoundSelector(audioHome + "spacemusic.au"); 
42               return; 
43           } 
44    
45           if (match(e, plane_mi)) { 
46               netImageSelector(imagesColor + "airplane.JPEG"); 
47               return; 
48           } 
49           if (match(e, baboon_mi)) { 
50               netImageSelector(imagesColor + "baboon.JPEG"); 
51               return; 
52           } 
53           if (match(e, peppers_mi)) { 
54               netImageSelector(imagesColor + "peppers.JPEG"); 
55               return; 
56           } 
57           if (match(e, lena_mi)) { 
58               netImageSelector(imagesColor + "lena.JPEG"); 
59               return; 
60           } 
61           if (match(e, building_mi)) { 
62               netImageSelector(imagesHome + building_mi.getActionCommand()); 
63               return; 
64           } 
65    
66           if (match(e, testPattern_mi)) { 
67               testPattern(); 
68               return; 
69           } 
70           if (match(e, grabTestPattern_mi)) { 
71               grabTestPattern(); 
72               return; 
73           } 
74           super.actionPerformed(e); 
75       } 
76    
77       public GrabFrame(String title) { 
78           super(title); 
79           grabMenu.add(netMenu); 
80    
81           getFileMenu().add(grabMenu); 
82       } 
83    
84       public void testPattern() { 
85           sw = new SnellWlx(); 
86           sw.init(); 
87           sw.start(); 
88           sw.setSize(256, 256); 
89       } 
90    
91       public void revert() { 
92           if (urlString == null) { 
93               super.revert(); 
94               return; 
95           } 
96           netImageSelector(urlString); 
97       } 
98    
99       public static void netSoundSelector(String st) { 
100          URL url = null; 
101          try { 
102              url = new URL(st); 
103          } catch (Exception e) { 
104              e.printStackTrace(); 
105          } 
106          netSoundSelector(url); 
107      } 
108   
109      public static void netSoundSelector(URL url) { 
110          sound.UlawCodec.play(url); 
111      } 
112   
113      public void netImageSelector(String st) { 
114          Toolkit tk = Toolkit.getDefaultToolkit(); 
115          Image img; 
116          urlString = st; 
117          URL url = null; 
118          Print.println("Loading..." + st); 
119          try { 
120              url = new URL(st); 
121          } catch (Exception e) { 
122              e.printStackTrace(); 
123          } 
124   
125          img = tk.getImage(url); 
126          ImageUtils.waitForImage(this, img); 
127          if (img == null) { 
128              Print.println("netload failed"); 
129              return; 
130          } 
131          setImage(img); 
132          setSize(getImageWidth(), getImageHeight()); 
133          repaint(); 
134      } 
135   
136      private String urlString = null; 
137   
138      public void grab(Container f) { 
139          setImageWidth(f.getSize().width); 
140          setImageHeight(f.getSize().height); 
141          Image backBuffer = createImage(getImageWidth(), getImageHeight()); 
142          Graphics g = backBuffer.getGraphics(); 
143          f.paint(g); 
144          setImage(backBuffer); 
145          g = getGraphics(); 
146          g.clearRect(0, 0, getSize().width, getSize().height); 
147          setSize(getImageWidth(), getImageHeight()); 
148          repaint(); 
149      } 
150   
151      public void grabTestPattern() { 
152          if (sw == null) { 
153              testPattern(); 
154          } 
155          grab(sw); 
156      } 
157   
158      public static void main(String args[]) { 
159          GrabFrame gf = new GrabFrame("Grab Frame"); 
160          gf.testPattern(); 
161      } 
162   
163  } 
164