import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import java.awt.event.WindowAdapter;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.behaviors.mouse.*;
import com.sun.j3d.utils.geometry.*;


public class test39 extends Applet implements ActionListener {
    
    public TransformGroup objTrans;
    Transform3D trans = new Transform3D();
    
    JButton changeB = new JButton("change appearance");
  
  
  // Ensure to set the primflag ENABLE_APPEARANCE_MODIFY
    Sphere mySphere = new Sphere(0.5f,Sphere.GENERATE_NORMALS|Sphere.ENABLE_APPEARANCE_MODIFY,30);
  //
    
    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.
    // set the capability bits for reading and writing  material, and its components
    // (the appearance capability bits have already been set in the Sphere constructor)
     Appearance sphereApp = new Appearance();
     sphereApp.setCapability(Appearance.ALLOW_MATERIAL_READ);
     sphereApp.setCapability(Appearance.ALLOW_MATERIAL_WRITE);
     Color3f eColor    = new Color3f(0.4f, 0.3f, 0.3f);
     Color3f sColor    = new Color3f(0.0f, 0.5f, 0.5f);
     Color3f objColor  = new Color3f(0.4f, 0.2f, 0.6f);
     Material m = new Material(objColor, eColor, objColor, sColor, 30.0f);
     m.setCapability(Material.ALLOW_COMPONENT_READ);
     m.setCapability(Material.ALLOW_COMPONENT_WRITE);
     m.setLightingEnable(true);
     sphereApp.setMaterial(m);

     mySphere.setAppearance(sphereApp);
     objTrans.addChild(mySphere);

    BoundingSphere bounds =
      new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
      // Create the rotate behavior node
      MouseRotate behavior = new MouseRotate();
      behavior.setTransformGroup(objTrans);
      objTrans.addChild(behavior);
      behavior.setSchedulingBounds(bounds);

      // Create the zoom behavior node
      MouseZoom behavior2 = new MouseZoom();
      behavior2.setTransformGroup(objTrans);
      objTrans.addChild(behavior2);
      behavior2.setSchedulingBounds(bounds);

      // Create the translate behavior node
      MouseTranslate behavior3 = new MouseTranslate();
      behavior3.setTransformGroup(objTrans);
      objTrans.addChild(behavior3);
      behavior3.setSchedulingBounds(bounds);
      
    //Shine it with two lights.
    Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
    Color3f lColor2 = new Color3f(0.2f, 0.2f, 0.1f);
    Vector3f lDir1  = new Vector3f(-1.0f, -1.0f, -1.0f);
    Vector3f lDir2  = new Vector3f(0.0f, 0.0f, -1.0f);
    DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
    DirectionalLight lgt2 = new DirectionalLight(lColor2, lDir2);
    lgt1.setInfluencingBounds(bounds);
    lgt2.setInfluencingBounds(bounds);
    objTrans.addChild(lgt1);
    objTrans.addChild(lgt2);
    return objRoot;
    }

    public test39() {
    setLayout(new BorderLayout());
    Canvas3D c = new Canvas3D(null);
    add("Center", c);
    
    Panel topPanel =new Panel();
    topPanel.add(changeB);
    add("North",topPanel);
    
    changeB.addActionListener(this);  

    // Create a simple scene and attach it to the virtual universe
    BranchGroup scene = createSceneGraph();
    scene.setCapability( BranchGroup.ALLOW_BOUNDS_READ );
    SimpleUniverse 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 actionPerformed(ActionEvent e) {
        if (e.getSource()==changeB){
           /// code goes here
           Appearance tmpApp = mySphere.getAppearance();
           Material tmpMat = tmpApp.getMaterial();
           Color3f tmpsC = new Color3f();
           tmpMat.getSpecularColor(tmpsC);
           tmpsC.x=tmpsC.x+0.1f;
           tmpMat.setSpecularColor(tmpsC);
           System.out.println(tmpsC);

        }
    }


    //
    // The following allows HelloUniverse to be run as an application
    // as well as an applet
    //
    public static void main(String[] args) {
    new MainFrame(new test39(), 500, 500);
    }
}
