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

1    package j3d; 
2     
3     
4    import com.sun.j3d.utils.applet.MainFrame; 
5    import com.sun.j3d.utils.universe.SimpleUniverse; 
6     
7    import javax.media.j3d.*; 
8    import java.applet.Applet; 
9    import java.awt.*; 
10    
11   public class TwistStripApp extends Applet { 
12    
13       ///////////////////////////////////////////////// 
14       // 
15       // create scene graph branch group 
16       // 
17       public BranchGroup createSceneGraph() { 
18    
19           BranchGroup contentRoot = new BranchGroup(); 
20    
21           // Create the transform group node and initialize it to the 
22           // identity. Add it to the root of the subgraph. 
23           TransformGroup objSpin = new TransformGroup(); 
24           objSpin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
25           contentRoot.addChild(objSpin); 
26    
27           Shape3D twist = new Twist(); 
28           objSpin.addChild(twist); 
29    
30           // Duplicate the twist strip geometry and set the 
31           // appearance of the new Shape3D object to line mode 
32           // without culling. 
33           // Add the POLYGON_FILLED and POLYGON_LINE strips 
34           // in the scene graph at the same point. 
35           // This will show the triangles of the original Mobius strip that 
36           // are clipped.  The PolygonOffset is set to prevent stitching. 
37           PolygonAttributes polyAttrib = new PolygonAttributes(); 
38           polyAttrib.setCullFace(PolygonAttributes.CULL_NONE); 
39           polyAttrib.setPolygonMode(PolygonAttributes.POLYGON_LINE); 
40           polyAttrib.setPolygonOffset(0.001f); 
41           Appearance polyAppear = new Appearance(); 
42           polyAppear.setPolygonAttributes(polyAttrib); 
43           objSpin.addChild(new Shape3D(twist.getGeometry(), polyAppear)); 
44    
45           Alpha rotationAlpha = new Alpha(-1, 1600); 
46    
47           RotationInterpolator rotator = 
48                   new RotationInterpolator(rotationAlpha, objSpin); 
49    
50           // a bounding sphere specifies a region a behavior is active 
51           // create a sphere centered at the origin with radius of 1 
52           BoundingSphere bounds = new BoundingSphere(); 
53           rotator.setSchedulingBounds(bounds); 
54           objSpin.addChild(rotator); 
55           addBackground(bounds, contentRoot); 
56    
57           // Let Java 3D perform optimizations on this scene graph. 
58           contentRoot.compile(); 
59    
60           return contentRoot; 
61       } // end of CreateSceneGraph method of TwistStripApp 
62    
63       private void addBackground(BoundingSphere bounds, BranchGroup contentRoot) { 
64           Background background = 
65                   j3d.Utils.getMandlebrotBackground(256, 256); 
66           background.setApplicationBounds(bounds); 
67           contentRoot.addChild(background); 
68       } 
69    
70       // Create a simple scene and attach it to the virtual universe 
71    
72       public TwistStripApp() { 
73           setLayout(new BorderLayout()); 
74           GraphicsConfiguration config = 
75                   SimpleUniverse.getPreferredConfiguration(); 
76    
77           Canvas3D canvas3D = new Canvas3D(config); 
78           add("Center", canvas3D); 
79    
80           BranchGroup scene = createSceneGraph(); 
81    
82           // SimpleUniverse is a Convenience Utility class 
83           SimpleUniverse su = new SimpleUniverse(canvas3D); 
84    
85           // This will move the ViewPlatform back a bit so the 
86           // objects in the scene can be viewed. 
87           su.getViewingPlatform().setNominalViewingTransform(); 
88    
89           su.addBranchGraph(scene); 
90    
91       } // end of TwistStripApp constructor 
92    
93       //  The following method allows this to be run as an application 
94    
95       public static void main(String[] args) { 
96    
97           new MainFrame(new TwistStripApp(), 256, 256); 
98       } // end of main method of TwistStripApp 
99    
100  } // end of class TwistStripApp 
101