//Source file: C:/jdk1.2.2/jre/lib/MainMan3D/DesignTool/Graphical/BGScene.java

import java.util.Observer;
import java.net.URL;
import java.util.Observable;
import java.awt.*;
import com.sun.j3d.utils.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import javax.vecmath.Tuple3f.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.behaviors.interpolators.*;
import java.lang.Double;
import com.sun.j3d.loaders.vrml97.*;
import java.net.*;
import java.io.*;
import java.awt.event.*;
import java.util.*;


public class BGScene extends Canvas3D implements Observer
{

        private BranchGroup                     scene;
        private VirtualUniverse                 universe;
        private javax.media.j3d.Locale  locale;
        private BranchGroup                     vpRoot;
        private TransformGroup                  viewTransformGroup;
        private View                                    view;
        private ViewPlatform                    viewPlatform;
        private PhysicalBody                    body;
        private PhysicalEnvironment     environment;
        private Transform3D                     viewTransform;
        private BGroup                                  group;
        private BranchGroup                     sceneRoot;
        private int                                     teller;
        private AmbientLight                    ambLight;
        private DirectionalLight                headLight;
        private URL                                     url;

   public BGScene(String name, URL url)
   {
           super(null);
           this.url = url;
           this.setupJ3D();
           this.setupScene();

                        // Creating lights
                        sceneRoot = new BranchGroup();
                        sceneRoot.setCapability(Group.ALLOW_CHILDREN_READ);
                        sceneRoot.setCapability(Group.ALLOW_CHILDREN_WRITE);
                        sceneRoot.setCapability(Group.ALLOW_CHILDREN_EXTEND);
                        sceneRoot.setCapability(BranchGroup.ALLOW_DETACH);


                        // maken van een ambientLight
                        BoundingSphere lightBounds = new BoundingSphere(new Point3d(), Double.MAX_VALUE);
                        ambLight = new AmbientLight(true, new Color3f(1.0f, 1.0f, 1.0f));
                        ambLight.setInfluencingBounds(lightBounds);
                        ambLight.setCapability(Light.ALLOW_STATE_WRITE);
                        sceneRoot.addChild(ambLight);

            // maken van een DirectionalLight
                headLight = new DirectionalLight();
                headLight.setCapability(Light.ALLOW_STATE_WRITE);
                headLight.setInfluencingBounds(lightBounds);
                sceneRoot.addChild(headLight);
                sceneRoot.addChild(this.scene);
                // Toevoegen van de lights aan de locale
                locale.addBranchGraph(sceneRoot);
                System.out.println(this);

        }

   /**
   @roseuid 39D0AEEB03D4
   */
   public void update(java.util.Observable arg0, java.lang.Object arg1)
   {
           System.out.println(this);
   }

   public void setupJ3D(){


                universe = new VirtualUniverse();

                locale = new javax.media.j3d.Locale(universe);

                vpRoot = new BranchGroup();
                vpRoot.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
                vpRoot.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
                vpRoot.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);


                viewTransform = new Transform3D();
                viewTransform.set(new Vector3f(0.0f, 0.0f, 3.0f));
                viewTransformGroup = new TransformGroup(viewTransform);
                viewTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
                viewTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);


                view = new View();

                viewPlatform = new ViewPlatform();

                body = new PhysicalBody();

                environment = new PhysicalEnvironment();

                view.setPhysicalBody(body);
                view.setPhysicalEnvironment(environment);
                view.attachViewPlatform(viewPlatform);
                view.addCanvas3D(this);

                viewTransformGroup.addChild(viewPlatform);

                vpRoot.addChild(viewTransformGroup);

                locale.addBranchGraph(vpRoot);
           }


  public void setupScene(){
                try {

                                VrmlScene scene;
                                VrmlLoader loader = new VrmlLoader();
                                scene = (VrmlScene) loader.load(url);

                                Vector3d offset = new Vector3d();
                                Transform3D objTrans = new Transform3D();
                                BranchGroup sceneGroup = scene.getSceneGroup();
                                sceneGroup.setCapability(BranchGroup.ALLOW_BOUNDS_READ);
                                sceneGroup.setCapability(BranchGroup.ALLOW_BOUNDS_WRITE);

                                // create the setup viewpoint
                                BoundingSphere sceneBounds = new BoundingSphere(sceneGroup.getBounds());
                                double radius = sceneBounds.getRadius();
                                Point3d center = new Point3d();
                                sceneBounds.getCenter(center);
                                offset.x = center.x;
                                offset.y = center.y;
                                offset.z = center.z;
                                System.out.println("x: " + offset.x + " y: " + offset.y + " z: " + offset.z + " radius: " + radius);
                                objTrans.set(radius, offset);
                                System.out.println("objTrans: " + objTrans);
                                viewTransformGroup.setTransform(objTrans);

                                // create the scene
                                Transform3D rotate = new Transform3D();
                                rotate.rotX(Math.PI / 4.0d);
                                group = new BGroup("kubus", scene.getSceneGroup(), new TransformGroup(rotate));

                                this.scene = group;


                        }
                        catch (java.io.IOException e) {
                                System.err.println("IO exception reading URL");
                        }
                        catch (vrml.InvalidVRMLSyntaxException e) {
                                System.err.println("VRML parse error");
                                e.printStackTrace(System.err);
                }



          }



}
