/Users/lyon/j4p/src/ip/raul/SnowManFrame.java

1    package ip.raul; 
2     
3    import java.awt.*; 
4    import java.awt.event.*; 
5    import graphics.NumImage; 
6     
7     
8    public class SnowManFrame extends Frame 
9            implements Runnable, KeyListener, WindowListener, 
10           MouseListener, MouseMotionListener { 
11    
12       Thread idx_Thread = null; 
13       boolean initialized = false; 
14       public Applet3d demo; 
15       float oldx,oldy; 
16       float xrot = 64; 
17       float yrot = 32; 
18       float zrot = 17; 
19       float dx = 0; 
20       float dy = 0; 
21       float dz = 0; 
22       int mode; 
23       boolean autorot = true; 
24    
25       public static void main(String args[]) { 
26           SnowManFrame f = new SnowManFrame(); 
27           f.setSize(400, 400); 
28           f.setVisible(true); 
29           f.initialize(); 
30           f.start(); 
31           f.addSnowMan(); 
32           for (int i = 1; i <= f.demo.objects; i++) 
33               f.demo.object[i].mode = 6; 
34       } 
35    
36       public static void image3D() { 
37           SnowManFrame f = new SnowManFrame(); 
38           f.setSize(400, 400); 
39           f.setVisible(true); 
40           f.initialize(); 
41           f.start(); 
42           for (int i = 1; i <= f.demo.objects; i++) 
43               f.demo.object[i].mode = 6; 
44       } 
45    
46       void addImageField(short s[][]) { 
47           int fieldres = 40; 
48           int fieldcolor = demo.getIntColor(255, 96, 0); 
49           float map[][] = new float[fieldres][fieldres]; 
50           for (int i = 1; i < fieldres - 1; i++) { 
51               for (int j = 1; j < fieldres - 1; j++) { 
52                   int x = s.length * i / fieldres; 
53                   int y = s[0].length * j / fieldres; 
54                   map[i][j] = (float) (s[x][y] / 255.0); 
55               } 
56           } 
57           demo.generateField(map, fieldres, fieldres, mode, fieldcolor); 
58           demo.object[1].texture = 1; 
59       } 
60    
61       public void initialize(Image i, short s[][]) { 
62           addListeners(); 
63           demo = new Applet3d(getSize().width, getSize().height); 
64           int mode = 1; 
65           demo.addTexture(i); 
66           addImageField(s); 
67           addLights(); 
68           demo.reflectivity = 200; 
69           demo.setStatic(); 
70           demo.scaleWorld(1.6f); 
71           demo.rotateWorld(0f, 0f, 150f); 
72           demo.shiftObject(1, 0, -.8f, 0); 
73           initialized = true; 
74   //      autorot=false; 
75       } 
76    
77       public static void image3D(Image img, short s[][]) { 
78           SnowManFrame f = new SnowManFrame(); 
79           f.setSize(s.length, s[0].length); 
80           f.setVisible(true); 
81           f.initialize(img, s); 
82           f.start(); 
83           for (int i = 1; i <= f.demo.objects; i++) { 
84               f.demo.object[i].mode = 6; 
85           } 
86       } 
87    
88    
89       public void addSphere(double xc, double yc, double zc, double radius, int texture) { 
90           float r = 0; 
91           float ry = 0; 
92           int scancolor = demo.getIntColor(60, 255, 20); 
93           demo.addObject(mode, scancolor); 
94           int obj = demo.objects; 
95           float twoPi = 2f * (float) Math.PI; 
96           float x1, y1, z1; 
97           int numberOfSteps = 8; 
98           int h = 8; 
99           float deltaTheta = (twoPi / (numberOfSteps - 1f)); 
100          float deltaY = (float) (2f * radius / (h - 1f)); 
101          for (int i = 0; i <= numberOfSteps; i++) 
102              for (int y = 0; y < h; y++) { 
103                  ry = (y * deltaY); 
104                  r = (ry * (float) (2f * radius - ry)); 
105                  r = (float) Math.sqrt(r); 
106                  x1 = (float) xc + (float) (r * Math.sin((i * deltaTheta))); 
107                  y1 = (float) yc - (float) radius + ry; 
108                  z1 = (float) zc + (float) (r * Math.cos((i * deltaTheta))); 
109                  demo.addNode(obj, x1, y1, z1); 
110              } 
111          demo.generateScanObject1(obj, h, numberOfSteps); 
112          demo.object[obj].texture = texture; 
113      } 
114   
115      public void addCylinder(double xc, double yc, double zc, double R, double H, int texture) { 
116          float r = 0; 
117          float ry = 0; 
118          int scancolor = demo.getIntColor(60, 255, 20); 
119          demo.addObject(mode, scancolor); 
120          int obj = demo.objects; 
121          float twoPi = 2f * (float) Math.PI; 
122          float x1, y1, z1; 
123          int numberOfSteps = 12; 
124          int h = 2; 
125          float deltaTheta = (twoPi / (numberOfSteps - 1f)); 
126          double deltaY = (H / (double) (h - 1f)); 
127          for (int i = 0; i <= numberOfSteps; i++) { 
128              for (int y = 0; y < h; y++) { 
129                  x1 = (float) xc + (float) (R * Math.sin((i * deltaTheta))); 
130                  y1 = (float) yc + (float) (y * deltaY); 
131                  z1 = (float) zc + (float) (R * Math.cos((i * deltaTheta))); 
132                  if (y == 0) 
133                      demo.addNode(obj, (float) xc, (float) yc, (float) zc); 
134                  demo.addNode(obj, x1, y1, z1); 
135                  if (y == h - 1) 
136                      demo.addNode(obj, (float) xc, (float) (yc + H), (float) zc); 
137              } 
138          } 
139          demo.generateScanObject1(obj, h + 2, numberOfSteps); 
140          demo.object[obj].texture = texture; 
141      } 
142   
143      public void addParalelipiped(double xc, double yc, double zc, double W, double L, double H, int texture) { 
144          int scancolor = demo.getIntColor(60, 255, 20); 
145          demo.addObject(mode, scancolor); 
146          int obj = demo.objects; 
147          float x1, y1, z1; 
148          x1 = (float) xc - (float) (W / 2f); 
149          y1 = (float) yc - (float) (H / 2f); 
150          z1 = (float) zc - (float) (L / 2f); 
151          //side 1 
152          demo.addNode(obj, (float) xc, (float) (yc - H / 2), (float) zc); 
153          demo.addNode(obj, x1, y1, z1); 
154          y1 = (float) yc + (float) (H / 2f); 
155          demo.addNode(obj, x1, y1, z1); 
156          demo.addNode(obj, (float) xc, (float) (yc + H / 2), (float) zc); 
157          y1 = (float) yc - (float) (H / 2f); 
158          //side 2 
159          x1 = (float) xc + (float) (W / 2f); 
160          demo.addNode(obj, (float) xc, (float) (yc - H / 2), (float) zc); 
161          demo.addNode(obj, x1, y1, z1); 
162          y1 = (float) yc + (float) (H / 2f); 
163          demo.addNode(obj, x1, y1, z1); 
164          demo.addNode(obj, (float) xc, (float) (yc + H / 2), (float) zc); 
165          y1 = (float) yc - (float) (H / 2f); 
166          //side 3 
167          z1 = (float) zc + (float) (L / 2f); 
168          demo.addNode(obj, (float) xc, (float) (yc - H / 2), (float) zc); 
169          demo.addNode(obj, x1, y1, z1); 
170          y1 = (float) yc + (float) (H / 2f); 
171          demo.addNode(obj, x1, y1, z1); 
172          demo.addNode(obj, (float) xc, (float) (yc + H / 2), (float) zc); 
173          y1 = (float) yc - (float) (H / 2f); 
174          //side4 
175          x1 = (float) xc - (float) (W / 2f); 
176          demo.addNode(obj, (float) xc, (float) (yc - H / 2), (float) zc); 
177          demo.addNode(obj, x1, y1, z1); 
178          y1 = (float) yc + (float) (H / 2f); 
179          demo.addNode(obj, x1, y1, z1); 
180          demo.addNode(obj, (float) xc, (float) (yc + H / 2), (float) zc); 
181          y1 = (float) yc - (float) (H / 2f); 
182          //side5 
183          z1 = (float) zc - (float) (L / 2f); 
184          demo.addNode(obj, (float) xc, (float) (yc - H / 2), (float) zc); 
185          demo.addNode(obj, x1, y1, z1); 
186          y1 = (float) yc + (float) (H / 2f); 
187          demo.addNode(obj, x1, y1, z1); 
188          demo.addNode(obj, (float) xc, (float) (yc + H / 2), (float) zc); 
189          y1 = (float) yc - (float) (H / 2f); 
190   
191          demo.generateScanObject1(obj, 4, 4); 
192          demo.object[obj].texture = texture; 
193      } 
194   
195   
196      public void initialize() { 
197          addListeners(); 
198          demo = new Applet3d(getSize().width, getSize().height); 
199          int mode = 2; 
200          demo.addTexture(NumImage.getImage()); 
201          addLights(); 
202          demo.reflectivity = 200; 
203          demo.setStatic(); 
204          autorot = false; 
205          initialized = true; 
206      } 
207   
208      public void addSnowMan() { 
209          addSphere(0.05, -0.3, 0.0, 0.3, 0); 
210          addSphere(0.05, 0.06, 0.0, 0.24, 0); 
211          addSphere(0.05, 0.19, -0.205, 0.01, 0); 
212          addSphere(0.05, 0.09, -0.237, 0.01, 0); 
213          addSphere(0.05, 0.0, -0.230, 0.01, 0); 
214          addSphere(0.05, 0.32, -0.205, 0.018, 0); 
215          addSphere(0.05, 0.34, 0.0, 0.2, 0); 
216          addSphere(0.1, 0.4, -0.195, 0.02, 0); 
217          addSphere(-0.02, 0.393, -0.185, 0.02, 0); 
218      } 
219   
220      public void paint(Graphics gx) { 
221          gx.setColor(Color.black); 
222          gx.fillRect(0, 0, this.getSize().width, this.getSize().height); 
223          gx.setColor(new Color(0, 255, 0)); 
224      } 
225   
226      public void start() { 
227          if (idx_Thread == null) { 
228              idx_Thread = new Thread(this); 
229              idx_Thread.start(); 
230          } 
231      } 
232   
233      public void stop() { 
234          if (idx_Thread != null) { 
235              idx_Thread = null; 
236          } 
237      } 
238   
239      public void run() { 
240          while (true) { 
241              repaint(); 
242              try { 
243                  idx_Thread.sleep(20); 
244              } catch (InterruptedException e) { 
245                  System.out.println("graphics.idx://interrupted"); 
246              } 
247          } 
248      } 
249   
250      public void update(Graphics g) { 
251          if (initialized) { 
252              if (autorot) { 
253                  demo.rotateWorld(0, 5, 0); 
254              } 
255              g.drawImage(demo.renderScene(), 0, 0, this); 
256          } else { 
257              paint(g); 
258              initialize(); 
259          } 
260      } 
261   
262      void addImageField() { 
263          int fieldres = 20; 
264          int fieldcolor = demo.getIntColor(255, 96, 0); 
265          float map[][] = new float[fieldres][fieldres]; 
266          for (int i = 2; i < fieldres; i++) { 
267              for (int j = 2; j < fieldres; j++) { 
268                  int x = NumImage.gray.length * i / fieldres; 
269                  int y = NumImage.gray[0].length * j / fieldres; 
270                  map[i][j] = (float) (NumImage.gray[x][y] / 255.0); 
271              } 
272          } 
273          demo.generateField(map, fieldres, fieldres, mode, fieldcolor); 
274          demo.object[1].texture = 1; 
275          demo.rotateObject(1, 0, (float) 180, (float) 180); 
276          demo.shiftObject(1, 0, (float) 0.5, 0); 
277      } 
278   
279      void addLights() { 
280          demo.ambient = 48; 
281          demo.setPhong(64); 
282          demo.addLight(new idx3d_vector((float) 0, (float) 0, (float) -1), 1, 164); 
283          demo.addLight(new idx3d_vector((float) 2, (float) -4, (float) -1), 1, 144); 
284      } 
285   
286      private static void waitForImage(Component component, Image image) { 
287          MediaTracker tracker = new MediaTracker(component); 
288          try { 
289              tracker.addImage(image, 0); 
290              tracker.waitForID(0); 
291              if (!tracker.checkID(0)) 
292                  System.out.println("Load failure!"); 
293          } catch (InterruptedException e) { 
294          } 
295      } 
296   
297      public void addListeners() { 
298          addWindowListener(this); 
299          addMouseListener(this); 
300          addMouseMotionListener(this); 
301          addKeyListener(this); 
302      } 
303   
304      public void keyTyped(KeyEvent e) { 
305          mode = (mode + 1) % 8; 
306          for (int i = 1; i <= demo.objects; i++) 
307              demo.object[i].mode = mode; 
308      } 
309   
310      public void mousePressed(MouseEvent evt) { 
311          autorot = false; 
312          oldx = evt.getX(); 
313          oldy = evt.getY(); 
314      } 
315   
316      public void mouseReleased(MouseEvent evt) { 
317          autorot = true; 
318          oldx = evt.getX(); 
319          oldy = evt.getY(); 
320      } 
321   
322      public void mouseDragged(MouseEvent evt) { 
323          int x = evt.getX(); 
324          int y = evt.getY(); 
325          demo.rotateWorld((oldy - y), (oldx - x), 0); 
326          dx = (dx + oldx - x + 360) % 360; 
327          dy = (dy + oldy - y + 360) % 360; 
328          oldx = x; 
329          oldy = y; 
330      } 
331   
332      public void mouseMoved(MouseEvent evt) { 
333      }; 
334      public void mouseClicked(MouseEvent evt) { 
335      }; 
336      public void mouseEntered(MouseEvent evt) { 
337      }; 
338      public void mouseExited(MouseEvent evt) { 
339      }; 
340   
341      public void windowClosing(WindowEvent e) { 
342          dispose(); 
343      } 
344   
345      public void windowClosed(WindowEvent e) { 
346      }; 
347      public void windowDeiconified(WindowEvent e) { 
348      }; 
349      public void windowIconified(WindowEvent e) { 
350      }; 
351      public void windowActivated(WindowEvent e) { 
352      }; 
353      public void windowDeactivated(WindowEvent e) { 
354      }; 
355      public void windowOpened(WindowEvent e) { 
356      }; 
357      public void keyPressed(KeyEvent e) { 
358      }; 
359      public void keyReleased(KeyEvent e) { 
360      }; 
361   
362  } 
363