// Copyright (c) 2002 Pro-3D
package max;

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.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.image.TextureLoader;
import javax.media.j3d.Behavior;
import javax.media.j3d.Light;
import javax.media.j3d.RotationInterpolator;

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

import  java.io.RandomAccessFile;
import  java.io.IOException;
import  java.applet.Applet;
import  java.awt.*;
import  java.awt.event.*;
import  javax.media.j3d.*;
import  javax.vecmath.*;
import  java.util.Hashtable;
import  java.util.Properties;

import  com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import  com.sun.j3d.utils.behaviors.mouse.MouseZoom;
import  com.sun.j3d.utils.behaviors.mouse.MouseTranslate;

/**
 * Applet
 * <P>
 * @author Rob
 */
public class ShowMax extends Applet {
  boolean isStandalone = false;

  Canvas3D            canvas          = null;
  VirtualUniverse     universe        = null;
  Locale              locale          = null;
  PhysicalBody        physicalBody    = null;
  PhysicalEnvironment physicalEnv     = null;
  View                view            = null;
  ViewPlatform        vp              = null;
  TransformGroup      vpTrans         = null;
  BranchGroup         sceneRoot       = null;
  float		            scalingFactor;
  String 		          filename;

  /**
   * Constructs a new instance.
   */
  /**
   * getParameter
   * @param key
   * @param def
   * @return java.lang.String
   */
  public String getParameter(String key, String def) {
    if (isStandalone) {
      return System.getProperty(key, def);
    }
    if (getParameter(key) != null) {
      return getParameter(key);
    }
    return def;
  }

  public ShowMax() {
  }

  /**
   * Initializes the state of this instance.
   */
  /**
   * init
   */
 /* public void init() {
    try  {
      jbInit();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
/*
  private void jbInit() throws Exception {
    xYLayout1.setWidth(700);
    xYLayout1.setHeight(500);
    this.setLayout(xYLayout1);
    this.setBackground(SystemColor.control);
  }
*/

  public void init() {
    	filename= "Szene.3DS";
//      	filename= "http://f:\\3DS\\Szene.3DS";
        System.out.println( "PARAM: Loading Data from " + filename);
    }

  /**
   * start
   */
  public void start() {

  System.out.println( "START: " + filename);
  //filename = "F://Szene.3DS";

  //
  // Create a canvas that Java3D can render into and
  // add it to the applet window.
  //
	scalingFactor	= 0.05f;

  setLayout( new BorderLayout() );

  canvas = new Canvas3D( null );
  add( "Center", canvas );
  canvas.setSize( 700, 500 );

  //
  // Create the basic structure of a Universe.
  //
  createUniverse();

  //
  // Now create some content for the scene and attach it to
  // the scene graph.
  //
  createScene( filename );
        locale.addBranchGraph( sceneRoot );
  }

  /**
   * stop
   */
  public void stop() {
  }

  /**
   * destroy
   */
  public void destroy() {
  }

  /**
   * getAppletInfo
   * @return java.lang.String
   */
  public String getAppletInfo() {
    return "Applet Information";
  }

  /**
   * getParameterInfo
   * @return java.lang.String[][]
   */
  public String[][] getParameterInfo() {
    return null;
  }
      private void createUniverse()
    {
      System.out.println( "CreateUniverse: " + filename);
        //
        // Create container of all things Java3D.
        //
        universe    = new VirtualUniverse();

        //
        // Create a HiRes Locale in the universe at ( 0, 0, 0 ).
        //
        locale      = new Locale( universe );

        //
        // Create an appropriate view.
        //
        createView();
    }

    private void createView()
    {
        System.out.println( "CreateView: " + filename);
        //
        // Create a default representation of the user's head and
        // the environment they inhabit.
        //
        physicalBody    = new PhysicalBody();
        physicalEnv     = new PhysicalEnvironment();

        //
        // Create a View which contains all the information needed to
        // render a view of the world, including the canvas to render
        // onto.
        //
        view    = new View();
        view.addCanvas3D( canvas );
        view.setPhysicalBody( physicalBody );
        view.setPhysicalEnvironment( physicalEnv );

        //
        // Now create a ViewPlatform and a home for it in the
        // scene graph.  Transform the view platform 4 metres
        // along the z axis, "out of the screen".
        //
        BranchGroup     vpRoot  = new BranchGroup();
        Transform3D     t       = new Transform3D();

        t.set( new Vector3f( 0.0f, 0.0f, 5.0f ) );

        TransformGroup  vpTrans = new TransformGroup( t );

        vp = new ViewPlatform();
        vpTrans.addChild( vp );
        vpRoot.addChild( vpTrans );

        view.attachViewPlatform( vp );

        //
        // Now that we have a view platform, attach it at the
        // top level of the locale.
        //
        locale.addBranchGraph( vpRoot );
    }

