
//Author: Terence J Fagan

//import statementes
import com.sun.j3d.utils.behaviors.mouse.*;
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame; 
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;


public class my3DBoxApp extends Applet{

  float height = .2f;
  float width = .2f;
  float depth = .2f;
  
  public void update(float height, float width,float depth){
    this.height = height;
    this.depth = depth;
    this.width = width;
    repaint();
  }
  //inner class for the myBox
  public class myBox{
    private BranchGroup boxBG;
    
    //this method creates the box
    public myBox(){
      boxBG = new BranchGroup();
      
      BoundingSphere bounds = 
          new BoundingSphere(new Point3d(0,0,0),100);
      
      Appearance app = new Appearance();
      Color3f objColor = new Color3f(.5f,.5f,.6f);
      Color3f black = new Color3f(0f,0f,0f);
      Color3f white = new Color3f(1f,1f,1f);
      app.setMaterial(new Material(objColor,black,objColor,white,100f));
      
      //rotation and translation
      Transform3D rotate = new Transform3D();
      Transform3D translate = new Transform3D();
      
      translate.set(new Vector3f(0.f,0.f,0.f));
      TransformGroup boxTr = new TransformGroup(translate);
      boxBG.addChild(boxTr);
      
      rotate.rotZ(Math.PI/2f);
      TransformGroup boxRo = new TransformGroup(rotate);
      Box stockCube = new Box(height,width,depth,app);
      boxRo.addChild(stockCube);
      boxTr.addChild(boxRo);
      
      //compile boxBG
      boxBG.compile();
    }
    
    public BranchGroup getboxBG(){
      return boxBG;
    }
  }
  //create the BranchGroup for Spin and translation for later use
  public BranchGroup createSceneGraph(){
    BranchGroup objRoot = new BranchGroup();
    
    //setBoundingSphere
    BoundingSphere bounds = new BoundingSphere(new Point3d(0,0,0),100);
    //setting the background color
    Color3f bgColor = new Color3f(.05f,.05f,.5f);
    Background bgNode = new Background(bgColor);
    bgNode.setApplicationBounds(bounds);
    objRoot.addChild(bgNode);
    
     TransformGroup mouseMove = new TransformGroup();
      mouseMove.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
      mouseMove.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
      objRoot.addChild(mouseMove);
      mouseMove.addChild(new myBox().getboxBG());
      
      MouseRotate mr = new MouseRotate(mouseMove);
      mr.setSchedulingBounds(bounds);
      mouseMove.addChild(mr);
      
      MouseZoom mz = new MouseZoom(mouseMove);
      mouseMove.addChild(mz);
      mz.setSchedulingBounds(bounds);
      
      MouseTranslate mt = new MouseTranslate(mouseMove);
      mouseMove.addChild(mt);
      mt.setSchedulingBounds(bounds);
      
      //set up ambient light
      Color3f ambientColor = new Color3f(.1f,.1f,.1f);
      AmbientLight ambientLightNode = new AmbientLight(ambientColor);
      ambientLightNode.setInfluencingBounds(bounds);
      objRoot.addChild(ambientLightNode);
      
      //set up directional Lights
      Color3f light1Color = new Color3f(1f,1f,.9f);
      Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
      Color3f light2Color = new Color3f(0.3f, 0.3f, 0.4f);
      Vector3f light2Direction  = new Vector3f(-6.0f, -2.0f, -1.0f);
      
      DirectionalLight light1 = new DirectionalLight(light1Color,light1Direction);
      light1.setInfluencingBounds(bounds);
      objRoot.addChild(light1);
      
       DirectionalLight light2 = new DirectionalLight(light2Color,light2Direction);
      light2.setInfluencingBounds(bounds);
      objRoot.addChild(light2);
    //TransformGroup objTran = new TransformGroup();
    //objRoot.addChild(new myBox().getboxBG());
    return objRoot;
  }
  
  public my3DBoxApp(){
    setLayout(new BorderLayout());
    Canvas3D c = new Canvas3D(null);
    add("Center",c);
    
    BranchGroup s= createSceneGraph();
    
    SimpleUniverse sU = new SimpleUniverse(c);
    sU.getViewingPlatform().setNominalViewingTransform();
    sU.addBranchGraph(s);
  }
  
  public static void main(String agrs[]){
    Frame f = new MainFrame(new my3DBoxApp(),300,300);
  }
}