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

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