/Users/lyon/j4p/src/j3d/cr325/Hello/HelloJava3Dd.java

1    package j3d.cr325.Hello; 
2     
3     
4    import com.sun.j3d.utils.applet.MainFrame; 
5    import com.sun.j3d.utils.geometry.ColorCube; 
6    import com.sun.j3d.utils.universe.SimpleUniverse; 
7     
8    import javax.media.j3d.*; 
9    import java.applet.Applet; 
10   import java.awt.*; 
11    
12   //   HelloJava3Dc renders a single, rotating cube. 
13    
14   public class HelloJava3Dd extends Applet { 
15    
16       public BranchGroup createSceneGraph() { 
17           // Create the root of the branch graph 
18           BranchGroup objRoot = new BranchGroup(); 
19    
20           // rotate object has composited transformation matrix 
21           Transform3D rotate = new Transform3D(); 
22           Transform3D tempRotate = new Transform3D(); 
23           rotate.rotX(Math.PI / 4.0d); 
24           tempRotate.rotY(Math.PI / 5.0d); 
25           rotate.mul(tempRotate); 
26    
27           TransformGroup objRotate = new TransformGroup(rotate); 
28    
29           // Create the transform group node and initialize it to the 
30           // identity.  Enable the TRANSFORM_WRITE capability so that 
31           // our behavior code can modify it at runtime.  Add it to the 
32           // root of the subgraph. 
33           TransformGroup objSpin = new TransformGroup(); 
34           objSpin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
35    
36           objRoot.addChild(objRotate); 
37           objRotate.addChild(objSpin); 
38    
39           // Create a simple shape leaf node, add it to the scene graph. 
40           // ColorCube is a Convenience Utility class 
41           objSpin.addChild(new ColorCube(0.4)); 
42    
43           // Create a new Behavior object that will perform the desired 
44           // operation on the specified transform object and add it into 
45           // the scene graph. 
46           Transform3D yAxis = new Transform3D(); 
47           Alpha rotationAlpha = new Alpha(-1, 4000); 
48    
49           RotationInterpolator rotator = 
50                   new RotationInterpolator(rotationAlpha, objSpin, yAxis, 
51                           0.0f, (float) Math.PI * 2.0f); 
52    
53           // a bounding sphere specifies a region a behavior is active 
54           // create a sphere centered at the origin with radius of 1 
55           BoundingSphere bounds = new BoundingSphere(); 
56           rotator.setSchedulingBounds(bounds); 
57           objSpin.addChild(rotator); 
58    
59           return objRoot; 
60       } // end of CreateSceneGraph method of HelloJava3Dd 
61    
62       public HelloJava3Dd() { 
63           setLayout(new BorderLayout()); 
64           GraphicsConfiguration config = 
65                   SimpleUniverse.getPreferredConfiguration(); 
66    
67           Canvas3D canvas3D = new Canvas3D(config); 
68           add("Center", canvas3D); 
69    
70           BranchGroup bg = createSceneGraph(); 
71           bg.compile(); 
72    
73           // SimpleUniverse is a Convenience Utility class 
74           SimpleUniverse su = new SimpleUniverse(canvas3D); 
75    
76           // This will move the ViewPlatform back a bit so the 
77           // objects in the scene can be viewed. 
78           su.getViewingPlatform().setNominalViewingTransform(); 
79    
80           su.addBranchGraph(bg); 
81       } // end of HelloJava3Dd (constructor) 
82    
83       //  The following allows this to be run as an application 
84       //  as well as an applet 
85    
86       public static void main(String[] args) { 
87           new MainFrame(new HelloJava3Dd(), 256, 256); 
88       } // end of main (method of HelloJava3D) 
89    
90   } // end of class HelloJava3Dd 
91