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;
import com.sun.j3d.utils.picking.PickTool;
import com.sun.j3d.utils.picking.PickResult;
import com.sun.j3d.utils.picking.behaviors.PickMouseBehavior;
import java.util.*;
import java.awt.*;
import java.awt.Event;
import java.awt.AWTEvent;
import java.awt.event.MouseEvent;

public class SimpleLine extends Applet
 {
public static Box shape1;
public static Cylinder shape2;
public static Cone shape3;
public static Sphere shape4;
public static Sphere shape5;

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);

PickTranslateBehavior pickTranslate= null;
pickTranslate = new PickTranslateBehavior(objRoot, canvas, bounds);
objRoot.addChild(pickTranslate);

PickZoomBehavior pickZoom= null;
pickZoom = new PickZoomBehavior(objRoot, canvas, bounds);
objRoot.addChild(pickZoom);


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 targetTG = new TransformGroup();
Transform3D tt=new Transform3D();
targetTG.setTransform(tt);
targetTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
targetTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
targetTG.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
targetTG.setCapability(TransformGroup.ALLOW_CHILDREN_READ);
targetTG.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);
targetTG.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
objRoot.addChild(targetTG);

TransformGroup ot1 = new TransformGroup();
Transform3D t1 = new Transform3D();
t1.setTranslation(new Vector3f(0.3F,0.0F,0.0F));
ot1.setTransform(t1);
shape1 = new Box(0.05f,0.05f,0.08f,null);
targetTG.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();
t2.setTranslation(new Vector3f(-0.3F,0.2F,0.0F));
ot2.setTransform(t2);
shape2 = new Cylinder(0.05f,0.05f,null);
targetTG.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);

TransformGroup ot3 = new TransformGroup();
Transform3D t3 = new Transform3D();
t3.setTranslation(new Vector3f(0.5F,0.3F,0.3F));
ot3.setTransform(t3);
shape3 = new Cone(0.1f,0.2f,null);
targetTG.addChild(ot3);
shape3.setCapability(Shape3D.ALLOW_BOUNDS_READ);
shape3.setCapability(Shape3D.ALLOW_BOUNDS_WRITE);
shape3.setCapability(Shape3D.ALLOW_LOCAL_TO_VWORLD_READ);
ot3.addChild(shape3);

TransformGroup ot4 = new TransformGroup();
Transform3D t4 = new Transform3D();
t4.setTranslation(new Vector3f(-0.2F,-0.4F,0.2F));
ot4.setTransform(t4);
shape4 = new Sphere(0.06f,null);
targetTG.addChild(ot4);
shape4.setCapability(Shape3D.ALLOW_BOUNDS_READ);
shape4.setCapability(Shape3D.ALLOW_BOUNDS_WRITE);
shape4.setCapability(Shape3D.ALLOW_LOCAL_TO_VWORLD_READ);
ot4.addChild(shape4);

TransformGroup ot5 = new TransformGroup();
Transform3D t5 = new Transform3D();
t5.setTranslation(new Vector3f(-0.6F,0.5F,0.0F));
ot5.setTransform(t5);
shape5 = new Sphere(0.08f,null);
targetTG.addChild(ot5);
shape5.setCapability(Shape3D.ALLOW_BOUNDS_READ);
shape5.setCapability(Shape3D.ALLOW_BOUNDS_WRITE);
shape5.setCapability(Shape3D.ALLOW_LOCAL_TO_VWORLD_READ);
ot5.addChild(shape5);

slbehavior b1=new slbehavior(targetTG,canvas,objRoot);
b1.setSchedulingBounds(bounds);
targetTG.addChild(b1);  

objRoot.compile();
return objRoot;
      }

public SimpleLine() 
{
    setLayout(new BorderLayout());
    GraphicsConfiguration config;
    config = SimpleUniverse.getPreferredConfiguration();
    Canvas3D c = new Canvas3D(config);
    add("Center",c);
    BranchGroup 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 );

