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]
# 1

Hi

You could possibly help me. I've written a Java3D program displaying a cylinder shape and implemented MouseListener, how do I go about connecting the two together? I notice from your example code segment you use PickCanvas and PickResult.

Please let me know.

Andrew

null

Andrew310779a at 2007-7-10 0:56:14 > top of Java-index,Security,Cryptography...
# 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 > top of Java-index,Security,Cryptography...
# 3
Andrew310779 can you further explain what you want to do?You want to write a custom picking class to pick the cylinder or ...?
Wicea at 2007-7-10 0:56:14 > top of Java-index,Security,Cryptography...
# 4

I'm currently writing a small Java 3D program that displays a cylinder shape in the virtual universe. To the program I've also added MouseListener interface. What I am wanting to do is to detect when the user has clicked on the cylinder shape - the problem I am faced with is the co-ordinate system for mouse is different then co-ord system to position cylinder shape.

I think I'm needing to read up an area of Java3D and / or 3D programming but not sure.

Please advise.

Andrew

Andrew310779a at 2007-7-10 0:56:15 > top of Java-index,Security,Cryptography...
# 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 > top of Java-index,Security,Cryptography...