Making a Scene with Java3D

by Michael Pilone



Listing One



// The canvas needs some information about the graphics environment. This

// information could be custom built if desired, but a utility method

// exists to make this easier.

GraphicsConfiguration gc = SimpleUniverse.getPreferredConfiguration();

        

// Create the canvas which will serve as the rendering surface. The

// canvas is a component like any AWT component, therefore it can

// be added to a JFrame to be displayed.

Canvas3D canvas = new Canvas3D(gc);

        

// A SimpleUniverse is a utility class that wraps some of the VirtualUniverse 

// configuration options and sets up a basic universe that is useful for 

// simple demonstations. The universe serves as the root of the scenegraph.

SimpleUniverse universe = new SimpleUniverse(canvas);

        

// Get the viewing platform from the universe and set a nominal

// transform. This will move the viewer slightly back from the

// center so you can see the nodes in the scene.

universe.getViewingPlatform().setNominalViewingTransform();





Listing Two



// Root group of scene graph. Everything is created as a child of this group.

BranchGroup rootBg = new BranchGroup();

        

// Create a simple transform to scale scene down so it fits in the view.

Transform3D scaleTrans = new Transform3D();

scaleTrans.setScale(0.6);

TransformGroup objScale = new TransformGroup(scaleTrans);

rootBg.addChild(objScale);

        

// Create a simple transform to rotate around the x

// axis to show that the cube really is 3 dimensional.

Transform3D rollTrans = new Transform3D();

rollTrans.rotX(Math.toRadians(35));

TransformGroup objRoll = new TransformGroup(rollTrans);

objScale.addChild(objRoll);





Listing Three



// Create a transform to rotate the shape using an interpolator. Once the 

// transform group is added to the scene, Java3D won't allow modifications 

// unless you tell it that you want that capability, therefore you set the

// ALLOW_TRANSFORM_WRITE.

TransformGroup objRotate = new TransformGroup();

objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

objRoll.addChild(objRotate);



// Create an interpolator behavior object that will rotate the cube

// by modifying the rotation transform at runtime.

Transform3D yAxis = new Transform3D();

Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE,

                                0, 0, 8000, 0, 0, 0, 0, 0);

// Setup the scheduling bounds of the behaviour so it runs indefinately.

Bounds bounds = new BoundingSphere(new Point3d(0, 0, 0), 100.0);

        

// Create the interpolator that will rotate the given transform

// around the y axis as the alpha value changes.

RotationInterpolator rotator =

    new RotationInterpolator(rotationAlpha, objRotate, yAxis,

                             0.0f, (float) Math.PI*2.0f);

rotator.setSchedulingBounds(bounds);

objRotate.addChild(rotator);















2



