//Comment out the following package statement to compile separately. package com.javaworld.media.j3d; import java.awt.*; import java.awt.event.*; import javax.media.j3d.*; import javax.vecmath.*; import com.sun.j3d.utils.geometry.*; import com.sun.j3d.utils.universe.*; /** * Example05 is identical to Example04, except our ViewingPlatform * is positioned slightly differently, so that we are outside of * the bounds. This results in no interpolation (behavior) being * scheduled, such that the text does not rotate. *
* This version is compliant with Java 1.2 and
* Java 3D 1.1 Beta 2, Nov 1998. Please refer to:
* http://www.javaworld.com/javaworld/jw-01-1999/jw-01-media.html
*
*
* @author Bill Day
* We place a Canvas3D object into the Frame so that Java 3D
* has the heavyweight component it needs to render 3D
* graphics into. We then call methods to construct the
* View and Content branches of our scene graph.
**/
public Example05() {
//Title our frame and set its size.
super("Java 3D Example05");
setSize(400,300);
//Here is our first Java 3d-specific code. We add a
//Canvas3D to our Frame so that we can render our 3D
//graphics. Java 3D requires a heavyweight component
//Canvas3D into which to render.
Canvas3D myCanvas3D = new Canvas3D(null);
add(myCanvas3D,BorderLayout.CENTER);
//Turn on the visibility of our frame.
setVisible(true);
//We want to be sure we properly dispose of resources
//this frame is using when the window is closed. We use
//an anonymous inner class adapter for this.
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{dispose(); System.exit(0);}
}
);
//Use SimpleUniverse as before.
SimpleUniverse myUniverse = new SimpleUniverse(myCanvas3D);
BranchGroup contentBranchGroup = constructContentBranch();
myUniverse.addBranchGraph(contentBranchGroup);
//Now, move the viewing position back a little bit, so that
//we can see everything in our scene. We use the universe's
//ViewingPlatform to get the view transform group and set
//it's transform.
//Note: ViewingPlatform is a different class from
//javax.media.j3d.ViewPlatform
ViewingPlatform myView = myUniverse.getViewingPlatform();
TransformGroup myViewTransformGroup = myView.getViewPlatformTransform();
Transform3D myViewTransform = new Transform3D();
myViewTransform.setTranslation(new Vector3f(2.0f,0.0f,12.0f));
myViewTransformGroup.setTransform(myViewTransform);
}
/**
* constructContentBranch() is where we specify the 3D graphics
* content to be rendered. We return the content branch group
* for use with our SimpleUniverse.
**/
private BranchGroup constructContentBranch() {
Font myFont = new Font("TimesRoman",Font.PLAIN,10);
Font3D myFont3D = new Font3D(myFont,new FontExtrusion());
Text3D myText3D = new Text3D(myFont3D, "JavaWorld");
Shape3D myShape3D = new Shape3D(myText3D, new Appearance());
Shape3D myCube = new ColorCube();
BranchGroup contentBranchGroup = new BranchGroup();
Transform3D myTransform3D = new Transform3D();
TransformGroup contentTransformGroup = new TransformGroup(myTransform3D);
contentTransformGroup.addChild(myShape3D);
Alpha myAlpha = new Alpha();
myAlpha.setIncreasingAlphaDuration(10000);
myAlpha.setLoopCount(-1);
RotationInterpolator myRotater =
new RotationInterpolator(myAlpha,contentTransformGroup);
myRotater.setAxisOfRotation(myTransform3D);
myRotater.setMinimumAngle(0.0f);
myRotater.setMaximumAngle((float)(Math.PI*2.0));
BoundingSphere myBounds = new BoundingSphere();
myRotater.setSchedulingBounds(myBounds);
contentTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
contentTransformGroup.addChild(myRotater);
contentBranchGroup.addChild(contentTransformGroup);
myTransform3D.setTranslation(new Vector3f(-0.5f,-0.5f,-2.3f));
TransformGroup cubeTransformGroup = new TransformGroup(myTransform3D);
cubeTransformGroup.addChild(myCube);
contentBranchGroup.addChild(cubeTransformGroup);
return(contentBranchGroup);
}
}