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 Cube4 extends Applet implements ActionListener {
       	
    TransformGroup objTrans;
    float step=(float)Math.toRadians(10.0);
    Transform3D t = 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 reset = new JButton("Reset");
    JButton exit = new JButton("Exit");
        
    private SimpleUniverse u = null;

    
    /******************* method ***********************/
    public BranchGroup createSceneGraph() {
	// Create the root of the branch graph
	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);

	// Create a simple shape leaf node, add it to the scene graph.
	objTrans.addChild(new ColorCube(0.1));

	// create the AWTInteractionBehavior	
	
	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);
    reset.addActionListener(this);
    exit.addActionListener(this);
    p.add(rotateX);
	p.add(rotateY);
	p.add(rotateZ);
	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);

    }

    public void destroy() {
	u.removeAllLocales();
    }
    
    /****reponse to key ********/
    public void actionPerformed(ActionEvent event){
    
    objTrans.getTransform(t);
	Matrix4d mat = new Matrix4d();
	t.get(mat);
    
    t.setTranslation(new Vector3d(0.0,0.0,0.0));
    
    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(reset)){
    t.setIdentity();
	
	
}
    
    
    if(event.getSource().equals(exit)){
    System.exit(0);
	
}  
    Vector3d translation = new Vector3d(mat.m03, mat.m13, mat.m23);
	t.setTranslation(translation);	
      
    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 Cube4(), 750, 500);
    }
}
