I am trying to implement a feature when the user clicks a specified shape (trigger) on the screen that an event is triggered. The following code is a class that I have written that implements mouse picking.

class Picker extends PickMouseBehavior
{
    private PickCanvas pickCanvas;
    private Node trigger;
    private Node shark1;
    private Node shark2;

    public Picker( Canvas3D canvas , BranchGroup root , Bounds bounds ,
                   Node triggerShape , Node sh1 , Node sh2 )
    {
        super( canvas , root , bounds );
        pickCanvas = new PickCanvas( canvas , root );
        trigger = triggerShape;
        shark1 = sh1;
        shark2 = sh2;
print("Created instance of Picker class.");
    }

    private void print( String s )
    {
       System.out.println("@PICKER: " + s );
    }

    public void updateScene(int xpos, int ypos)
    {
print("updateScene() BEGIN;");
                PickResult pickResult = null;		
		pickCanvas.setShapeLocation(xpos, ypos);
		pickResult = pickCanvas.pickClosest();
                Shape3D shape = null;

		if (pickResult != null) 
                {
                    if ( pickResult != null )
                    {
                       Node pickedShape = (Primitive) pickResult.getNode(PickResult.PRIMITIVE);
                       String pickedData = (String) pickedShape.getUserData();
                       if ( pickedData.equals( trigger.getUserData() ) )
                       {
                           print("Triggering..");
                       }
                       else
                       {
                           print("Incorrect Shape");
                       }
                    }
		} 
                else 
                {
                    print("No shape picked");
		}
    }
}

In my main code I am instantiating this class and adding it into the scene graph like so..

Picker picker = new Picker( canvas3D , objRoot , bounds , island , shark1 , shark2 );                                    
         objRoot.addChild( picker );         
         objRoot.compile();

I have found that the class is instantiated correctly because the output messages in Picker's constructor are working fine.

My problem is that it simply does not work, clicking anywhere in the screen does not call updateScene() whatsoever. Am I doing something wrong? I'm quite new to Java3D and especially picking.

Thanks,

- Jonathon.

This article has been dead for over six months. Start a new discussion instead.