This compiles nicely.import java.applet.Applet; public class HelloU extends Applet { }
We add an empty constructor, and a main method.
Now you can compile and run this program, but what do we have?import java.applet.Applet; import com.sun.j3d.utils.applet.MainFrame; public class HelloU extends Applet { public HelloU() { } public static void main(String[] args) { new MainFrame(new HelloU(), 256, 256); } }
The program above does not compile. Why?import java.applet.Applet; import com.sun.j3d.utils.applet.MainFrame; public class HelloU extends Applet { public HelloU() { } public static void main(String[] args) { new MainFrame(new HelloU(), 256, 256); } public void init() { } public void destroy() { u.removeAllLocales(); } private SimpleUniverse u = null; }
Now it compiles, runs and you can even see a small change in its appearance.import java.applet.Applet; import com.sun.j3d.utils.applet.MainFrame; import com.sun.j3d.utils.universe.*; import java.awt.BorderLayout; import java.awt.GraphicsConfiguration; import javax.media.j3d.*; public class HelloU extends Applet { public HelloU() { } public static void main(String[] args) { new MainFrame(new HelloU(), 256, 256); } public void init() { setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); Canvas3D c = new Canvas3D(config); // http://java.sun.com/products/java-media/3D/forDevelopers/J3D_1_3_API/j3dapi/javax/media/j3d/Canvas3D.html add("Center", c); u = new SimpleUniverse(c); } public void destroy() { u.removeAllLocales(); } private SimpleUniverse u = null; }
The scene has been set for what we want to show:import java.applet.Applet; import com.sun.j3d.utils.applet.MainFrame; import com.sun.j3d.utils.universe.*; import java.awt.BorderLayout; import java.awt.GraphicsConfiguration; import javax.media.j3d.*; public class HelloU extends Applet { public HelloU() { } public static void main(String[] args) { new MainFrame(new HelloU(), 256, 256); } public void init() { setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); Canvas3D c = new Canvas3D(config); add("Center", c); BranchGroup scene = createSceneGraph(); u = new SimpleUniverse(c); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(scene); } public BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); return objRoot; } public void destroy() { u.removeAllLocales(); } private SimpleUniverse u = null; }
Compile and run the program. What changes do you notice?import java.applet.Applet; import com.sun.j3d.utils.applet.MainFrame; import com.sun.j3d.utils.universe.*; import java.awt.BorderLayout; import java.awt.GraphicsConfiguration; import javax.media.j3d.*; import com.sun.j3d.utils.geometry.ColorCube; public class HelloU extends Applet { public HelloU() { } public static void main(String[] args) { new MainFrame(new HelloU(), 256, 256); } public void init() { setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); Canvas3D c = new Canvas3D(config); add("Center", c); BranchGroup scene = createSceneGraph(); u = new SimpleUniverse(c); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(scene); } public BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); TransformGroup objTrans = new TransformGroup(); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objRoot.addChild(objTrans); objTrans.addChild(new ColorCube(0.4)); return objRoot; } public void destroy() { u.removeAllLocales(); } private SimpleUniverse u = null; }
Now the program is in almost its final state.import java.applet.Applet; import java.awt.BorderLayout; 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.*; public class HelloU extends Applet { public HelloU() { } public static void main(String[] args) { new MainFrame(new HelloU(), 256, 256); } public void init() { setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); Canvas3D c = new Canvas3D(config); add("Center", c); BranchGroup scene = createSceneGraph(); u = new SimpleUniverse(c); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(scene); } public BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); TransformGroup objTrans = new TransformGroup(); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objRoot.addChild(objTrans); objTrans.addChild(new ColorCube(0.4)); Transform3D yAxis = new Transform3D(); Alpha rotationAlpha = new Alpha(-1, 4000); RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, objTrans, yAxis, 0.0f, (float)Math.PI * 2.0f); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); rotator.setSchedulingBounds(bounds); objRoot.addChild(rotator); return objRoot; } public void destroy() { u.removeAllLocales(); } private SimpleUniverse u = null; }
What's the benefit of the last few added pieces?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.*; public class HelloU extends Applet { public HelloU() { } public static void main(String[] args) { new MainFrame(new HelloU(), 256, 256); } public void init() { setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); Canvas3D c = new Canvas3D(config); add("Center", c); BranchGroup scene = createSceneGraph(); u = new SimpleUniverse(c); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(scene); } public BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); TransformGroup objTrans = new TransformGroup(); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objRoot.addChild(objTrans); objTrans.addChild(new ColorCube(0.4)); Transform3D yAxis = new Transform3D(); Alpha rotationAlpha = new Alpha(-1, 4000); RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, objTrans, yAxis, 0.0f, (float)Math.PI * 2.0f); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); rotator.setSchedulingBounds(bounds); objRoot.addChild(rotator); objRoot.compile(); return objRoot; } public void destroy() { u.removeAllLocales(); } private SimpleUniverse u = null; }