Bounds q3 = shape3.getBounds();
Point3d center3=null; 
if (q3 instanceof BoundingSphere)
 {
 center3 = new Point3d();
 ((BoundingSphere)q3).getCenter(center3); 
}
System.out.println(" Center3 is"+ center3);
boolean z3=shape3.isLive();
System.out.println(" z3 is"+ z3);
Transform3D xform3=new Transform3D();
shape3.getLocalToVworld(xform3);
xform3.transform(center3);
System.out.println(" Updated Center3 is"+ center3);
shape3.setUserData( center3 );

Bounds q4 = shape4.getBounds();
Point3d center4=null; 
if (q4 instanceof BoundingSphere)
 {
 center4 = new Point3d();
 ((BoundingSphere)q4).getCenter(center4); 
}
System.out.println(" Center4 is"+ center4);
boolean z4=shape4.isLive();
System.out.println(" z4 is"+ z4);
Transform3D xform4=new Transform3D();
shape4.getLocalToVworld(xform4);
xform4.transform(center4);
System.out.println(" Updated Center4 is"+ center4);
shape4.setUserData( center4 );

Bounds q5 = shape5.getBounds();
Point3d center5=null; 
if (q5 instanceof BoundingSphere)
 {
 center5 = new Point3d();
 ((BoundingSphere)q5).getCenter(center5); 
}
System.out.println(" Center5 is"+ center5);
boolean z5=shape5.isLive();
System.out.println(" z5 is"+ z5);
Transform3D xform5=new Transform3D();
shape5.getLocalToVworld(xform5);
xform5.transform(center5);
System.out.println(" Updated Center5 is"+ center5);
shape5.setUserData( center5 );
 }

  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;
public static int count=0;
public static Point3d cg[]=new Point3d[2];

slbehavior (TransformGroup targetTG,Canvas3D canvas, BranchGroup scene)
{
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)

{
PickShape shape=null; 
PickObject obj = new PickObject(canvas,scene);
if ( obj != null ) 
{ 
AWTEvent myEvent[] = trigger.getAWTEvent(); 
int xpos = 0;
int ypos = 0; 
MouseEvent event1=null; 
try
{
event1 = (MouseEvent) myEvent[0]; 
}
catch (ArrayIndexOutOfBoundsException a){}
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 ) 
{
 Primitive myShape=null;
 
// get the first node in the path
 try
{
Node node = closest.getNode(1); 
int nodeCount = closest.nodeCount();
System.out.println(nodeCount);

 // Identify your object
 if ( node instanceof Box) 
{
 System.out.println("Touched a box!");
 } 
if ( node instanceof Cylinder)
 {
 System.out.println("Touched a cylinder");
 }
if ( node instanceof Cone) 
{
 System.out.println("Touched a Cone!");
 } 
if ( node instanceof Sphere)
 {
 System.out.println("Touched a Sphere");
 }

if (node instanceof Primitive)
{
myShape = (Primitive) node;
}
BranchGroup bb=new BranchGroup();
bb.setCapability(BranchGroup.ALLOW_DETACH);

if ( myShape!=null && event1.getClickCount()==1)
{
System.out.println(count);
cg[count++] = (Point3d)myShape.getUserData();
if (count==2)
{
LineArray la=new LineArray(2,LineArray.COORDINATES); 
la.setCoordinates(0,cg);
Shape3D shapex = new Shape3D(la);
shapex.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
PickTool.setCapabilities(shapex, PickTool.INTERSECT_FULL);
bb.addChild(shapex);
targetTG.addChild(bb);
count=0;
}
else
{
System.out.println("less than 2");
}
}

else if ( myShape!=null && event1.getClickCount()==2)
{
bb.detach();
System.out.println("XYZ");
}
 else
{
System.out.println("No Primitive is being Picked Up");
}
}
catch (ArrayIndexOutOfBoundsException a){System.out.println("Exception Loop...");
}
}
else 
 { 
System.out.println("scenegraph path is null...");
 }
}
this.wakeupOn(trigger); 

}
}