How to catch and handle events on the 3D canvas

prev toc next
You can download example source code for this HowTo here. This howto shows how to catch and handle mouse or key events: Where you set up the universe, call the method "setInteractiveBehavior(...)":
	univ = new Image3DUniverse();
	univ.show();
	Content c = univ.addCustomMesh(...);

	// this is necessary for catching the mouse events
	univ.setInteractiveBehavior(new CustomBehavior(univ, c));
Then create a new (maybe inner) class that extends ij3d.behaviors.InteractiveBehavior.
private class CustomBehavior extends InteractiveBehavior {

	private Content c;
	
	CustomBehavior(Image3DUniverse univ, Content c) {
		super(univ);
		this.c = c;
	}

	public void doProcess(MouseEvent e) {
		if(!e.isControlDown() ||
			e.getID() != MouseEvent.MOUSE_PRESSED) {
			super.doProcess(e);
			return;
		}
		// Get the point on the geometry where the mouse
		// press occurred
		Point3d p = univ.getPicker().
			getPickPointGeometry(c, e.getX(), e.getY());
		if(p == null)
			return;

		IJ.showMessage("Picked " + new Point3f(p));
	}
}
In the example shown above, mouse presses with hold-down control key are captured. The universe's Picker object is used to obtain the picked point (i.e. the point where the mouse ray intersects the object). In the example, this point is just displayed on the screen then. Important: Be careful to call super.process(...) when necessary to retain normal (rotating, translating) behavior as desired.