I recently borrowed some PickMouseBehavior code from Palmer's book, which seems to work pretty well.
My simple test scene graph consists of a BranchGroup containing three TransformGroups. Each TransformGroup contains one Shape3D object. The scene graph diagram is:
(BG)
/ | \
(TG1)(TG2)(TG3)
| | |
(S1) (S2) (S3)
The PickMouseBehavior I'm using does a fine job of picking the Shape3D Leaf Nodes. But the information I need is actually the Leaf's parent, which is a TransformGroup. But when I try to determine the TransformGroup from the PickResult, I only get a null pointer for it. I do not get an error message.
Ultimately, what I need to do is get the indexOfChild for the TransformGroup so that I can temporarily replace it with a different TransformGroup and child). To do that, I need the index of the TG within the BranchGroup. For some reason, my behavior doesn't identify the TransformGroup.
Can someone please help me with this?
Here is the PickMouseBehavior that I'm using:
----------------------------------------------------------------------
----------------------------------------------------------------------
public class pMouseBehavior extends PickMouseBehavior {
private BranchGroup pickRoot;
WakeupOnAWTEvent buttonPressed =
new WakeupOnAWTEvent(MouseEvent.MOUSE_EVENT_MASK);
public pMouseBehavior(Canvas3D pickCanvas, BranchGroup pickRoot,
Bounds pickBounds) {
super(pickCanvas, pickRoot, pickBounds);
setSchedulingBounds(pickBounds);
}
public void initialize() {
wakeupOn(buttonPressed);
}
public void processStimulus(Enumeration criteria) {
WakeupOnAWTEvent theCriterion = (WakeupOnAWTEvent) criteria.nextElement();
AWTEvent theEvents[] = theCriterion.getAWTEvent();
if (theEvents[0].getID() == MouseEvent.MOUSE_RELEASED) {
MouseEvent theMouseEvent = (MouseEvent) theEvents[0];
if((theMouseEvent.getModifiers() & MouseEvent.BUTTON1_MASK) != 0) {
int xpos = theMouseEvent.getX();
int ypos = theMouseEvent.getY();
Shape3D pickedShape = null;
TransformGroup pickedTransform = null;
PickResult pickResult = null;
pickCanvas.setShapeLocation(xpos,ypos);
pickResult = pickCanvas.pickClosest();
if(pickResult != null) pickedShape = (Shape3D)
pickResult.getNode(PickResult.SHAPE3D);
if(pickedShape != null){
System.out.println("Picked the " + pickedShape.getUserData());
pickedTransform = (TransformGroup)
pickResult.getNode(PickResult.TRANSFORM_GROUP);
if(pickedTransform != null)
System.out.println("picked a transform");
else
System.out.println("transform not picked");
}
else
System.out.println("Nothing picked");
}
}
wakeupOn(buttonPressed);
}
public void updateScene(int xpos, int ypos) {}
} \\ end of class pMouseBehavior
-------------
Paul Drallos - 28 Jun 2005 20:59 GMT
> ....
> The PickMouseBehavior I'm using does a fine job of picking the Shape3D
[quoted text clipped - 3 lines]
> do not get an error message.
> ...
Man, I'm good! Only half an hour after I posted this question, I figured out the answer: I needed to set the capability to ENABLE_PICK_REPORTING on the TransformGroups in question. Since I wasn't getting a 'capability not set' error, I didn't think that was a problem. But I tried it anyway and it works!
Thanks for your patience.
Paul