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

1    package j3d; 
2     
3    import javax.media.j3d.*; 
4    import javax.vecmath.Color3f; 
5    import javax.vecmath.Point3d; 
6     
7    /** 
8     * DocJava, Inc. 
9     * http://www.docjava.com 
10    * Programmer: dlyon 
11    * Date: Mar 3, 2004 
12    * Time: 5:34:28 PM 
13    */ 
14   ///////////////////////////////////////////////// 
15   // 
16   // create Twist visual object 
17   // 
18   public class Twist extends Shape3D { 
19    
20       //////////////////////////////////////////// 
21       // 
22   // create twist subgraph 
23       // 
24       public Twist() { 
25    
26           this.setGeometry(createGeometry()); 
27           this.setAppearance(createAppearance()); 
28       } // end of twist constructor 
29    
30       Geometry createGeometry() { 
31    
32           TriangleStripArray twistStrip; 
33           Color3f blue = new Color3f(0.0f, 0.0f, 1.0f); 
34    
35           // create triangle strip for twist 
36           int N = 80; 
37           int stripCounts[] = {N}; 
38           twistStrip = new TriangleStripArray(N, 
39                   TriangleStripArray.COORDINATES | TriangleStripArray.COLOR_3, 
40                   stripCounts); 
41    
42           double a; 
43           int v; 
44           for (v = 0, a = 0.0; 
45                v < N; v += 2, 
46                        a = v * 2.0 * Math.PI / (N - 2)) { 
47               twistStrip.setCoordinate(v, new Point3d(0.7 * Math.sin(a) + 0.2 * Math.cos(a), 
48                       0.3 * Math.sin(a), 
49                       0.7 * Math.cos(a) + 0.2 * Math.cos(a))); 
50               twistStrip.setCoordinate(v + 1, new Point3d(0.7 * Math.sin(a) - 0.2 * Math.cos(a), 
51                       -0.3 * Math.sin(a), 
52                       0.7 * Math.cos(a) - 0.2 * Math.cos(a))); 
53               twistStrip.setColor(v, blue); 
54               twistStrip.setColor(v + 1, blue); 
55           } 
56    
57           return twistStrip; 
58    
59       } 
60    
61       // create Appearance for Twist Strip 
62       // 
63       // this method creates the default Appearance for the 
64       // twist strip.  The commented line of code containting 
65       // the setCullFace will fix the problem of half of the 
66       // Twisted Strip disappearing. 
67    
68       Appearance createAppearance() { 
69    
70           Appearance twistAppear = new Appearance(); 
71           PolygonAttributes polyAttrib = new PolygonAttributes(); 
72           // polyAttrib.setCullFace(PolygonAttributes.CULL_NONE); 
73           twistAppear.setPolygonAttributes(polyAttrib); 
74    
75           return twistAppear; 
76       } 
77    
78   } // end of class Twist 
79