Demo 5:

HelloUniverse5.java

</COMMENT> <blockquote> <hr> If you were using a Java-capable browser, you would see Hello Universe! instead of this paragraph. <hr> </blockquote>

Notes:

Again this uses MyGeom.java, this time we give the shape an appearance.

The Code for MyGeom is the same as it was in Demo 4.

The sceneGraph still looks like:

    canvas3d
        |
        V
  simpleUniverse
        |
        V
 BranchGroup scene -------------------,
        |                             |
        V                             V
TransforGroup trans < ~ ~ ~ ~ ~ MouseRotate mr
        |
        V
     MyGeom

Code:

 

// Adapted from java3d HelloUniverse Demo

/*
 * simply creates a cube an adds it to a universe, also allows the user to rotate the
 *  cube via the mouse 
 */

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.event.*;
import java.awt.GraphicsConfiguration;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;

public class HelloUniverse3 extends Applet {

    private SimpleUniverse u = null;
    
    public BranchGroup createSceneGraph() {

        // Create the root of the branch graph
        BranchGroup objRoot = new BranchGroup();
 
        // Create a transform group, so we can manipulate the object
        TransformGroup trans = new TransformGroup();              
        
        // create a behavior to allow the user to move the object with the mouse
        MouseRotate    mr = new MouseRotate();
        
        // tell the behavior which transform Group it is operating on
        mr.setTransformGroup(trans);
        
        // create the bounds for rotate behavior (centered at the origin) 
        BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
        mr.setSchedulingBounds(bounds);
        
        // add the Rotate Behavior to the root.(not the transformGroup
        objRoot.addChild(mr);
        
        // since the transfom in the transformGroup will be changing when we rotate the object
        // we need to explicitly allow the transform to be read, and written.
        trans.setCapability(trans.ALLOW_TRANSFORM_READ);
        trans.setCapability(trans.ALLOW_TRANSFORM_WRITE);

//------------------------------------------------------------------------v 
        // Create a new Appearance
        Appearance appearance = new Appearance();
        // Specify that we are not using a texture
        appearance.setTexture(null);
        // Create the polygon attributes to display only the wireframe
        PolygonAttributes pa = new PolygonAttributes();
        pa.setPolygonMode(pa.POLYGON_LINE);
        pa.setCullFace(pa.CULL_NONE);
        // set the polygon attributes
        appearance.setPolygonAttributes(pa);
        
        // Create a MyGeom shape with an Appearance; add it to the TransformGroup
        trans.addChild(new MyGeom(0.4f,appearance));
//------------------------------------------------------------------------^ 

        // add the transform group (and therefore also the cube) to the scene
        objRoot.addChild(trans);
      
        // Have Java 3D perform optimizations on this scene graph.
        objRoot.compile();

        return objRoot;
    }

    public HelloUniverse3() {
    }

    public void init() {
        setLayout(new BorderLayout());
        GraphicsConfiguration config =
           SimpleUniverse.getPreferredConfiguration();

        Canvas3D c = new Canvas3D(config);
        add("Center", c);

        // Create a simple scene and attach it to the virtual universe
        BranchGroup scene = createSceneGraph();

        u = new SimpleUniverse(c);

        // This will move the ViewPlatform back a bit so the
        // objects in the scene can be viewed.
        u.getViewingPlatform().setNominalViewingTransform();

        // add the objects to the universe
        u.addBranchGraph(scene);
    }

    public void destroy() {
        u.removeAllLocales();
    }

    //
    // The following allows HelloUniverse to be run as an application
    // as well as an applet
    //
    public static void main(String[] args) {
        new MainFrame(new HelloUniverse3(), 256, 256);
    }
}