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

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