/Users/lyon/j4p/src/j3d/AxisApp.java

1    package j3d; 
2     
3    /* 
4     *      @(#)AxisApp.java 1.1 00/09/22 15:57 
5     * 
6     * Copyright (c) 1996-2000 Sun Microsystems, Inc. All Rights Reserved. 
7     * 
8     * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use, 
9     * modify and redistribute this software in source and binary code form, 
10    * provided that i) this copyright notice and license appear on all copies of 
11    * the software; and ii) Licensee does not utilize the software in a manner 
12    * which is disparaging to Sun. 
13    * 
14    * This software is provided "AS IS," without a warranty of any kind. ALL 
15    * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY 
16    * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR 
17    * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE 
18    * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING 
19    * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS 
20    * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, 
21    * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER 
22    * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF 
23    * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE 
24    * POSSIBILITY OF SUCH DAMAGES. 
25    * 
26    * This software is not designed or intended for use in on-line control of 
27    * aircraft, air traffic, aircraft navigation or aircraft communications; or in 
28    * the design, construction, operation or maintenance of any nuclear 
29    * facility. Licensee represents and warrants that it will not use or 
30    * redistribute the Software for such purposes. 
31    */ 
32    
33   /* 
34    * Getting Started with the Java 3D API 
35    * written in Java 3D 
36    * 
37    * This program demonstrates: 
38    *   1. writing a visual object class 
39    *      In this program, Axis class defines a visual object 
40    *      This particular class does not extend another class. 
41    *      See other the text for a discussion and a differnt approach. 
42    *   2. Using LineArray to draw 3D lines. 
43    *      Three LineArray geometries are contained in an instance of Axis. 
44    *      There are better ways of doing this.  This is a simple example. 
45    */ 
46    
47   import com.sun.j3d.utils.applet.MainFrame; 
48   import com.sun.j3d.utils.universe.SimpleUniverse; 
49    
50   import javax.media.j3d.BranchGroup; 
51   import javax.media.j3d.Canvas3D; 
52   import javax.media.j3d.LineArray; 
53   import javax.media.j3d.Shape3D; 
54   import javax.vecmath.Color3f; 
55   import javax.vecmath.Point3f; 
56   import java.applet.Applet; 
57   import java.awt.*; 
58    
59    
60   public class AxisApp extends Applet { 
61    
62       ///////////////////////////////////////////////// 
63       // 
64       // create graph branch group 
65       // 
66       public class Axis { 
67    
68           private BranchGroup axisBG; 
69    
70           //////////////////////////////////////////// 
71           // 
72           // create axis subgraph 
73           // 
74           public Axis() { 
75    
76               axisBG = new BranchGroup(); 
77    
78               // create line for X axis 
79               LineArray axisXLines = new LineArray(2, LineArray.COORDINATES); 
80               axisBG.addChild(new Shape3D(axisXLines)); 
81    
82               axisXLines.setCoordinate(0, new Point3f(-1.0f, 0.0f, 0.0f)); 
83               axisXLines.setCoordinate(1, new Point3f(1.0f, 0.0f, 0.0f)); 
84    
85               Color3f red = new Color3f(1.0f, 0.0f, 0.0f); 
86               Color3f green = new Color3f(0.0f, 1.0f, 0.0f); 
87               Color3f blue = new Color3f(0.0f, 0.0f, 1.0f); 
88    
89               // create line for Y axis 
90               LineArray axisYLines = new LineArray(2, 
91                       LineArray.COORDINATES | LineArray.COLOR_3); 
92               axisBG.addChild(new Shape3D(axisYLines)); 
93    
94               axisYLines.setCoordinate(0, new Point3f(0.0f, -1.0f, 0.0f)); 
95               axisYLines.setCoordinate(1, new Point3f(0.0f, 1.0f, 0.0f)); 
96    
97               axisYLines.setColor(0, green); 
98               axisYLines.setColor(1, blue); 
99    
100              // create line for Z axis 
101              Point3f z1 = new Point3f(0.0f, 0.0f, -1.0f); 
102              Point3f z2 = new Point3f(0.0f, 0.0f, 1.0f); 
103   
104              LineArray axisZLines = new LineArray(10, 
105                      LineArray.COORDINATES | LineArray.COLOR_3 
106              ); 
107              axisBG.addChild(new Shape3D(axisZLines)); 
108   
109              axisZLines.setCoordinate(0, z1); 
110              axisZLines.setCoordinate(1, z2); 
111              axisZLines.setCoordinate(2, z2); 
112              axisZLines.setCoordinate(3, new Point3f(0.1f, 0.1f, 0.9f)); 
113              axisZLines.setCoordinate(4, z2); 
114              axisZLines.setCoordinate(5, new Point3f(-0.1f, 0.1f, 0.9f)); 
115              axisZLines.setCoordinate(6, z2); 
116              axisZLines.setCoordinate(7, new Point3f(0.1f, -0.1f, 0.9f)); 
117              axisZLines.setCoordinate(8, z2); 
118              axisZLines.setCoordinate(9, new Point3f(-0.1f, -0.1f, 0.9f)); 
119   
120              Color3f colors[] = new Color3f[9]; 
121   
122              colors[0] = new Color3f(0.0f, 1.0f, 1.0f); 
123              for (int v = 0; v < 9; v++) { 
124                  colors[v] = red; 
125              } 
126   
127              axisZLines.setColors(1, colors); 
128   
129          } // end of axis constructor 
130   
131          public BranchGroup getBG() { 
132              return axisBG; 
133          } 
134   
135      } // end of class Axis 
136   
137      ///////////////////////////////////////////////// 
138      // 
139      // create scene graph branch group 
140      // 
141      public BranchGroup createSceneGraph() { 
142   
143          BranchGroup objRoot = new Axis().getBG(); 
144   
145          // Let Java 3D perform optimizations on this scene graph. 
146          objRoot.compile(); 
147   
148          return objRoot; 
149      } // end of CreateSceneGraph method of yoyo1 
150   
151      // Create a simple scene and attach it to the virtual universe 
152   
153      public AxisApp() { 
154          setLayout(new BorderLayout()); 
155          GraphicsConfiguration config = 
156                  SimpleUniverse.getPreferredConfiguration(); 
157   
158          Canvas3D canvas3D = new Canvas3D(config); 
159          add("Center", canvas3D); 
160   
161          BranchGroup scene = createSceneGraph(); 
162   
163          // SimpleUniverse is a Convenience Utility class 
164          SimpleUniverse simpleU = new SimpleUniverse(canvas3D); 
165   
166          // This will move the ViewPlatform back a bit so the 
167          // objects in the scene can be viewed. 
168          simpleU.getViewingPlatform().setNominalViewingTransform(); 
169   
170          simpleU.addBranchGraph(scene); 
171      } // end of coneyoyo constructor 
172   
173      //  The following allows this to be run as an application 
174      //  as well as an applet 
175   
176      public static void main(String[] args) { 
177          Frame frame = new MainFrame(new AxisApp(), 256, 256); 
178      } // end of main method of axisapp 
179   
180  } // end of class axisap 
181