PickCanvas
Hi,
Does anyone know how to retrieve the name of an object or the TransformGroup it is originating form. With the help of an example i whas able to retrieve the Class and Name of the type.
Declaration
pickCanvas =new PickCanvas(canvas3d, scene);
pickCanvas.setMode(PickCanvas.BOUNDS);
canvas3d.addMouseListener(this);
Use with mouseClicked
publicvoid mouseClicked(MouseEvent e){
pickCanvas.setShapeLocation(e);
PickResult result = pickCanvas.pickClosest();
if (result ==null){
System.out.println("Nothing picked");
}else{
Primitive p = (Primitive)result.getNode(PickResult.PRIMITIVE);
Shape3D s = (Shape3D)result.getNode(PickResult.SHAPE3D);
if (p !=null){
System.out.println(p.getClass().getName());
}elseif (s !=null){
System.out.println(s.getClass().getName());
}else{
System.out.println("null");
}
}
}
[1755 byte] By [
DaMicka] at [2007-11-26 20:28:24]

# 2
Hi DaMick, try this:
public void mouseClicked(MouseEvent e){
pickCanvas.setShapeLocation(e);
PickResult result = pickCanvas.pickClosest();
if (result == null) {
System.out.println("Nothing picked");
} else {
Primitive p = (Primitive)result.getNode(PickResult.PRIMITIVE);
Shape3D s = (Shape3D)result.getNode(PickResult.SHAPE3D);
if (p != null) {
System.out.println(p.getParent().getClass().getName());
} else if (s != null) {
System.out.println(s.getParent().getClass().getName());
} else{
System.out.println("null");
}
}
}
I hope it helps.
Wice
Message was edited by:
Wice
Wicea at 2007-7-10 0:56:14 >

# 5
Hi
the easyest way to do this is to write a custom PickMouseBehavior:
public class pickMouseBehavior extends PickMouseBehavior{
public pickMouseBehavior(Canvas3D canvas, BranchGroup bg, Bounds bounds){
super(canvas, bg, bounds);
this.setSchedulingBounds(bounds);
pickCanvas.setMode(PickTool.GEOMETRY);
}
public void initialize(){
super.initialize();
this.wakeupOn(new WakeupOnAWTEvent(MouseEvent.MOUSE_CLICKED));
super.wakeupOn(new WakeupOnAWTEvent(MouseEvent.MOUSE_CLICKED));
}
public void processStimulus(Enumeration e){
WakeupCriterion wokeup = (WakeupCriterion)e.nextElement();
AWTEvent event[] = ((WakeupOnAWTEvent)wokeup).getAWTEvent();
MouseEvent mEvent = (MouseEvent)event[0];
pickCanvas.setShapeLocation(mEvent);
PickResult pickResult = pickCanvas.pickClosest();
if (pickResult != null){
shape = (Shape3D) pickResult.getNode(PickResult.SHAPE3D);
// Do what you want to do with the picked Shape3D
}
this.wakeupOn(new WakeupOnAWTEvent(MouseEvent.MOUSE_CLICKED));
super.wakeupOn(new WakeupOnAWTEvent(MouseEvent.MOUSE_CLICKED));
}
public void updateScene(int xpos, int ypos){
}
}
And in the branchgroup you should add:
pickMouseBehavior p = new pickMouseBehavior(simpleU.getCanvas(), objRoot, bounds);
objRoot.addChild(p);
There could be some small errors in this code because i just did copy-paste from my own code, but this is the main idea for picking.
Hope this helps.
Wice
Wicea at 2007-7-10 0:56:15 >
