Page history Edit this page How do I edit this website?
Original MediaWiki page

3D Viewer › CustomBehavior

The content of this page has not been vetted since shifting away from MediaWiki. If you’d like to help, check out the how to help guide!

How to catch and handle events on the 3D canvas

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 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, the point’s coordinates are just displayed on the screen then.

Important:
Be careful to call super.process(...) when necessary to retain normal (rotating, translating) behavior as desired.