/*
// header - edit "Data/yourJavaHeader" to customize
// contents - edit "EventHandlers/Java file/onCreate" to customize
//
*/


import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.image.TextureLoader;
import javax.media.j3d.*;
import javax.vecmath.*;




public class firsttry extends Applet
{

        private SimpleUniverse u = null;

        public firsttry(){

        }

                public BranchGroup createSceneGraph(Canvas3D c) {
                // Create the root of the branch graph
                BranchGroup objRoot = new BranchGroup();

                // Create a bounds for the background and behaviors
                BoundingSphere bounds =
                        new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);

                // Set up the background
                Color3f bgColor = new Color3f(0.5f, 0.1f, 0.7f);
                Background bg = new Background(bgColor);
                bg.setApplicationBounds(bounds);
                objRoot.addChild(bg);

                // Set up the global lights
                Color3f lColor1 = new Color3f(1.0f, 1.0f, 1.0f);
                Vector3f Dir1  = new Vector3f(-1.0f, -1.0f, -1.0f);
                Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);

                AmbientLight aLgt = new AmbientLight(alColor);
                aLgt.setInfluencingBounds(bounds);
                DirectionalLight lgt1 = new DirectionalLight(lColor1, Dir1);
                lgt1.setInfluencingBounds(bounds);
                objRoot.addChild(aLgt);
                objRoot.addChild(lgt1);

                // Create a bunch of objects with a behavior and add them
                // into the scene graph.
        Appearance app = new Appearance();
        Color3f black = new Color3f(1.0f, 0.0f, 0.0f);
        Color3f white = new Color3f(1.0f, 0.0f, 0.0f);


        Color3f objColor = new Color3f(0.8f, 0.9f, 0.3f);
        app.setMaterial(new Material(objColor, black, objColor,
                                        white, 80.0f));

        PolygonAttributes pa = new PolygonAttributes();
                                pa.setPolygonMode(pa.POLYGON_LINE);
                                pa.setCullFace(pa.CULL_NONE);
                                app.setPolygonAttributes(pa);

        objRoot.addChild(createObject(app, 0.1));


                objRoot.compile();

                return objRoot;

        }


        private Group createObject(Appearance app, double scale) {

                // Create a transform group node to scale and position the object.
                Transform3D t = new Transform3D();
                t.set(scale, new Vector3d(0.0, 0.0, 0.0));
                TransformGroup objTrans = new TransformGroup(t);

                // Create a second transform group node and initialize it to the
                // identity.  Enable the TRANSFORM_WRITE capability so that
                // our behavior code can modify it at runtime.
                TransformGroup spinTg = new TransformGroup();
                spinTg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
                spinTg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
//Primitive g = null;
Shape3D obj = null;
                obj = new Tetrahedron1();   // shows backlines
                //obj = new Cube(3.0,3.0,3.0); // do not show backlines


                obj.setAppearance(app);
                //obj = new Box3(2.0,2.0,2.0);

                //Shape3D shape = new Shape3D(g,app);

                //obj.setAppearance(app);
                //      obj = (Primitive) new Box(4.5f, 4.5f, 4.5f, app);

                /*obj = (Primitive) new Sphere(3.0f,
                                Sphere.GENERATE_NORMALS |
                                Sphere.GENERATE_TEXTURE_COORDS,
                                1*8 + 4, app);*/

                // add it to the scene graph.
                                spinTg.addChild(obj);

                // Create a new Behavior object that will perform the desired
                // operation on the specified transform object and add it into
                // the scene graph.
                Transform3D yAxis = new Transform3D();



                yAxis.rotX(Math.PI*0.25);
                Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE,
                        0, 0,
                        5000, 0, 0,
                        0, 0, 0);

                RotationInterpolator rotator =
                        new RotationInterpolator(rotationAlpha, spinTg, 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);

                // Add the behavior and the transform group to the object
                objTrans.addChild(rotator);
                objTrans.addChild(spinTg);

                return objTrans;
        }


        public void init(){

                setLayout(new BorderLayout());
                GraphicsConfiguration config =
                        SimpleUniverse.getPreferredConfiguration();

                Canvas3D c = new Canvas3D(config);
                add("Center", c);


                BranchGroup scene = createSceneGraph(c);
                u = new SimpleUniverse(c);


                u.getViewingPlatform().setNominalViewingTransform();

                u.addBranchGraph(scene);
        }

        public void destroy() {
                u.removeAllLocales();
        }



        public static void main(String args[]){


                new MainFrame(new firsttry(),300,300);

                }

}

