import java.awt.*; import java.awt.event.*; import com.sun.j3d.utils.geometry.*; import com.sun.j3d.utils.universe.*; import javax.swing.*; import javax.media.j3d.*; import javax.vecmath.*; public class Orbiter extends JFrame { public Orbiter() { initComponents (); setSize(600, 600); GraphicsConfiguration graphicsConfig = SimpleUniverse.getPreferredConfiguration(); Canvas3D canvas = new Canvas3D(graphicsConfig); getContentPane().add("Center", canvas); BasicUniverse universe = new BasicUniverse(canvas, 20.0f); // Rotate the view platform to get an interesting view TransformGroup viewTransform = universe.getViewPlatformTransform(); Transform3D transform = new Transform3D(); transform.rotZ(-Math.PI/4.0); Transform3D currentTransform = new Transform3D(); viewTransform.getTransform(currentTransform); transform.mul(currentTransform); viewTransform.setTransform(transform); // Add a small sphere orbiting a large sphere - both spinning BranchGroup scene = createOrbiterGraph(); universe.addBranchGraph(scene); } public BranchGroup createOrbiterGraph() { BranchGroup objRoot = new BranchGroup(); // Create an identity transform to permit a large sphere to spin Transform3D largeT3D = new Transform3D(); // Create a transform group for this transform TransformGroup largeTG = new TransformGroup(); largeTG.setTransform(largeT3D); largeTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); largeTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); // Create an appearance and add in the large sphere Appearance largeAppearance = new Appearance(); Material largeMaterial = new Material(); Color3f pureBlue = new Color3f(0,0,1); largeMaterial.setEmissiveColor(pureBlue); largeAppearance.setMaterial(largeMaterial); largeTG.addChild(new Sphere(2.0f,Sphere.GENERATE_NORMALS,largeAppearance)); // Add the spin for the large sphere to the branch graph objRoot.addChild(largeTG); // Create a transform group to permit a smaller sphere // to orbit the larger sphere Transform3D orbitT3D = new Transform3D(); TransformGroup orbitTG = new TransformGroup(); orbitTG.setTransform(orbitT3D); orbitTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); orbitTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); // Add the orbiter for the small sphere to the branch graph objRoot.addChild(orbitTG); // Create another transform group to offset the small sphere // from the large sphere Transform3D offsetT3D = new Transform3D(new Matrix4d( 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 6 , 0 , 0 , 0 , 1 )); TransformGroup offsetTG = new TransformGroup(); offsetTG.setTransform(offsetT3D); // Add the offset transform into the orbit transform orbitTG.addChild(offsetTG); // Create a further transform group to permit the small sphere to spin Transform3D smallT3D = new Transform3D(); TransformGroup smallTG = new TransformGroup(); smallTG.setTransform(smallT3D); smallTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); smallTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); // Add the spinning transform into the offset transform offsetTG.addChild(smallTG); // Create an appearance and add in the small sphere Appearance smallAppearance = new Appearance(); Material smallMaterial = new Material(); Color3f pureGreen = new Color3f(0,1,0); smallMaterial.setEmissiveColor(pureGreen); smallAppearance.setMaterial(smallMaterial); smallTG.addChild(new Sphere(1.0f,Sphere.GENERATE_NORMALS,smallAppearance)); // Animation part Alpha alpha = new Alpha(-1,5000); BoundingSphere system = new BoundingSphere(); // Create the rotation interpolator to spin the large sphere RotationInterpolator largeInterpolator = new RotationInterpolator(alpha,largeTG); largeInterpolator.setSchedulingBounds(system); // Note that the rotation is about the local Y axis by default objRoot.addChild(largeInterpolator); // Create the rotation interpolator for the small sphere orbital RotationInterpolator orbitInterpolator = new RotationInterpolator(alpha,orbitTG); orbitInterpolator.setSchedulingBounds(system); // Note that the rotation is about the local Y axis by default objRoot.addChild(orbitInterpolator); // Create the rotation interpolator to spin the small sphere RotationInterpolator smallInterpolator = new RotationInterpolator(alpha,smallTG); smallInterpolator.setSchedulingBounds(system); // Note that the rotation is about the local Y axis by default objRoot.addChild(smallInterpolator); return objRoot; } private void initComponents() { addWindowListener (new WindowAdapter () { public void windowClosing (WindowEvent evt) {exitForm (evt);} } ); } /*** Exit Application ***/ private void exitForm(WindowEvent evt) {System.exit (0);} public static void main(String[] args) { Orbiter mySystem = new Orbiter(); mySystem.setVisible(true); } }