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

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