import java.applet.Applet;
import java.awt.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import com.sun.j3d.utils.picking.behaviors.*;
import com.sun.j3d.utils.picking.*;
import java.awt.event.*;
import java.util.Enumeration;
import javax.media.j3d.GeometryStripArray.*;
import java.awt.event.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.behaviors.picking.PickObject;

public class SimpleLine extends Applet
 {
public static Box shape1;
public static Cylinder shape2;
public BranchGroup createSceneGraph(Canvas3D canvas)
 {    
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),150.0);
BranchGroup objRoot = new BranchGroup();

//PickRotateBehavior pickRotate= null;
//pickRotate = new PickRotateBehavior(objRoot, canvas, bounds);
//objRoot.addChild(pickRotate);

Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
AmbientLight ambientLightNode = new AmbientLight(ambientColor);
ambientLightNode.setInfluencingBounds(bounds);
objRoot.addChild(ambientLightNode);

// Set up the directional lights

Color3f light1Color = new Color3f(1.0f, 1.0f, 0.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 ot1 = new TransformGroup();
Transform3D t1 = new Transform3D();
ot1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
ot1.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
ot1.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
t1.setTranslation(new Vector3f(0.5F,0.0F,0.0F));
ot1.setTransform(t1);
shape1 = new Box(0.15f,0.5f,0.08f,null);
objRoot.addChild(ot1);
shape1.setCapability(Shape3D.ALLOW_BOUNDS_READ);
shape1.setCapability(Shape3D.ALLOW_BOUNDS_WRITE);
shape1.setCapability(Shape3D.ALLOW_LOCAL_TO_VWORLD_READ);
ot1.addChild(shape1);


TransformGroup ot2 = new TransformGroup();
Transform3D t2 = new Transform3D();
ot2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
ot2.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
ot2.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
t2.setTranslation(new Vector3f(-0.6F,0.0F,0.0F));
ot2.setTransform(t2);
shape2 = new Cylinder(0.15f,0.5f,null);
objRoot.addChild(ot2);
shape2.setCapability(Shape3D.ALLOW_BOUNDS_READ);
shape2.setCapability(Shape3D.ALLOW_BOUNDS_WRITE);
shape2.setCapability(Shape3D.ALLOW_LOCAL_TO_VWORLD_READ);
ot2.addChild(shape2);

slbehavior b1=new slbehavior(ot1,objRoot,canvas);
b1.setSchedulingBounds(bounds);
ot1.addChild(b1);     

slbehavior b2=new slbehavior(ot1,objRoot,canvas);
b2.setSchedulingBounds(bounds);
ot2.addChild(b2);     

objRoot.compile();
return objRoot;

      }

public static Canvas3D c=null;
public static BranchGroup scene=null;

public SimpleLine() 
{
    setLayout(new BorderLayout());
    GraphicsConfiguration config;
    config = SimpleUniverse.getPreferredConfiguration();
    c = new Canvas3D(config);
    add("Center",c);
    scene = createSceneGraph(c);
    SimpleUniverse u = new SimpleUniverse(c);
    u.getViewingPlatform().setNominalViewingTransform();
    u.addBranchGraph(scene);


Bounds q1 = shape1.getBounds();
Point3d center1=null; 
if (q1 instanceof BoundingSphere)
 {
 center1 = new Point3d();
 ((BoundingSphere)q1).getCenter(center1); 
}
System.out.println(" Center1 is"+ center1);
boolean z1=shape1.isLive();
System.out.println(" z1 is"+ z1);
Transform3D xform1=new Transform3D();
shape1.getLocalToVworld(xform1);
xform1.transform(center1);
System.out.println(" Updated Center1 is"+ center1);
shape1.setUserData( center1 );

Bounds q2 = shape2.getBounds();
Point3d center2=null;
 if (q2 instanceof BoundingSphere)
 {
   center2= new Point3d();
 ((BoundingSphere)q2).getCenter(center2); 
}
System.out.println(" Center2 is"+ center2);
boolean z2=shape2.isLive();
System.out.println(" z2 is"+ z2);
Transform3D xform2=new Transform3D();
shape2.getLocalToVworld(xform2);
xform2.transform(center2);
System.out.println(" Updated Center2 is"+ center2);
shape2.setUserData( center2 );
   
    }

  public static void main(String[] args) {
    new MainFrame(new SimpleLine(),450,250);
    }
  }

class slbehavior extends Behavior
{

private TransformGroup targetTG;
private BranchGroup scene;
private Canvas3D canvas;
static int count=0;

slbehavior (TransformGroup  targetTG, BranchGroup scene, Canvas3D canvas)
{
this.targetTG=targetTG;
this.scene=scene;
this.canvas=canvas;
}

public void initialize()
{
this.wakeupOn(new WakeupOnAWTEvent(MouseEvent.MOUSE_CLICKED));
}

WakeupOnAWTEvent trigger = new WakeupOnAWTEvent(MouseEvent.MOUSE_CLICKED); 

public void processStimulus (Enumeration criteria)

{ 
this.wakeupOn(trigger); 
Point3d cg[]=new Point3d[10];
 PickShape shape=null; 
PickObject obj = new PickObject(canvas,scene); 
if ( obj != null ) 
{ 
AWTEvent myEvent[] = trigger.getAWTEvent(); 
MouseEvent event1=null; 
event1 = (MouseEvent) myEvent[0]; 
int xpos = 0;
 int ypos = 0; 
if ( event1 != null ) 
{
 xpos = event1.getX(); 
ypos = event1.getY();
 // For debugging purposes 
System.out.println(xpos + " " + ypos); 
}
 SceneGraphPath closest = obj.pickClosest(xpos, ypos); 
if ( closest != null ) 
{
 Shape3D myShape=null;
 // get the first node in the path 
Node node = closest.getNode(1); 
int nodeCount = closest.nodeCount();
 System.out.println(nodeCount);
 

if ( node instanceof  Shape3D) 
{
myShape=(Shape3D) node;
}

 // Identify your object
 if ( node instanceof Box) 
{
 System.out.println("Touched a box!");
 } 
if ( node instanceof Cylinder)
 {
 System.out.println("Touched a cylinder");
 }
 
if ( myShape != null )
 {
 cg[count++] = (Point3d)myShape.getUserData();


 LineArray la=new LineArray(count+1,LineArray.COORDINATES); 
}
 else 
 
{ 
System.out.println("scenegraph path is null...");
 }
 } 
}
}
}


