Recipe for a Java 3D Program

The following steps are taken by the example program to create the scene graph elements and link them together. Java 3D will then render the scene graph and display the graphics in a window on the screen:

  1. Create a Canvas3D object and add it to the Applet/Frame panel.

  2. Create a BranchGroup as the root of the scene branch graph.

  3. Construct a Shape3D node with a TransformGroup node above it.

  4. Attach a RotationInterpolator behavior to the TransformGroup.

  5. Call the simple universe utility function to do the following:

    1. Establish a virtual universe with a single high-resolution Locale

    2. Create the PhysicalBody, PhysicalEnvironment, View, and ViewPlat-form objects.

    3. Create a BranchGroup as the root of the view platform branch graph.

    4. Insert the view platform branch graph into the Locale.

  6. Insert the scene branch graph into the simple universe's Locale.

The Java 3D renderer then starts running in an infinite loop.


NB :

SimpleUniverse Utility is not part of Java API

Most Java 3D programs build an identical set of superstructure and view branch objects, so the Java 3D utility packages provide a universe package for constructing and manipulating the objects in a view branch. The classes in the universe package provide a quick means for building a single view (single window) application. Listing 2-3 shows a code fragment for using the SimpleUniverse class. Note that the SimpleUniverse constructor takes a Canvas3D as an argument, in this case referred to by the variable myCanvas.

Code for Constructing a Scene Graph Using the Universe Package

------------------------------------------------------------------------
   import com.sun.j3d.utils.universe.*;
   Shape3D myShape1 = new Shape3D(myGeometry1, myAppearance1);
   Shape3D myShape2 = new Shape3D(myGeometry2, myAppearance2);
   BranchGroup myBranch = new BranchGroup();
   myBranch.addChild(myShape1);
   myBranch.addChild(myShape2);
   myBranch.compile();
   SimpleUniverse myUniv = new SimpleUniverse(myCanvas);
   myUniv.addBranchGraph(myBranch);