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

1    package ip.raul; 
2     
3    import futils.Futil; 
4    import ip.gui.dialog.DoubleLog; 
5    import ip.gui.frames.ImageFrame; 
6     
7    import java.awt.*; 
8    import java.awt.event.ActionEvent; 
9    import java.awt.event.ActionListener; 
10   import java.awt.event.ItemEvent; 
11   import java.awt.event.ItemListener; 
12   import java.io.File; 
13    
14    
15   public class Editor3D extends ImageFrame 
16           implements ActionListener, ItemListener { 
17    
18       MenuBar mb = new MenuBar(); 
19       Menu ModelMenu = new Menu("Model"); 
20       Menu WindowMenu = new Menu("Window"); 
21       Menu ViewsMenu = new Menu("Views"); 
22    
23       MenuItem props_mi = addMenuItem(ModelMenu, "[s]how object info"); 
24       MenuItem save_mi = addMenuItem(ModelMenu, "[S]ave scene"); 
25       MenuItem open_mi = addMenuItem(ModelMenu, "[O]pen scene"); 
26       MenuItem texture_mi = addMenuItem(ModelMenu, "[T] Add texture"); 
27       MenuItem sphere_mi = addMenuItem(ModelMenu, "[1] sphere"); 
28       MenuItem cylinder_mi = addMenuItem(ModelMenu, "[2] cylinder"); 
29       MenuItem paralelipiped_mi = addMenuItem(ModelMenu, "[3] Paralelipiped"); 
30       MenuItem snowman_mi = addMenuItem(ModelMenu, "[4] snowMan"); 
31    
32       MenuItem frontview_mi = addMenuItem(ViewsMenu, "Front"); 
33       MenuItem backview_mi = addMenuItem(ViewsMenu, "Back"); 
34       MenuItem leftview_mi = addMenuItem(ViewsMenu, "Left"); 
35       MenuItem rightview_mi = addMenuItem(ViewsMenu, "Right"); 
36       MenuItem topview_mi = addMenuItem(ViewsMenu, "Top"); 
37       MenuItem bottomview_mi = addMenuItem(ViewsMenu, "Bottom"); 
38    
39       MenuItem threeD_mi = addMenuItem(ViewsMenu, "Full 3D mode"); 
40    
41       static int maxALL = 20; 
42       private Object3D objects[] = new Object3D[maxALL]; 
43       private String textures[] = new String[maxALL]; 
44       Image img = null; 
45       private int maxTextures = -1; 
46       private int maxObjects = -1; 
47       private int objectType = 1; 
48       ObjectInfo oi = null; 
49       DoubleLog DLog = null; 
50       private ObjectView front = null; 
51       private ObjectView back = null; 
52       private ObjectView top = null; 
53       private ObjectView bottom = null; 
54       private ObjectView left = null; 
55       private ObjectView right = null; 
56       private SnowManFrame _3D = null; 
57       public boolean busyView = false; 
58       public int indexView = 1; 
59    
60       public void itemStateChanged(ItemEvent e) { 
61           if (e.getSource() == oi.names) { 
62               indexView = oi.names.getSelectedIndex(); 
63               busyView = true; 
64               updateViews(); 
65               return; 
66           } 
67       } 
68    
69       public void actionPerformed(ActionEvent e) { 
70           try { 
71               Button b = (Button) e.getSource(); 
72               if (b == oi.setButton) { 
73                   objects = oi.objects; 
74                   for (int i = 0; i <= maxObjects; i++) { 
75                       if (objects[i].textureIndex >= 0) 
76                           objects[i].Texture = textures[objects[i].textureIndex]; 
77                   } 
78                   busyView = false; 
79                   updateViews(); 
80               } 
81               if (b == oi.cancelButton) { 
82                   busyView = false; 
83                   updateViews(); 
84               } 
85           } catch (Exception ex) { 
86           } 
87           try { 
88               Button b = (Button) e.getSource(); 
89               if (b == DLog.setButton) { 
90                   double d[] = DLog.getUserInputAsDouble(); 
91                   switch (objectType) { 
92                       case 1: 
93                           addSphere(d[0], d[1], d[2], d[3]); 
94                           break; 
95                       case 2: 
96                           addCylinder(d[0], d[1], d[2], d[3], d[4]); 
97                           break; 
98                       case 3: 
99                           addParalelipiped(d[0], d[1], d[2], d[3], d[4], d[5]); 
100                          break; 
101                  } 
102                  updateViews(); 
103              } 
104          } catch (Exception ex) { 
105          } 
106          if (match(e, texture_mi)) { 
107              maxTextures++; 
108              String fn = Futil.getReadFileName(); 
109              if (fn == null) return; 
110              textures[maxTextures] = fn; 
111              System.out.println("Texture " + maxTextures + "(" + fn + ") Added..."); 
112              return; 
113          } 
114          if (match(e, props_mi)) { 
115              Properties(); 
116              return; 
117          } 
118          if (match(e, threeD_mi)) { 
119              showFull3D(); 
120              return; 
121          } 
122          if (match(e, frontview_mi)) { 
123              if (front == null) 
124                  front = new ObjectView("Front View", objects, maxObjects, textures, maxTextures, 1); 
125              else { 
126                  front.show(); 
127                  front.hidden = false; 
128                  updateViews(); 
129              } 
130              return; 
131          } 
132          if (match(e, backview_mi)) { 
133              if (back == null) 
134                  back = new ObjectView("Back View", objects, maxObjects, textures, maxTextures, 2); 
135              else { 
136                  back.show(); 
137                  back.hidden = false; 
138                  updateViews(); 
139              } 
140              return; 
141          } 
142          if (match(e, topview_mi)) { 
143              if (top == null) 
144                  top = new ObjectView("Top View", objects, maxObjects, textures, maxTextures, 3); 
145              else { 
146                  top.show(); 
147                  top.hidden = false; 
148                  updateViews(); 
149              } 
150              return; 
151          } 
152          if (match(e, bottomview_mi)) { 
153              if (bottom == null) 
154                  bottom = new ObjectView("Bottom View", objects, maxObjects, textures, maxTextures, 4); 
155              else { 
156                  bottom.show(); 
157                  bottom.hidden = false; 
158                  updateViews(); 
159              } 
160              return; 
161          } 
162          if (match(e, leftview_mi)) { 
163              if (left == null) 
164                  left = new ObjectView("Left View", objects, maxObjects, textures, maxTextures, 5); 
165              else { 
166                  left.show(); 
167                  left.hidden = false; 
168                  updateViews(); 
169              } 
170              return; 
171          } 
172          if (match(e, rightview_mi)) { 
173              if (right == null) 
174                  right = new ObjectView("Right View", objects, maxObjects, textures, maxTextures, 6); 
175              else { 
176                  right.show(); 
177                  right.hidden = false; 
178                  updateViews(); 
179              } 
180              return; 
181          } 
182          if (match(e, sphere_mi)) { 
183              addSphere(); 
184              return; 
185          } 
186          if (match(e, cylinder_mi)) { 
187              addCylinder(); 
188              return; 
189          } 
190          if (match(e, paralelipiped_mi)) { 
191              addParalelipiped(); 
192              return; 
193          } 
194          if (match(e, snowman_mi)) { 
195              addSphere(0.05, -0.3, 0.0, 0.3); 
196              addSphere(0.05, 0.06, 0.0, 0.24); 
197              addSphere(0.05, 0.19, -0.205, 0.01); 
198              addSphere(0.05, 0.09, -0.237, 0.01); 
199              addSphere(0.05, 0.0, -0.230, 0.01); 
200              addSphere(0.05, 0.32, -0.205, 0.018); 
201              addSphere(0.05, 0.34, 0.0, 0.2); 
202              addSphere(0.1, 0.4, -0.195, 0.02); 
203              addSphere(-0.02, 0.393, -0.185, 0.02); 
204              updateViews(); 
205          } 
206          super.actionPerformed(e); 
207      } 
208   
209      public void updateViews() { 
210          if (front != null) 
211              if (!front.hidden) { 
212                  front.maxObjects = maxObjects; 
213                  front.objects = objects; 
214                  front.index = indexView; 
215                  front.busy = busyView; 
216                  front.repaint(); 
217              } 
218          if (back != null) 
219              if (!back.hidden) { 
220                  back.maxObjects = maxObjects; 
221                  back.objects = objects; 
222                  back.index = indexView; 
223                  back.busy = busyView; 
224                  back.repaint(); 
225              } 
226          if (top != null) 
227              if (!top.hidden) { 
228                  top.maxObjects = maxObjects; 
229                  top.objects = objects; 
230                  top.index = indexView; 
231                  top.busy = busyView; 
232                  top.repaint(); 
233              } 
234          if (bottom != null) 
235              if (!bottom.hidden) { 
236                  bottom.maxObjects = maxObjects; 
237                  bottom.objects = objects; 
238                  bottom.index = indexView; 
239                  bottom.busy = busyView; 
240                  bottom.repaint(); 
241              } 
242          if (left != null) 
243              if (!left.hidden) { 
244                  left.maxObjects = maxObjects; 
245                  left.objects = objects; 
246                  left.index = indexView; 
247                  left.busy = busyView; 
248                  left.repaint(); 
249              } 
250          if (right != null) 
251              if (!right.hidden) { 
252                  right.maxObjects = maxObjects; 
253                  right.objects = objects; 
254                  right.index = indexView; 
255                  right.busy = busyView; 
256                  right.repaint(); 
257              } 
258      } 
259   
260      Editor3D(String title) { 
261          super(title); 
262          init(); 
263          mb.add(ModelMenu); 
264          WindowMenu.add(ViewsMenu); 
265          mb.add(WindowMenu); 
266          setMenuBar(mb); 
267      } 
268   
269      private void init() { 
270      } 
271   
272      public void Properties() { 
273          if (maxObjects >= 0) { 
274              oi = new ObjectInfo( 
275                      "Show Object Info...", objects, maxObjects, maxTextures); 
276              oi.setVisible(true); 
277              oi.setButton.addActionListener(this); 
278              oi.cancelButton.addActionListener(this); 
279              oi.names.addItemListener(this); 
280              indexView = 0; 
281              busyView = true; 
282              updateViews(); 
283          } else 
284              System.out.println("No objects to show."); 
285      } 
286   
287      public void showFull3D() { 
288          Image img1 = null; 
289          int indx = 0; 
290          if (_3D == null) 
291              _3D = new SnowManFrame(); 
292          else { 
293              _3D.show(); 
294          } 
295          _3D.setSize(400, 400); 
296          _3D.setVisible(true); 
297          _3D.initialize(); 
298          System.out.println("Loading textures..."); 
299          for (int i = 0; i <= maxTextures; i++) { 
300              System.out.println(textures[i]); 
301              File f = new File(textures[i]); 
302              if (!f.exists()) break; 
303              img = Toolkit.getDefaultToolkit().getImage(textures[i]); 
304              _3D.demo.addTexture(img); 
305          } 
306          System.out.println("Textures loaded."); 
307          _3D.start(); 
308          for (int i = 0; i <= maxObjects; i++) { 
309              indx = objects[i].textureIndex; 
310              indx = indx + 2; 
311  //  if (indx==0) indx=1; 
312              switch (objects[i].Type) { 
313                  case 1: 
314                      _3D.addSphere(objects[i].Pos.x, 
315                              objects[i].Pos.y, 
316                              objects[i].Pos.z, 
317                              objects[i].Radius, indx); 
318                      break; 
319                  case 2: 
320                      _3D.addCylinder(objects[i].Pos.x, 
321                              objects[i].Pos.y, 
322                              objects[i].Pos.z, 
323                              objects[i].Radius, 
324                              objects[i].Height, indx); 
325                      break; 
326                  case 3: 
327                      _3D.addParalelipiped(objects[i].Pos.x, 
328                              objects[i].Pos.y, 
329                              objects[i].Pos.z, 
330                              objects[i].Width, 
331                              objects[i].Length, 
332                              objects[i].Height, indx); 
333                      break; 
334              } 
335          } 
336          for (int i = 1; i <= _3D.demo.objects; i++) 
337              _3D.demo.object[i].mode = 6; 
338      } 
339   
340      public void addSphere() { 
341          String prompts[] = {"x=", "y=", "z=", "R="}; 
342          String defaults[] = {"0.0", "0.0", "0.0", "0.3"}; 
343          DLog = new DoubleLog(this, "Sphere:", prompts, defaults, 6); 
344          DLog.setVisible(true); 
345          DLog.setButton.addActionListener(this); 
346          objectType = 1; 
347      } 
348   
349      public void addCylinder() { 
350          String prompts[] = {"x=", "y=", "z=", "R=", "H="}; 
351          String defaults[] = {"0.0", "0.0", "0.0", "0.3", "0.3"}; 
352          DLog = new DoubleLog(this, "Cylinder:", prompts, defaults, 6); 
353          DLog.setVisible(true); 
354          DLog.setButton.addActionListener(this); 
355          objectType = 2; 
356      } 
357   
358      public void addParalelipiped() { 
359          String prompts[] = {"x=", "y=", "z=", "W=", "L=", "H="}; 
360          String defaults[] = {"0.0", "0.0", "0.0", "0.3", "0.3", "0.3"}; 
361          DLog = new DoubleLog(this, "Paralelipiped::", prompts, defaults, 6); 
362          DLog.setVisible(true); 
363          DLog.setButton.addActionListener(this); 
364          objectType = 3; 
365      } 
366   
367      public void addSphere(double _x, double _y, double _z, double R) { 
368          maxObjects++; 
369          objects[maxObjects] = new Object3D(maxObjects + "-Sphere", 1); 
370          objects[maxObjects].Radius = (float) R; 
371          objects[maxObjects].Pos = new Point3D((float) _x, (float) _y, (float) _z); 
372          System.out.println("Object " + maxObjects + "-Sphere" + " Added..."); 
373      } 
374   
375      public void addCylinder(double _x, double _y, double _z, double R, double H) { 
376          maxObjects++; 
377          objects[maxObjects] = new Object3D(maxObjects + "-Cylinder", 2); 
378          objects[maxObjects].Radius = (float) R; 
379          objects[maxObjects].Height = (float) H; 
380          objects[maxObjects].Pos = new Point3D((float) _x, (float) (_y - (float) (H / 2d)), (float) _z); 
381          System.out.println("Object " + maxObjects + "-Cylinder" + " Added..."); 
382      } 
383   
384      public void addParalelipiped(double _x, double _y, double _z, double W, double L, double H) { 
385          maxObjects++; 
386          objects[maxObjects] = new Object3D(maxObjects + "-Paralelipiped", 3); 
387          objects[maxObjects].Width = (float) W; 
388          objects[maxObjects].Height = (float) H; 
389          objects[maxObjects].Length = (float) L; 
390          objects[maxObjects].Pos = new Point3D((float) _x, (float) _y, (float) _z); 
391          System.out.println("Object " + maxObjects + "-Paralelipiped" + " Added..."); 
392      } 
393   
394      public static void main(String args[]) { 
395          Editor3D mL = new Editor3D( 
396                  "Editor3D"); 
397          mL.setSize(150, 150); 
398          mL.setVisible(true); 
399      } 
400  } 
401