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

1    package j3d.yoyo; 
2     
3    /* 
4     *      @(#)ConeYoyoApp.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, ConeYoyo class defines a visual object 
40    *      independent of all other classes. 
41    *   2. Using Cone to create surfaces. 
42    */ 
43    
44   import com.sun.j3d.utils.applet.MainFrame; 
45   import com.sun.j3d.utils.geometry.Cone; 
46   import com.sun.j3d.utils.universe.SimpleUniverse; 
47    
48   import javax.media.j3d.*; 
49   import javax.vecmath.Point3d; 
50   import javax.vecmath.Vector3f; 
51   import java.applet.Applet; 
52   import java.awt.*; 
53    
54    
55   public class ConeYoyoApp extends Applet { 
56    
57       ///////////////////////////////////////////////// 
58       // 
59       // create scene graph branch group 
60       // 
61       public class ConeYoyo { 
62    
63           private BranchGroup yoyoBG; 
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 ConeYoyo() { 
72    
73               yoyoBG = new BranchGroup(); 
74    
75               Appearance app = new Appearance(); 
76    
77               // rotate object has composited transformation matrix 
78               Transform3D rotate = new Transform3D(); 
79               Transform3D translate = new Transform3D(); 
80    
81               translate.set(new Vector3f(0.1f, 0.0f, 0.0f)); 
82               TransformGroup yoyoTGT1 = new TransformGroup(translate); 
83               yoyoBG.addChild(yoyoTGT1); 
84    
85               rotate.rotZ(Math.PI / 2.0d); 
86               TransformGroup yoyoTGR1 = new TransformGroup(rotate); 
87               Cone cone1 = new Cone(0.6f, 0.2f); 
88               cone1.setAppearance(app); 
89               yoyoTGR1.addChild(cone1); 
90               yoyoTGT1.addChild(yoyoTGR1); 
91    
92               translate.set(new Vector3f(-0.1f, 0.0f, 0.0f)); 
93               TransformGroup yoyoTGT2 = new TransformGroup(translate); 
94               yoyoBG.addChild(yoyoTGT2); 
95    
96               rotate.rotZ(-Math.PI / 2.0d); 
97               TransformGroup yoyoTGR2 = new TransformGroup(rotate); 
98               Cone cone2 = new Cone(0.6f, 0.2f); 
99               cone2.setAppearance(app); 
100              yoyoTGR2.addChild(cone2); 
101              yoyoTGT2.addChild(yoyoTGR2); 
102   
103              yoyoBG.compile(); 
104   
105          } // end of ConeYoyo constructor 
106   
107          public BranchGroup getBG() { 
108              return yoyoBG; 
109          } 
110   
111      } // end of class ConeYoyo 
112   
113      ///////////////////////////////////////////////// 
114      // 
115      // create scene graph branch group 
116      // 
117      public BranchGroup createSceneGraph() { 
118   
119          BranchGroup objRoot = new BranchGroup(); 
120   
121          // Create the transform group node and initialize it to the 
122          // identity.  Enable the TRANSFORM_WRITE capability so that 
123          // our behavior code can modify it at runtime.  Add it to the 
124          // root of the subgraph. 
125          TransformGroup objSpin = new TransformGroup(); 
126          objSpin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
127   
128          objRoot.addChild(objSpin); 
129          objSpin.addChild(new ConeYoyo().getBG()); 
130   
131          // Create a new Behavior object that will perform the desired 
132          // operation on the specified transform object and add it into 
133          // the scene graph. 
134          Transform3D yAxis = new Transform3D(); 
135          Alpha rotationAlpha = new Alpha(-1, 4000); 
136   
137          RotationInterpolator rotator = 
138                  new RotationInterpolator(rotationAlpha, objSpin); 
139          BoundingSphere bounds = 
140                  new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); 
141          rotator.setSchedulingBounds(bounds); 
142          objSpin.addChild(rotator); 
143   
144          // Let Java 3D perform optimizations on this scene graph. 
145          objRoot.compile(); 
146   
147          return objRoot; 
148      } // end of CreateSceneGraph method of yoyo1 
149   
150      // Create a simple scene and attach it to the virtual universe 
151   
152      public ConeYoyoApp() { 
153          setLayout(new BorderLayout()); 
154          GraphicsConfiguration config = 
155                  SimpleUniverse.getPreferredConfiguration(); 
156   
157          Canvas3D canvas3D = new Canvas3D(config); 
158          add("Center", canvas3D); 
159   
160          BranchGroup scene = createSceneGraph(); 
161   
162          SimpleUniverse simpleU = new SimpleUniverse(canvas3D); 
163   
164          simpleU.getViewingPlatform().setNominalViewingTransform(); 
165   
166          simpleU.addBranchGraph(scene); 
167      } // end of ConeYoyoApp constructor 
168   
169      //  The following allows this to be run as an application 
170      //  as well as an applet 
171   
172      public static void main(String[] args) { 
173          Frame frame = new MainFrame(new ConeYoyoApp(), 256, 256); 
174      } // end of main method of coneyoyo 
175   
176  } // end of class ConeYoyoApp 
177