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 Example extends Applet {

    private SimpleUniverse u = null;

    public BranchGroup createSceneGraph() {
      // Create the root of the branch graph
      BranchGroup objRoot = new BranchGroup();

      // Create the first cube
      // Set it to (-3.0, 0.0, -15.0)
      // It is STILL visible with far clipping plane set to 5.0
      Transform3D cube1Trans = new Transform3D();
      cube1Trans.set( new Vector3d(-3.0, 0.0, -15.0) );
      TransformGroup cube1TransGroup = new TransformGroup(cube1Trans);
      cube1TransGroup.addChild(new ColorCube(0.4));
      objRoot.addChild(cube1TransGroup);

      // Create the second cube
      // Set it to (-3.0, 0.0, -25.0)
      // It is STILL visible with far clipping plane set to 5.0
      Transform3D cube2Trans = new Transform3D();
      cube2Trans.set( new Vector3d(0.0, 0.0, -25.0) );
      TransformGroup cube2TransGroup = new TransformGroup(cube2Trans);
      cube2TransGroup.addChild(new ColorCube(0.4));
      objRoot.addChild(cube2TransGroup);

      // Create the third cube
      // Set it to (-3.0, 0.0, -35.0)
      // It is NOT visible with far clipping plane set to 5.0
      Transform3D cube3Trans = new Transform3D();
      cube3Trans.set( new Vector3d(3.0, 0.0, -35.0) );
      TransformGroup cube3TransGroup = new TransformGroup(cube3Trans);
      cube3TransGroup.addChild(new ColorCube(0.4));
      objRoot.addChild(cube3TransGroup);

      // Have Java 3D perform optimizations on this scene graph.
      objRoot.compile();

      return objRoot;
    }

    public Example() {
    }

    public void init() {
      setLayout(new BorderLayout());
           GraphicsConfiguration config =
              SimpleUniverse.getPreferredConfiguration();

      Canvas3D c = new Canvas3D(config);
      add("Center", c);

      // Create a simple scene and attach it to the virtual universe
      BranchGroup scene = createSceneGraph();
      u = new SimpleUniverse(c);

      u.addBranchGraph(scene);

      // Set the Back Clipping plane to 5.0
      c.getView().setBackClipDistance( 5.0 );
    }

    public void destroy() {
      u.removeAllLocales();
    }

    //
    // The following allows HelloUniverse to be run as an application
    // as well as an applet
    //
    public static void main(String[] args) {
      new MainFrame(new Example(), 500, 500);
    }
}
