Java Reference
In-Depth Information
a sImple Java 3d proGram for a rotatingcube
To illustrate the main principles and components needed for creating a Java3D application,
Figure 1 shows a simple program for creating a rotating color cube.
Appendix B gives details of the various steps needed to compile and run the program.
The steps involve first creating an empty workspace for writing the Java source code in the
form of a project file, followed by compiling the code and running the program.
After performing the relevant steps to generate the executable file, a rotating cube as
shown in Figure 2-2 will be displayed on the browser window. As can be seen, the cube
has four colors and will be rotating continuously at the rate of 1 complete turn every four
Figure 1a. First part of RotatingCube
1. import javax.media.j3d.*;
2. import javax.vecmath.*;
3. import java.awt.*;
4. import java.awt.event.*;
5. import com.sun.j3d.utils.geometry.*;
6. import com.sun.j3d.utils.universe.*;
7.
8. public class RotatingCube extends Frame implements ActionListener
9. {
protected Button myButton=new Button("Exit");
10.
private SimpleUniverse U1 = null;
11.
12. protected BranchGroup buildContentBranch(Node Geometry_Appearance)
13. {
14. BranchGroup BG_c=new BranchGroup();
15. Transform3D S1=new Transform3D();
16. S1.set(new AxisAngle4d(1.0,1.0,0.0,Math.PI/4.0));
17. TransformGroup TG_c=new TransformGroup(S1);
18. TG_c.setCapability
19. (TransformGroup.ALLOW_TRANSFORM_WRITE);
20. BG_c.addChild(TG_c);
21. TG_c.addChild(Geometry_Appearance);
22. Transform3D yAxis = new Transform3D();
23. Alpha rotationAlpha = new Alpha(-1,4000);
24. RotationInterpolator B1 = new RotationInterpolator
25. (rotationAlpha,TG_c,yAxis,0.0f,(float)Math.PI*2.0f);
26.
BoundingSphere bounds=new BoundingSphere
27.
(new Point3d(0.0,0.0,0.0),100.0);
28.
B1.setSchedulingBounds(bounds);
29.
TG_c.addChild(B1);
30.
BG_c.compile();
31.
return BG_c;
32.
}//end
33.
Search WWH ::




Custom Search