Exercise 9: Picking a Shape

This Lab involves writing an application that displays 3d shapes. When one of these shapes is clicked on the program will determine which shape, and print it out.

Lab 9 ScreenShot

Without the utilities


   private BranchGroup pickBG = new BranchGroup();
   private Board3D board = ???


   board.addMouseListener(new MouseHandler());


   private void mouseClick(int x, int y)
       Point3d eye_pos = new Point3d();
       Point3d mouse_pos = new Point3d();

       board.getPixelLocationInImagePlate(x, y, mouse_pos);

       Transform3D motion = new Transform3D();

       Vector3d direction = new Vector3d(mouse_pos);

       PickRay pickRay = new PickRay(eye_pos, direction);

       SceneGraphPath[] path = pickBG.pickAll(pickRay);



   private class MouseHandler extends MouseAdapter
       public void mousePressed(MouseEvent e)
           int x = e.getX();
           int y = e.getY();

           mouseClick(x, y);

With the utilities

G.3.21 PickTest

Directory: demo/java3d/PickTest
The PickTest program displays several 3D objects and a control panel. The control panel allows the user to change the pick mode, the pick tolerance, and the view mode. You can pick and rotate objects with the mouse. The PickTest program demonstrates the use of the PickMouseBehavior utility classes.
The IntersectTest program demonstrates the ability to get geometric intersection information from a picked object. Use the mouse to pick a point on one of the objects in the window. The program illuminates the picked location and the vertices of the primitive with tiny spheres. Information about the picked primitive and the point of intersection is printed. The IntersectTest program uses a mouse-based behavior, IntersectInfoBehavior, to control the picking. The relevant source code fragments from IntersectInfoBehavior.java follow:

PickCanvas pickCanvas;
PickResult[] pickResult;
public IntersectInfoBehavior(Canvas3D canvas3D,
BranchGroup branchGroup,
float size) {
pickCanvas = new PickCanvas(canvas3D, branchGroup);
The IntersectInfoBehavior class constructor creates a new PickCanvas object, initializes the PickCanvas with the desired tolerance, and sets the mode to allow geometry intersection information to be retrieved.


public void processStimulus(Enumeration criteria) {
<check for mouse event> if (eventId == MouseEvent.MOUSE_PRESSED) {
int x = ((MouseEvent)event[i]).getX();
int y = ((MouseEvent)event[i]).getY();
pickCanvas.setShapeLocation(x, y);
Point3d eyePos = pickCanvas.getStartPosition();
pickResult = pickCanvas.pickAllSorted();
if (pickResult != null) {
// Get node info
Node curNode = pickResult[0].getObject();
Geometry curGeom = ((Shape3D)curNode).getGeometry();
GeometryArray curGeomArray = (GeometryArray) curGeom;
// Get closest intersection results
PickIntersection pi =
<get specific info from PickIntersection>