/Users/lyon/j4p/src/j3d/yoyo/YoyoApp.java

1    package j3d.yoyo; 
2     
3    /* 
4     *      @(#)YoyoApp.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   /* 
35    * Getting Started with the Java 3D API 
36    * written in Java 3D 
37    */ 
38    
39   import com.sun.j3d.utils.applet.MainFrame; 
40   import com.sun.j3d.utils.universe.SimpleUniverse; 
41    
42   import javax.media.j3d.*; 
43   import javax.vecmath.Point3d; 
44   import javax.vecmath.Point3f; 
45   import java.applet.Applet; 
46   import java.awt.*; 
47    
48    
49   public class YoyoApp extends Applet { 
50    
51       ///////////////////////////////////////////////// 
52       // 
53       // create scene graph branch group 
54       // 
55       public class Yoyo extends Shape3D { 
56    
57           //////////////////////////////////////////// 
58           // 
59           // create Shape3D with geometry and appearance 
60   // the geometry is created in method yoyoGeometry 
61   // the appearance is created in method yoyoAppearance 
62           // 
63           public Yoyo() { 
64    
65               this.setGeometry(yoyoGeometry()); 
66    
67           } // end of Yoyo constructor 
68    
69           //////////////////////////////////////////// 
70           // 
71           // create yoyo geometry 
72   // four triangle fans represent the yoyo 
73   // strip   indicies_______________ 
74   //   0     0N+0 to 1N+0 ( 0 to N ) 
75   //   1     1N+1 to 2N+1 
76   //   2     2N+2 to 3N+2 
77   //   3     3N+4 to 4N+3 
78           // 
79           private Geometry yoyoGeometry() { 
80    
81               TriangleFanArray tfa; 
82               int N = 17; 
83               int totalN = 4 * (N + 1); 
84               Point3f coords[] = new Point3f[totalN]; 
85               int stripCounts[] = {N + 1, N + 1, N + 1, N + 1}; 
86               float r = 0.6f; 
87               float w = 0.4f; 
88               int n; 
89               double a; 
90               float x, y; 
91    
92   // set the central points for four triangle fan strips 
93               coords[0 * (N + 1)] = new Point3f(0.0f, 0.0f, w); 
94               coords[1 * (N + 1)] = new Point3f(0.0f, 0.0f, 0.0f); 
95               coords[2 * (N + 1)] = new Point3f(0.0f, 0.0f, 0.0f); 
96               coords[3 * (N + 1)] = new Point3f(0.0f, 0.0f, -w); 
97    
98               for (a = 0, n = 0; n < N; a = 2.0 * Math.PI / (N - 1) * ++n) { 
99                   x = (float) (r * Math.cos(a)); 
100                  y = (float) (r * Math.sin(a)); 
101                  coords[0 * (N + 1) + n + 1] = new Point3f(x, y, w); 
102                  coords[1 * (N + 1) + N - n] = new Point3f(x, y, w); 
103                  coords[2 * (N + 1) + n + 1] = new Point3f(x, y, -w); 
104                  coords[3 * (N + 1) + N - n] = new Point3f(x, y, -w); 
105              } 
106   
107              tfa = new TriangleFanArray(totalN, 
108                      TriangleFanArray.COORDINATES, 
109                      stripCounts); 
110   
111              tfa.setCoordinates(0, coords); 
112   
113              return tfa; 
114   
115          } // end of method yoyoGeometry in class Yoyo 
116   
117   
118      } // end of class Yoyo 
119   
120      ///////////////////////////////////////////////// 
121      // 
122      // create scene graph branch group 
123      // 
124      public BranchGroup createSceneGraph() { 
125   
126          BranchGroup objRoot = new BranchGroup(); 
127   
128          // Create the transform group node and initialize it to the 
129          // identity.  Enable the TRANSFORM_WRITE capability so that 
130          // our behavior code can modify it at runtime.  Add it to the 
131          // root of the subgraph. 
132          TransformGroup objSpin = new TransformGroup(); 
133          objSpin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
134   
135          objRoot.addChild(objSpin); 
136   
137          objSpin.addChild(new Yoyo()); 
138   
139          // Create a new Behavior object that will perform the desired 
140          // operation on the specified transform object and add it into 
141          // the scene graph. 
142          Transform3D yAxis = new Transform3D(); 
143          Alpha rotationAlpha = new Alpha(-1, 4000); 
144   
145          RotationInterpolator rotator = 
146                  new RotationInterpolator(rotationAlpha, objSpin); 
147          BoundingSphere bounds = 
148                  new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); 
149          rotator.setSchedulingBounds(bounds); 
150          objSpin.addChild(rotator); 
151   
152          // Let Java 3D perform optimizations on this scene graph. 
153          objRoot.compile(); 
154   
155          return objRoot; 
156      } // end of CreateSceneGraph method of YoyoApp 
157   
158      // Create a simple scene and attach it to the virtual universe 
159   
160      public YoyoApp() { 
161          setLayout(new BorderLayout()); 
162          GraphicsConfiguration config = 
163                  SimpleUniverse.getPreferredConfiguration(); 
164   
165          Canvas3D canvas3D = new Canvas3D(config); 
166          add("Center", canvas3D); 
167   
168          BranchGroup scene = createSceneGraph(); 
169   
170          // SimpleUniverse is a Convenience Utility class 
171          SimpleUniverse simpleU = new SimpleUniverse(canvas3D); 
172   
173          // This will move the ViewPlatform back a bit so the 
174          // objects in the scene can be viewed. 
175          simpleU.getViewingPlatform().setNominalViewingTransform(); 
176   
177          simpleU.addBranchGraph(scene); 
178      } // end of YoyoApp constructor 
179   
180      //  The following allows this to be run as an application 
181      //  as well as an applet 
182   
183      public static void main(String[] args) { 
184          Frame frame = new MainFrame(new YoyoApp(), 256, 256); 
185      } // end of main method of YoyoApp 
186   
187  } // end of class YoyoApp 
188