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 HelloUniverse extends Applet 
{
    public BranchGroup createSceneGraph() 
    {
        // Create the root of the branch graph
        BranchGroup objRoot = new BranchGroup();

        // Create the transform group node and initialize it to the
        // identity.  Enable the TRANSFORM_WRITE capability so that
        // our behavior code can modify it at runtime.  Add it to the
        // root of the subgraph.
        TransformGroup objTrans = new TransformGroup();
        objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objRoot.addChild(objTrans);

        // Create a simple shape leaf node, add it to the scene graph.
        objTrans.addChild(new ColorCube(0.4));
        
         //-----------------------------------------
         // neils test - add a floor grid so that we can more easily view what happens
         LineArray landGeom = new LineArray(44, GeometryArray.COORDINATES | GeometryArray.COLOR_3);
         float l = -50.0f;
         for(int c = 0; c < 44; c+=4)
         {
            landGeom.setCoordinate( c+0, new Point3f( -50.0f, -0.4f,  l ));
            landGeom.setCoordinate( c+1, new Point3f(  50.0f, -0.4f,  l ));
            landGeom.setCoordinate( c+2, new Point3f(   l , -0.4f, -50.0f ));
            landGeom.setCoordinate( c+3, new Point3f(   l , -0.4f,  50.0f ));
            l += 10.0f;
         }
         // color the grid
         Color3f c = new Color3f(0.1f, 0.8f, 0.1f);
         for(int i = 0; i < 44; i++) 
            landGeom.setColor( i, c);
         // and add it....              
         objTrans.addChild(new Shape3D(landGeom));
         // OK, now a grid floor is there - easier to see the rotation, yes?        
         //----------------------------------------------------
                         
        AxisAngle4f axisAngle = new AxisAngle4f(0.0f, 0.0f, 1.0f, 
                                                -(float)Math.PI / 2.0f);
        Transform3D yAxis = new Transform3D();
        Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE,
                                        0, 0,
                                        4000, 0, 0,
                                        0, 0, 0);
        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);
        objTrans.addChild(rotator);

        // Have Java 3D perform optimizations on this scene graph.
        objRoot.compile();

        return objRoot;
    }

    public HelloUniverse() 
    {
        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();
        SimpleUniverse u = new SimpleUniverse(c);

//-------------Here's what you gave me--------
        ViewPlatform viewPlatform = new ViewPlatform();                
 
        //reset the camera and the position transforms
        TransformGroup mCameraTransform = new TransformGroup();
 
        mCameraTransform.setCapability(TransformGroup.ALLOW_CHILDREN_READ);
        mCameraTransform.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);
        mCameraTransform.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
        mCameraTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        mCameraTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        mCameraTransform.addChild(viewPlatform);

        scene.setCapability(BranchGroup.ALLOW_CHILDREN_READ);         
        scene.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
        scene.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
 
        //vpBGRoot is the branchgroup root of the viewable scene graph
        //sceneRoot.addChild(vpBGRoot);
        //the camera transform is what moves the view platform around
        scene.addChild(mCameraTransform);
 
        Transform3D t3d = new Transform3D();
        t3d.setTranslation(new Vector3d(0.0, 1.0, 5.0));
        mCameraTransform.setTransform(t3d);
 
        //weird how Java3d has ViewPlatform, ViewingPlatform..
        //why not VeiwingContainer? or something like that? oh well.
        ViewingPlatform viewplat = new ViewingPlatform();
        viewplat.setViewPlatform(viewPlatform);

        u.getViewer().setViewingPlatform(viewplat);
        u.getViewer().getView().setBackClipDistance(100);
        
         //----------------------------------------------
         // Today's question: this should disable resising of the objects 
         // when the window gets resized.  But, it does not.  Why?
         u.getViewer().getView().setWindowResizePolicy(View.VIRTUAL_WORLD);
         //------------------------------------------------
        	
                // This will move the ViewPlatform back a bit so the
                // objects in the scene can be viewed.
                u.getViewingPlatform().setNominalViewingTransform();
        
//----------------------

        u.addBranchGraph(scene);
    }

    //
    // The following allows HelloUniverse to be run as an application
    // as well as an applet
    //
    public static void main(String[] args) 
    {
        new MainFrame(new HelloUniverse(), 256, 256);
    }
}
