Getting a reference to the main parent of a group - Java3D

190 Views Asked by At

I have the following code for capturing mouse clicks in Java3D. I am hoping to eventually be able to click on an object in my scene and move it around with the mouse.

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.getClass().getName());

       } else{

          System.out.println("null");

       }

    }

}// end of class Pick

The output of clicking on some of the objects is as follows:

Wall3D
Floor3D
Carpet3D
javax.media.j3d.TransformGroup
javax.media.j3d.SharedGroup
javax.media.j3d.TransformGroup

This is due to my Wall3D / Floor3D and Carpet3D being made up only of a single primitive, for example:

Carpet3D:

public class Carpet3D extends Group{

public Carpet3D() {

    this.createSceneGraph();

}

public void createSceneGraph() {
    Appearance appearance = getCarpetAppearance();
    int primitiveflags = Primitive.GENERATE_NORMALS + Primitive.GENERATE_TEXTURE_COORDS;
    Box floorShape = new Box(1.7f, .015f, 2.3f,primitiveflags,appearance);
    this.addChild(floorShape);
}

DinnerTable3D:

public class DinnerTable3D extends Group{

    public DinnerTable3D() {

        this.createSceneGraph();

    }

    public void createSceneGraph() {
        Appearance appearance = getWoodenAppearance();

        //Table TOP
        int primitiveflags = Primitive.GENERATE_NORMALS + Primitive.GENERATE_TEXTURE_COORDS;

        Box tableTop = new Box(1.2f, .035f, .8f,primitiveflags,appearance);
        TransformGroup tableTopGroup = new TransformGroup();
        tableTopGroup.addChild(tableTop);

        this.addChild(tableTopGroup);


        //Dinner Table Legs
        Box tableLeg = new Box(0.06f,.72f,0.06f, primitiveflags,appearance);
        SharedGroup dinnerTableLegs = new SharedGroup();
        dinnerTableLegs.addChild(tableLeg);

        //Leg 1
        this.addChild(Main.setPositionOfObject(new Vector3f(-1.14f,-0.755f,-.74f), dinnerTableLegs));
        //Leg 2
        this.addChild(Main.setPositionOfObject(new Vector3f(1.14f,-0.755f,.74f), dinnerTableLegs));
        //Leg 3
        this.addChild(Main.setPositionOfObject(new Vector3f(1.14f,-0.755f,-.74f), dinnerTableLegs));
        //Leg 4
        this.addChild(Main.setPositionOfObject(new Vector3f(-1.14f,-0.755f,.74f), dinnerTableLegs));
    }
}

Does anybody know how I can get a reference to the main group of any object I click?

Update:

I have this code which works but it seems extremely ugly. When an object with only one level is referenced, a null pointer is thrown for the higher levels...

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("One parent: " + p.getParent().getClass().getName());
           System.out.println("Two parent: " + p.getParent().getParent().getClass().getName());
           System.out.println("Three parent: " + p.getParent().getParent().getParent().getClass().getName());
}
0

There are 0 best solutions below