/Users/lyon/j4p/src/j3d/viewer/Viewer.java

1    package j3d.viewer; 
2     
3    /* 
4     * 
5     * (c) September 1998 by Mauro Marinilli 
6     * 
7     */ 
8     
9     
10   import com.sun.j3d.utils.applet.MainFrame; 
11   import com.sun.j3d.utils.geometry.ColorCube; 
12   import com.sun.j3d.utils.universe.SimpleUniverse; 
13   import j3d.Utils; 
14    
15   import javax.media.j3d.BranchGroup; 
16   import javax.media.j3d.Canvas3D; 
17   import javax.media.j3d.VirtualUniverse; 
18   import javax.swing.*; 
19   import javax.swing.event.TreeSelectionEvent; 
20   import javax.swing.event.TreeSelectionListener; 
21   import java.awt.*; 
22   import java.awt.event.MouseEvent; 
23   import java.awt.event.WindowEvent; 
24   import java.io.File; 
25   import java.io.FileInputStream; 
26   import java.io.ObjectInputStream; 
27    
28    
29   public class Viewer extends JFrame { 
30       JToolBar jToolBar1 = new JToolBar(); 
31       JButton jButton1 = new JButton("open"); 
32       JButton saveB = new JButton("save"); 
33       JButton jButton3 = new JButton("new"); 
34       JButton jButton4 = new JButton("help"); 
35       JSplitPane viewPanel; 
36       VirtualUniverse vUniverse = new VirtualUniverse(); 
37       SceneTreeModel model; 
38       SceneTreePanel panel; 
39       JPanel panelView = new JPanel(); 
40       VU applet = new Simple3D(); 
41    
42    
43       SimpleUniverse u; 
44    
45       public Viewer() { 
46           try { 
47               draw(); 
48           } catch (Exception e) { 
49               e.printStackTrace(); 
50           } 
51    
52   // 
53           getContentPane().setLayout(new BorderLayout()); 
54    
55           Canvas3D c = Utils.getCanvas3D(getContentPane()); 
56           getContentPane().add("Center", c); 
57           BranchGroup objRoot = new BranchGroup(); 
58           u = new SimpleUniverse(c); 
59           objRoot.addChild(new ColorCube(0.1)); 
60           objRoot.compile(); 
61           u.addBranchGraph(objRoot); 
62   // 
63       } 
64    
65       private void draw() throws Exception { 
66           this.addWindowListener(new java.awt.event.WindowAdapter() { 
67               public void windowClosing(WindowEvent e) { 
68                   this_windowClosing(e); 
69               } 
70           }); 
71           jButton1.setToolTipText("load a VirtualUniverse object from file"); 
72           jButton1.addMouseListener(new java.awt.event.MouseAdapter() { 
73               public void mouseClicked(MouseEvent e) { 
74                   jButton1_mouseClicked(e); 
75               } 
76           }); 
77           saveB.setToolTipText("save this VirtualUniverse object to a file"); 
78           saveB.addMouseListener(new java.awt.event.MouseAdapter() { 
79               public void mouseClicked(MouseEvent e) { 
80                   saveB_mouseClicked(e); 
81               } 
82           }); 
83           jButton3.setToolTipText("create a blank VirtualObject"); 
84           jButton4.setToolTipText("about Scene Viewer"); 
85           jButton4.addMouseListener(new java.awt.event.MouseAdapter() { 
86               public void mouseClicked(MouseEvent e) { 
87                   jButton4_mouseClicked(e); 
88               } 
89           }); 
90           this.getContentPane().add(jToolBar1, BorderLayout.NORTH); 
91           jToolBar1.add(jButton1, null); 
92           jToolBar1.add(saveB, null); 
93           jToolBar1.add(jButton3, null); 
94           jToolBar1.add(jButton4, null); 
95    
96    
97           VirtualUniverse vu = applet.getVU(); 
98    
99           model = new j3d.viewer.SceneTreeModel(vu); 
100          panel = new SceneTreePanel(model); 
101   
102   
103   
104          JPanel panelStatus = new JPanel(); 
105                  Canvas3D c = Utils.getCanvas3D(panelStatus); 
106   
107          panel.getTree().addTreeSelectionListener(new TreeListener(model)); 
108   
109          JScrollPane viewPanel2 = new JScrollPane(panelStatus); 
110   
111          JScrollPane treeScroller = new JScrollPane(panel); 
112          viewPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treeScroller, viewPanel2); 
113   
114   
115          this.getContentPane().add(viewPanel); 
116   
117          viewPanel.setContinuousLayout(true); 
118          setTitle("Scene Graph Viewer"); 
119          setResizable(false); 
120          setSize(500, 300); 
121          show(); 
122          System.out.println(vu.numLocales()); 
123   
124   
125          MainFrame m = new MainFrame(applet, 256, 256); 
126   
127      } 
128   
129      void this_windowClosing(WindowEvent e) { 
130          System.exit(0); 
131      } 
132   
133      void jButton1_mouseClicked(MouseEvent e) { 
134          FileDialog fd = new FileDialog(this); 
135          fd.setFile("*.obj"); 
136          fd.show(); 
137          if (fd.getFile() == null) return; 
138          if (!fd.getFile().endsWith(".obj")) return; 
139  //    if (!fd.getFile().endsWith(".class")) return; 
140   
141          File f = new File(fd.getDirectory(), fd.getFile()); 
142   
143   
144      } 
145   
146      void jButton4_mouseClicked(MouseEvent e) { 
147          InfoDialog info = new InfoDialog(this); 
148      } 
149   
150   
151      void saveB_mouseClicked(MouseEvent e) { 
152          FileDialog fd = new FileDialog(this, "Save .obj file", FileDialog.SAVE); 
153          fd.setFile("*.obj"); 
154          fd.show(); 
155          if (fd.getFile() == null) return; 
156          SerializeVU v = new SerializeVU(applet.getVU(), fd.getFile()); 
157   
158      } 
159   
160   
161      VirtualUniverse loadVU(String fileName) { 
162   
163          Object ob = new Object(); 
164          try { 
165              FileInputStream ostream = new FileInputStream(fileName); 
166              ObjectInputStream in = new ObjectInputStream(ostream); 
167              ob = in.readObject(); 
168              in.close(); 
169   
170          } catch (Exception ie) { 
171              System.out.println("deserializing:" + fileName + ie.toString()); 
172          } 
173          return null; 
174      } 
175   
176      public static void main(String[] args) { 
177          new Viewer(); 
178      } 
179   
180   
181      protected static class TreeListener implements TreeSelectionListener { 
182          SceneTreeModel model; 
183   
184          public TreeListener(SceneTreeModel mdl) { 
185              model = mdl; 
186          } 
187   
188          public void valueChanged(TreeSelectionEvent e) { 
189          } 
190      } 
191   
192   
193  } 
194   
195