Can anyone recommend an approach for a behavior that changes the cursor to a hand while the cursor is over a pickable object in the scene?  E.G. My scene consists of a room containing objects such as a desk, file cabinet, computer and chair.  Some of the objects, like the file cabinet, can be selected.  I'd like the cursor to change when it is over the file cabinet and return to the default cursor when not over the file cabinet.

I figure that I must have to add the behavior to the branch group containing the pickable shapes, but I don't know how to set the bounds.  I want the cursor to change regardless of how far away I am from the object, as long as it is visible.

Here is the code I used for my behavior:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.JOptionPane;
import javax.media.j3d.Behavior;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Shape3D;
import javax.media.j3d.WakeupCriterion;
import javax.media.j3d.WakeupOnAWTEvent;
import javax.media.j3d.WakeupOr;
import com.sun.j3d.utils.picking.PickCanvas;
import com.sun.j3d.utils.picking.PickResult;
import com.sun.j3d.utils.picking.PickTool;

public class MouseOverBehavior extends Behavior {
    private WakeupOr mouseCriterion;
    private WakeupCriterion[] mouseEvents;
    Canvas3D canvas;

    public MouseOverBehavior(Canvas3D canvas) {
        super();
        this.canvas = canvas;
    }

    public void initialize() {
        mouseEvents = new WakeupCriterion[2];
     mouseEvents[0] = new WakeupOnAWTEvent(MouseEvent.MOUSE_ENTERED);
     mouseEvents[1] = new WakeupOnAWTEvent(MouseEvent.MOUSE_EXITED);

        mouseCriterion = new WakeupOr(mouseEvents);
        wakeupOn (mouseCriterion);
    }
 
    public void processStimulus(Enumeration criteria) {
        WakeupCriterion wakeup;
     AWTEvent[] events;
      MouseEvent evt;
     while (criteria.hasMoreElements()) {
         wakeup = (WakeupCriterion) criteria.nextElement();
         if (wakeup instanceof WakeupOnAWTEvent) {
          events = ((WakeupOnAWTEvent)wakeup).getAWTEvent();
          if (events.length > 0) {
              evt = (MouseEvent) events[events.length-1];
              processMouseEvent(evt);
          }
         }
     }
        this.wakeupOn(mouseCriterion);
    }

    /**
    * Handles mouse events
    */
    public void processMouseEvent(MouseEvent evt) {
        if (evt.getID()==MouseEvent.MOUSE_ENTERED) {
            canvas.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        }
        else if (evt.getID()==MouseEvent.MOUSE_EXITED){
            canvas.setCursor(Cursor.getDefaultCursor());
        }
    }
 
}

Any ideas?
Thanks,
Lisa Strader

Reply via email to