import java.applet.Applet;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.WindowAdapter;
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 Cube3 extends Applet implements ActionListener {
       	
    TransformGroup objTrans,aux;
    float step=(float)Math.toRadians(10.0);
    double x,y,z;
    Transform3D t = new Transform3D();
    Transform3D auxt = new Transform3D();
    Transform3D transX = new Transform3D();
    Transform3D transY = new Transform3D();
    Transform3D transZ = new Transform3D();

    JButton rotateX = new JButton("X");
    JButton rotateY = new JButton("Y");
    JButton rotateZ = new JButton("Z");
    JButton moveL = new JButton("Left");
    JButton moveR = new JButton("Right");
    JButton moveU = new JButton("Up");
    JButton moveD = new JButton("Down");
    JButton moveB = new JButton("Back");
    JButton moveF = new JButton("Forth");
    JButton reset = new JButton("Reset");
    JButton exit = new JButton("Exit");
        
    private SimpleUniverse u = null;

    
    /******************* method ***********************/
    public BranchGroup createSceneGraph() {
	
	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.
	objTrans = new TransformGroup();
	objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
	objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
	objRoot.addChild(objTrans);

    aux= new TransformGroup();
    aux.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
	aux.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    objRoot.addChild(aux);
    
	// Create a simple shape leaf node, add it to the scene graph.
	objTrans.addChild(new ColorCube(0.05));
    aux.addChild(new ColorCube(0.05));
	
	
	
	return objRoot;
    }

    

    public void init() {
	setLayout(new BorderLayout());
    GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
    Canvas3D c = new Canvas3D(config);
	add("Center", c);

	JPanel p = new JPanel();
	rotateX.addActionListener(this);
    rotateY.addActionListener(this);
    rotateZ.addActionListener(this);
    moveL.addActionListener(this); 
    moveR.addActionListener(this);
    moveU.addActionListener(this);
    moveD.addActionListener(this);
    moveB.addActionListener(this);
    moveF.addActionListener(this);
    reset.addActionListener(this);
    exit.addActionListener(this);
    p.add(rotateX);
	p.add(rotateY);
	p.add(rotateZ);
	p.add(moveL);
	p.add(moveR);
	p.add(moveU);
	p.add(moveD);
	p.add(moveB);
	p.add(moveF);
	p.add(reset);
	p.add(exit);
	add("North", p);

	// Create a simple scene and attach it to the virtual universe
	BranchGroup scene = createSceneGraph();
	scene.setCapability( BranchGroup.ALLOW_BOUNDS_READ );
	u = new SimpleUniverse(c);

    // This will move the ViewPlatform back a bit so the
    // objects in the scene can be viewed.
    u.getViewingPlatform().setNominalViewingTransform();

	u.addBranchGraph(scene);
    
    x=0.2;
    y=0;
    z=0;
   
   	auxt.setTranslation(new Vector3d(0.2,0.0,0.0));
    aux.setTransform(auxt);
    }

    public void destroy() {
	u.removeAllLocales();
    }
    
    /****reponse to key ********/
    public void actionPerformed(ActionEvent event){
    
    
    
    
    if(event.getSource().equals(rotateX)){
    transX.rotX(step);
	t.mul(transX);
}
    
    if(event.getSource().equals(rotateY)){
    transY.rotY(step);
	t.mul(transY);
	
}

    if(event.getSource().equals(rotateZ)){
    transZ.rotZ(step);
	t.mul(transZ);
}   
    
    if(event.getSource().equals(moveF)){
    z=z+0.01;
	auxt.setTranslation(new Vector3d(x,0.0,z));
}   
    
    
    
    if(event.getSource().equals(reset)){
    t.setIdentity();
	
	
}
    
    
    if(event.getSource().equals(exit)){
    System.exit(0);
	
}  
    aux.setTransform(auxt);
    objTrans.setTransform(t);
    
    
    }
    
    // The following allows HelloUniverse to be run as an application
    // as well as an applet
    //
    public static void main(String[] args) {
	new MainFrame(new Cube3(), 750, 500);
    }
}
