import com.sun.j3d.utils.behaviors.picking.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.image.TextureLoader;
import javax.media.j3d.*;
import java.awt.*;
import javax.vecmath.*;

public class PickHighlightBehavior extends PickMouseBehavior {
   Appearance savedAppearance;
   Shape3D oldShape;
   Appearance highlightAppearance;
   
   public PickHighlightBehavior(Canvas3D canvas, BranchGroup root,
			       Bounds bounds) {
      super(canvas, root, bounds);
      this.setSchedulingBounds(bounds);
      root.addChild(this);

      highlightAppearance = new Appearance();
	  ColoringAttributes highlightColoring = new ColoringAttributes();
	  highlightColoring.setColor(new Color3f(1.0f,1.0f,0.8f));
	  highlightAppearance.setColoringAttributes(highlightColoring);

	  oldShape = null;
	  savedAppearance = null;
   }

   public void updateScene(int xpos, int ypos) {
      Shape3D thisShape;

	if (!mevent.isMetaDown()) return;

      thisShape = (Shape3D) pickScene.pickNode(pickScene.pickClosest(xpos, ypos),
						 PickObject.SHAPE3D);

      if (oldShape != null){
         oldShape.setAppearance(savedAppearance);
      }
      if (thisShape != null) {
         savedAppearance = thisShape.getAppearance();
         oldShape = thisShape;
         thisShape.setAppearance(highlightAppearance);
		 Object userData = (Object) thisShape.getUserData();
		 System.out.println("Picked object of "+userData.getClass());
      }
   }
}
