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 NotSoSimple3D extends JFrame { public NotSoSimple3D() { setSize(600, 600); GraphicsConfiguration graphicsConfig = SimpleUniverse.getPreferredConfiguration(); Canvas3D canvas = new Canvas3D(graphicsConfig); getContentPane().add("Center", canvas); BasicUniverse universe = new BasicUniverse(canvas, 8.0f); // Rotate the view platform by PI/4 radians about X in the BasicUniverse TransformGroup viewTransform = universe.getViewPlatformTransform(); Transform3D transform = new Transform3D(); transform.rotX(Math.PI / 4.0); Transform3D currentTransform = new Transform3D(); viewTransform.getTransform(currentTransform); transform.mul(currentTransform); viewTransform.setTransform(transform); // Add something to display BranchGroup scene = createCubeGraph(); universe.addBranchGraph(scene); } public BranchGroup createCubeGraph() { BranchGroup objRoot = new BranchGroup(); // Add an unrotated cube to the branch graph objRoot.addChild(new ColorCube()); // Create a transform to rotate a further cube by PI/4 radians about Y Transform3D T3D = new Transform3D(); T3D.rotY(Math.PI / 4.0); // Create a transform group for this transform TransformGroup TG = new TransformGroup(); TG.setTransform(T3D); // Add in the cube TG.addChild(new ColorCube()); // Create another transform group to rotate another cube about Y Transform3D anotherT3D = new Transform3D(); anotherT3D.rotY(3*Math.PI / 4.0); TransformGroup anotherTG = new TransformGroup(); anotherTG.setTransform(anotherT3D); // Note that swing also has a Box method // So we have to specify that we want the j3d.utils.geometry method here anotherTG.addChild( new com.sun.j3d.utils.geometry.Box(1.0f,1.0f,1.0f, com.sun.j3d.utils.geometry.Box.GENERATE_NORMALS,new Appearance())); // Add the second transform group to the first TG.addChild(anotherTG); // Create yet another transform group to rotate a cylinder about Z Appearance cylinderAppearance = new Appearance(); Material cylinderMaterial = new Material(); Color3f pureBlue = new Color3f(0,0,1); cylinderMaterial.setEmissiveColor(pureBlue); cylinderAppearance.setMaterial(cylinderMaterial); Transform3D yetAnotherT3D = new Transform3D(); yetAnotherT3D.rotZ(5*Math.PI / 4.0); TransformGroup yetAnotherTG = new TransformGroup(); yetAnotherTG.setTransform(yetAnotherT3D); yetAnotherTG.addChild( new Cylinder(1.5f,2.0f,Cylinder.GENERATE_NORMALS,cylinderAppearance)); // Add in a sphere for good measure Appearance sphereAppearance = new Appearance(); Material sphereMaterial = new Material(); Color3f pureGreen = new Color3f(0,1,0); sphereMaterial.setEmissiveColor(pureGreen); sphereAppearance.setMaterial(sphereMaterial); yetAnotherTG.addChild( new Sphere(1.5f,Sphere.GENERATE_NORMALS,sphereAppearance)); // Add the third transform group to the second anotherTG.addChild(yetAnotherTG); // Add the two nested transform groups to the branch graph objRoot.addChild(TG); return objRoot; } public static void main(String[] args) { NotSoSimple3D myPrimitives = new NotSoSimple3D(); myPrimitives.setVisible(true); } }