    private BranchGroup createScene( String filename )
    {
        System.out.println( "BranchGroup: " + filename);
        //
        // Create a home for the scene in the scene graph.
        //
        sceneRoot   = new BranchGroup();

        //
        // Add a background.
        //
  	    BoundingSphere  bounds  =
	                        new BoundingSphere( new Point3d( 0.0, 0.0, 0.0 ), 100.0 );

  	    Background      bg      = new Background( 0.1f, 0.4f, 0.7f );
      	bg.setApplicationBounds( bounds );
        sceneRoot.addChild( bg );

        //
        // Add some fog.
        //
/*
        LinearFog   fog = new LinearFog( 1.0f, 1.0f, 1.0f, 0.0f, 10.0f );
        fog.setInfluencingBounds( bounds );
        sceneRoot.addChild( fog );
*/

        //
        // Add a light.
        //
/* */
        SpotLight   spot = new SpotLight( new Color3f( 1.0f, 1.0f, 1.0f ),
                                          new Point3f( 5.0f, 5.0f, 5.0f ),
                                          new Point3f( 1.0f, 1.0f, 1.0f ),
                                          new Vector3f( 0.5f, 0.5f, 0.5f ),
                                          (float)Math.PI / 2, (float)Math.PI / 4 );
        spot.setInfluencingBounds( bounds );
        sceneRoot.addChild( spot );

        DirectionalLight dl = new DirectionalLight( new Color3f( 1.0f, 1.0f, 1.0f ),
                                                  new Vector3f( 0.0f, 0.0f, -1.0f ) );
        dl.setInfluencingBounds( bounds );
//        sceneRoot.addChild( dl );

        Vector3f direction = new Vector3f( -4.0f, -6.0f, -4.0f );
        direction.normalize();
        dl = new DirectionalLight( new Color3f( 1.0f, 1.0f, 1.0f ),
                                   direction );
        dl.setInfluencingBounds( bounds );
        sceneRoot.addChild( dl );

        try
        {
        	Transform3D	transform = new Transform3D();
        	transform.setIdentity();
        	transform.setScale( scalingFactor );
	        TransformGroup worldTrans   = new TransformGroup( transform );

        	worldTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
	        worldTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
	        sceneRoot.addChild( worldTrans );

         	//
	        // Create the drag behavior node
	        //
	        MouseRotate behavior = new MouseRotate( worldTrans );
	        behavior.setSchedulingBounds( bounds );
	        sceneRoot.addChild( behavior );

          //
        	// Create the zoom behavior node
          //
        	MouseZoom behavior2 = new MouseZoom( worldTrans );
        	behavior2.setSchedulingBounds( bounds );
	        sceneRoot.addChild( behavior2 );

        	//
        	// Create the translate behavior node
        	//
        	MouseTranslate behavior3 = new MouseTranslate( worldTrans );
        	behavior3.setSchedulingBounds( bounds );
	        sceneRoot.addChild( behavior3 );

       		System.out.println( "Loading..." );
          Properties  properties  = new Properties();
          Load3DS     l           = new Load3DS( filename, this,
                                    new Hashtable(),
                                    new Hashtable(),
                                    properties );

          System.out.println( "# Polygons = " + properties.get( "polygons" ) );
          System.out.println( "# Vertices = " + properties.get( "vertices" ) );

          worldTrans.addChild( l.root() );
        }
        catch ( IOException e )
        {
        	System.err.println( "** Exception: " + e.getMessage() );
            e.printStackTrace( System.err );
        }
        return sceneRoot;
    }


  /**
   * main
   * @param args
   */
   public static void main(String[] args) {
      new MainFrame(new ShowMax(), 750, 480);
   }
/*  public static void main(String[] args) {
    ShowMax applet = new ShowMax();
    applet.isStandalone = true;
    Frame frame = new Frame();
    frame.setTitle("Applet Frame");
    frame.add(applet, BorderLayout.CENTER);
    applet.init();
    applet.start();
    frame.setSize(700, 500);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
    frame.setVisible(true);
    frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } });
  }
  */
}

