/Users/lyon/j4p/src/j3d/cr325/Solid5.java

1    package j3d.cr325; 
2     
3    import com.sun.j3d.utils.applet.MainFrame; 
4    import com.sun.j3d.utils.geometry.Cylinder; 
5    import com.sun.j3d.utils.geometry.Primitive; 
6    import com.sun.j3d.utils.universe.SimpleUniverse; 
7    import j3d.Utils; 
8    import utils.SystemUtils; 
9     
10   import javax.media.j3d.*; 
11   import javax.vecmath.Color3f; 
12   import javax.vecmath.Point3d; 
13   import javax.vecmath.Vector3d; 
14   import javax.vecmath.Vector3f; 
15   import java.applet.Applet; 
16   import java.awt.*; 
17    
18    
19   public class Solid5 extends Applet { 
20       Color3f black = new Color3f(0.0f, 0.0f, 0.0f); 
21       Color3f white = new Color3f(1.0f, 1.0f, 1.0f); 
22       Color3f blue = new Color3f(0.0f, 0.0f, 0.8f); 
23    
24       private BranchGroup createSceneGraph() { 
25           // Create the root of the branch graph 
26           BranchGroup objRoot = new BranchGroup(); 
27           addBackgroundAndLights( 
28                   new BoundingSphere( 
29                           new Point3d(0.0, 0.0, 0.0), 100.0), 
30                   objRoot); 
31           addObjects(objRoot); 
32    
33           return objRoot; 
34       } 
35    
36       private void addBackgroundAndLights(BoundingSphere bounds, BranchGroup objRoot) { 
37           addBackground(bounds, objRoot); 
38           addLights(bounds, objRoot); 
39       } 
40    
41       private void addBackground(BoundingSphere bounds, BranchGroup objRoot) { 
42           Background bg = //Utils.getMandlebrotBackground(640, 480); 
43                   Utils.getImageBackground("gifs" 
44                   +SystemUtils.getPathSeparator()+"animation2.gif"); 
45           bg.setApplicationBounds(bounds); 
46           objRoot.addChild(bg); 
47       } 
48    
49       private void addObjects(BranchGroup objRoot) { 
50           Appearance ap1 = new Appearance(); 
51           Appearance ap2 = new Appearance(); 
52           Appearance ap3 = new Appearance(); 
53    
54           ap1.setMaterial(new Material(blue, black, blue, 
55                   white, 80.0f)); 
56           objRoot.addChild(spinningObject(ap1, 0.2, -.6, -.7, 0.0)); 
57           blue = new Color3f(0.8f, 0.0f, 0.0f); 
58           ap2.setMaterial(new Material(blue, black, blue, 
59                   white, 80.0f)); 
60           objRoot.addChild(spinningObject(ap2, 0.2, 0.0, -.5, 0.38)); 
61           blue = new Color3f(0.8f, 0.8f, 0.0f); 
62           Material m1 = new Material(blue, black, blue, 
63                   white, 80.0f); 
64           m1.setShininess(128f); 
65           ap3.setMaterial(m1); 
66           objRoot.addChild(spinningObject(ap3, 0.2, .5, -.7, 0.0)); 
67    
68           // Let Java 3D perform optimizations on this scene graph. 
69           objRoot.compile(); 
70       } 
71    
72       private void addLights(BoundingSphere bounds, BranchGroup objRoot) { 
73           Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f); 
74           Vector3f lDir1 = new Vector3f(-1.0f, -0.3f, -1.0f); 
75           Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f); 
76    
77           AmbientLight aLgt = new AmbientLight(alColor); 
78           aLgt.setInfluencingBounds(bounds); 
79           DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1); 
80           lgt1.setInfluencingBounds(bounds); 
81           objRoot.addChild(aLgt); 
82           objRoot.addChild(lgt1); 
83       } 
84    
85       private Group spinningObject(Appearance app, double scale, 
86                                    double xpos, double ypos, double zpos) { 
87           TransformGroup objTrans = getTransformgroup(scale, xpos, ypos, zpos); 
88           TransformGroup spinTg = addTGAndEnableTransformWriteSoItCanBeModified(); 
89           addCylinder(app, spinTg); 
90    
91           Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 
92                   0, 0, 5000, 0, 0, 0, 0, 0); 
93    
94           RotationInterpolator rotator = 
95                   new RotationInterpolator(rotationAlpha, spinTg); 
96    
97           BoundingSphere bounds = 
98                   new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); 
99    
100          rotator.setSchedulingBounds(bounds); 
101          addBehaviorAndTransformGroup(objTrans, rotator, spinTg); 
102          return objTrans; 
103      } 
104   
105      private TransformGroup addTGAndEnableTransformWriteSoItCanBeModified() { 
106          TransformGroup spinTg = new TransformGroup(); 
107          spinTg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
108          return spinTg; 
109      } 
110   
111      private void addBehaviorAndTransformGroup(TransformGroup objTrans, 
112                                                RotationInterpolator rotator, 
113                                                TransformGroup spinTg) { 
114          objTrans.addChild(rotator); 
115          objTrans.addChild(spinTg); 
116      } 
117   
118      private void addCylinder(Appearance app, TransformGroup spinTg) { 
119          Primitive obj = new Cylinder(1.0f, 0.2f); 
120          obj.setAppearance(app); 
121          spinTg.addChild(obj); 
122      } 
123   
124      private TransformGroup getTransformgroup(double scale, double xpos, double ypos, double zpos) { 
125          Transform3D t = new Transform3D(); 
126          t.set(scale, new Vector3d(xpos, ypos, zpos)); 
127          TransformGroup objTrans = new TransformGroup(t); 
128          return objTrans; 
129      } 
130   
131      public Solid5() { 
132          setLayout(new BorderLayout()); 
133          Canvas3D c = Utils.getCanvas3D(this); 
134          add("Center", c); 
135   
136          // Create a simple scene and attach it to the virtual universe 
137          SimpleUniverse u = new SimpleUniverse(c); 
138   
139          // This will move the ViewPlatform back a bit so the 
140          // objects in the scene can be viewed. 
141          u.getViewingPlatform().setNominalViewingTransform(); 
142          u.addBranchGraph(createSceneGraph()); 
143      } 
144   
145   
146      // 
147      // The following allows Solid5 to be run as an application 
148      // as well as an applet 
149      // 
150      public static void main(String[] args) { 
151          new MainFrame(new Solid5(), 640, 480); 
152      } 
153  } 
154