/Users/lyon/j4p/src/j3d/SwingingCubeApp.java

1    package j3d; 
2     
3    /* 
4     *      @(#)RotPosPathApp.java 1.1 00/09/22 14:37 
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   import com.sun.j3d.utils.applet.MainFrame; 
34   import com.sun.j3d.utils.geometry.ColorCube; 
35   import com.sun.j3d.utils.universe.SimpleUniverse; 
36    
37   import javax.media.j3d.*; 
38   import javax.vecmath.AxisAngle4f; 
39   import javax.vecmath.Point3f; 
40   import javax.vecmath.Quat4f; 
41   import java.applet.Applet; 
42   import java.awt.*; 
43    
44    
45   public class SwingingCubeApp extends Applet { 
46    
47       public BranchGroup createSceneGraph() { 
48           // Create the root of the branch graph 
49           BranchGroup objRoot = new BranchGroup(); 
50    
51           Alpha alpha = new Alpha(-1, 10000); 
52           TransformGroup target = new TransformGroup(); 
53           Transform3D axisOfRotPos = new Transform3D(); 
54           float[] knots = {0.0f, 0.1f, 0.2f, 0.3f, 0.4f, 0.6f, 0.8f, 0.9f, 1.0f}; 
55           Quat4f[] quats = new Quat4f[9]; 
56           Point3f[] positions = new Point3f[9]; 
57    
58           target.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
59    
60           AxisAngle4f axis = new AxisAngle4f(1.0f, 0.0f, 0.0f, 0.0f); 
61           axisOfRotPos.set(axis); 
62    
63           quats[0] = new Quat4f(0.0f, 1.0f, 1.0f, 0.0f); 
64           quats[1] = new Quat4f(1.0f, 0.0f, 0.0f, 0.0f); 
65           quats[2] = new Quat4f(0.0f, 1.0f, 0.0f, 0.0f); 
66           quats[3] = new Quat4f(0.0f, 1.0f, 1.0f, 0.0f); 
67           quats[4] = new Quat4f(0.0f, 0.0f, 1.0f, 0.0f); 
68           quats[5] = new Quat4f(0.0f, 1.0f, 1.0f, 0.0f); 
69           quats[6] = new Quat4f(1.0f, 1.0f, 0.0f, 0.0f); 
70           quats[7] = new Quat4f(1.0f, 0.0f, 0.0f, 0.0f); 
71           quats[8] = quats[0]; 
72    
73           positions[0] = new Point3f(0.0f, 0.0f, -1.0f); 
74           positions[1] = new Point3f(1.0f, -1.0f, -2.0f); 
75           positions[2] = new Point3f(-1.0f, 1.0f, -3.0f); 
76           positions[3] = new Point3f(2.0f, 0.0f, -4.0f); 
77           positions[4] = new Point3f(-2.0f, -1.0f, -5.0f); 
78           positions[5] = new Point3f(3.0f, 1.0f, -6.0f); 
79           positions[6] = new Point3f(-3.0f, 0.0f, -7.0f); 
80           positions[7] = new Point3f(2.0f, -1.0f, -4.0f); 
81           positions[8] = positions[0]; 
82    
83    
84           RotPosPathInterpolator rotPosPath = new RotPosPathInterpolator( 
85                   alpha, target, axisOfRotPos, knots, quats, positions); 
86           rotPosPath.setSchedulingBounds(new BoundingSphere()); 
87    
88    
89           objRoot.addChild(target); 
90           objRoot.addChild(rotPosPath); 
91           target.addChild(new ColorCube(0.4)); 
92    
93           Background background = new Background(); 
94           background.setColor(1.0f, 1.0f, 1.0f); 
95           background.setApplicationBounds(new BoundingSphere()); 
96           objRoot.addChild(background); 
97    
98           PointArray point_geom = new PointArray(9, GeometryArray.COORDINATES); 
99           point_geom.setCoordinates(0, positions); 
100          Appearance points_appear = new Appearance(); 
101          ColoringAttributes points_coloring = new ColoringAttributes(); 
102          points_coloring.setColor(1.0f, 0.0f, 0.0f); 
103          points_appear.setColoringAttributes(points_coloring); 
104          PointAttributes points_points = new PointAttributes(4.0f, true); 
105          points_appear.setPointAttributes(points_points); 
106          Shape3D points = new Shape3D(point_geom, points_appear); 
107          objRoot.addChild(points); 
108   
109          objRoot.compile(); 
110   
111          return objRoot; 
112      } // end of CreateSceneGraph method of RotPosPathApp 
113   
114      // Create a simple scene and attach it to the virtual universe 
115   
116      public SwingingCubeApp() { 
117          setLayout(new BorderLayout()); 
118          GraphicsConfiguration config = 
119                  SimpleUniverse.getPreferredConfiguration(); 
120   
121          Canvas3D canvas3D = new Canvas3D(config); 
122          add("Center", canvas3D); 
123   
124          BranchGroup scene = createSceneGraph(); 
125   
126          // SimpleUniverse is a Convenience Utility class 
127          SimpleUniverse simpleU = new SimpleUniverse(canvas3D); 
128   
129          // This will move the ViewPlatform back a bit so the 
130          // objects in the scene can be viewed. 
131          simpleU.getViewingPlatform().setNominalViewingTransform(); 
132   
133          simpleU.addBranchGraph(scene); 
134      } // end of RotPosPathApp (constructor) 
135   
136      //  The following allows this to be run as an application 
137      //  as well as an applet 
138   
139      public static void main(String[] args) { 
140          System.out.print("RotPosPathApp.java \n- a demonstration of using the RotPosPathInterpolator "); 
141          System.out.println("and Alpha classes to provide animation in a Java 3D scene."); 
142          System.out.println("The RotPosPathInterpolator changes the target TransformGroup"); 
143          System.out.println("to change the POSition and ROTation along a PATH.  The positions"); 
144          System.out.println("are marked by the red dots in the scene."); 
145          System.out.println("This is a simple example progam from The Java 3D API Tutorial."); 
146          System.out.println("The Java 3D Tutorial is available on the web at:"); 
147          System.out.println("http://java.sun.com/products/java-media/3D/collateral"); 
148          Frame frame = new MainFrame(new SwingingCubeApp(), 256, 256); 
149      } // end of main (method of RotPosPathApp) 
150   
151  } // end of class RotPosPathApp 
152