import java.applet.Applet; import java.awt.BorderLayout; import java.awt.Frame; import java.awt.event.*; import java.awt.*; import com.sun.j3d.utils.applet.MainFrame; import com.sun.j3d.utils.universe.*; import com.sun.j3d.utils.geometry.*; import javax.media.j3d.*; import javax.vecmath.*; import com.sun.j3d.utils.behaviors.keyboard.*; // FourObjects2.java public class FourObjects2 extends Applet { TransformGroup vpTrans; public FourObjects2() { setLayout(new BorderLayout()); Canvas3D canvas3D = new Canvas3D(null); add("Center", canvas3D); VirtualUniverse universe; Locale locale; universe = new VirtualUniverse(); locale = new Locale(universe); BranchGroup viewBG = createView(canvas3D); locale.addBranchGraph(viewBG); BranchGroup contentBG= createContent(); locale.addBranchGraph(contentBG); } public BranchGroup createView(Canvas3D canvas3D){ BranchGroup vpRoot = new BranchGroup(); View view = new View(); PhysicalBody body = new PhysicalBody(); PhysicalEnvironment environment = new PhysicalEnvironment(); ViewPlatform vp = new ViewPlatform(); view.setPhysicalBody(body); view.setPhysicalEnvironment(environment); view.attachViewPlatform(vp); view.addCanvas3D(canvas3D); Transform3D t = new Transform3D(); t.set(new Vector3f(0.0f,0.0f,6.0f)); vpTrans = new TransformGroup(t); vpRoot.addChild(vpTrans); vpTrans.addChild(vp); vpTrans.setCapability(vpTrans.ALLOW_TRANSFORM_READ); vpTrans.setCapability(vpTrans.ALLOW_TRANSFORM_WRITE); vpTrans.setCapability(Group.ALLOW_CHILDREN_READ); vpTrans.setCapability(Group.ALLOW_CHILDREN_WRITE); vpTrans.setCapability(Group.ALLOW_CHILDREN_EXTEND); vpRoot.addChild(new FPS()); // add frame per sec return vpRoot; } public BranchGroup createContent() { BranchGroup contentRoot = new BranchGroup(); TransformGroup cube = createCube(); contentRoot.addChild(cube); TransformGroup cone = createCone(); contentRoot.addChild(cone); TransformGroup cylinder = createCylinder(); contentRoot.addChild(cylinder); TransformGroup text = createText(); contentRoot.addChild(text); KeyNavigatorBehavior keyNavBeh = new KeyNavigatorBehavior(vpTrans); keyNavBeh.setSchedulingBounds(new BoundingSphere(new Point3d(), 1000.0)); contentRoot.addChild(keyNavBeh); return contentRoot; } public TransformGroup createCube() { Transform3D t = new Transform3D(); t.set(new Vector3f(-1.0f,1.0f,0.0f)); TransformGroup moveCube = new TransformGroup(t); TransformGroup objSpin = new TransformGroup(); objSpin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); moveCube.addChild(objSpin); Alpha rotationAlpha = new Alpha(-1, 4000); RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, objSpin); BoundingSphere bounds = new BoundingSphere(); rotator.setSchedulingBounds(bounds); objSpin.addChild(rotator); objSpin.addChild(new ColorCube(0.4)); return moveCube; } public TransformGroup createCone() { Transform3D t = new Transform3D(); t.set(new Vector3f(1.0f,1.0f,0.0f)); TransformGroup moveCone = new TransformGroup(t); TransformGroup objSpin = new TransformGroup(); objSpin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); moveCone.addChild(objSpin); Transform3D rotate = new Transform3D(); Transform3D tempRotate = new Transform3D(); rotate.rotX(Math.PI/3.0d); tempRotate.rotY(Math.PI/6.0d); rotate.mul(tempRotate); Alpha rotationAlpha = new Alpha(-1, 8000); RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, objSpin, rotate, 0.0f ,(float)(2*Math.PI)); BoundingSphere bounds = new BoundingSphere(); rotator.setSchedulingBounds(bounds); objSpin.addChild(rotator); Cone cone = new Cone(0.4f, 0.4f); Appearance color = new Appearance(); color.setColoringAttributes(new ColoringAttributes(1f,0f,0f,1)); cone.setAppearance(color); objSpin.addChild(cone); return moveCone; } public TransformGroup createCylinder() { Transform3D t = new Transform3D(); t.set(new Vector3f(-1.0f,-1.0f,0.0f)); TransformGroup moveCylinder = new TransformGroup(t); TransformGroup objSpin = new TransformGroup(); objSpin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); moveCylinder.addChild(objSpin); Transform3D rotate = new Transform3D(); Transform3D tempRotate = new Transform3D(); rotate.rotX(Math.PI/4.0d); tempRotate.rotY(Math.PI/5.0d); rotate.mul(tempRotate); Alpha rotationAlpha = new Alpha(-1, 12000); RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, objSpin, rotate, 0.0f ,(float)(2*Math.PI)); BoundingSphere bounds = new BoundingSphere(); rotator.setSchedulingBounds(bounds); objSpin.addChild(rotator); Cylinder cylinder = new Cylinder(0.4f, 0.4f); Appearance color = new Appearance(); color.setColoringAttributes(new ColoringAttributes(0f,1f,0f,1)); cylinder.setAppearance(color); objSpin.addChild(cylinder); return moveCylinder; } public TransformGroup createText() { Transform3D t = new Transform3D(); t.set(new Vector3f(1.0f,-1.0f,0.0f)); TransformGroup moveText = new TransformGroup(t); TransformGroup objSpin = new TransformGroup(); objSpin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); moveText.addChild(objSpin); Alpha rotationAlpha = new Alpha(-1, 4000); RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, objSpin); BoundingSphere bounds = new BoundingSphere(); rotator.setSchedulingBounds(bounds); objSpin.addChild(rotator); objSpin.addChild(new Text2D("Go Yankees", new Color3f(0f,0f, 1f), "Serif", 40, Font.BOLD)); return moveText; } public static void main(String[] args) { Frame frame = new MainFrame(new FourObjects2(), 256, 256); } } // end of class FourObjects2