Chapter 17 ■ i3D Game Square SeleCtion: uSinG the piCkreSult ClaSS with 3D moDelS
The MouseEvent Class: Trapping Mouse Clicks on 3D Primitives
The public MouseEvent class extends the InputEvent superclass. MouseEvent is kept in a javafx.scene.
input package along with its subclass MouseDragEvent and other InputEvent superclass subclasses.
MouseEvent implements the Serializable and Cloneable Java interfaces. This class is used for implementing
or “trapping” mouse events for processing by your Java game logic, which you will learn how to do during
this chapter. When mouse event generation, such as a click, occurs, the first (top or front) Node object under
cursor is “picked,” and the MouseEvent is delivered to that Node object event handling structure. The event
is delivered by using capturing and bubbling phases described by the public EventDispatcher Java interface
that is stored in the javafx.event package. The Java hierarchy for the MouseEvent class should therefore look
like the following:
java.lang.Object
java.util.EventObject
javafx.event.Event
javafx.scene.input.InputEvent
javafx.scene.input.MouseEvent
The mouse pointer (cursor) location is available in several different coordinate systems. It is available
using X ,Y coordinates relative to the origin of the MouseEvent’s Node object (as well as relative to your
Scene object), using sceneX, sceneY coordinates relative to the origin of the Scene that contains the Node,
or even using screenX, screenY coordinates relative to the origin of the display screen that contains the
mouse pointer. We’ll be comparing the Node that is clicked to our game-processing logic in the case of this
particular i3D BoardGame project.
There are a number of Event fields specific to the MouseEvent object. These are static and use
uppercase letters, as they’re “hard-coded” constants for different types of events offered by the MouseEvent
type of InputEvent. An ANY static EventType
any of the mouse event types.
A DRAG_DETECTED static EventType
identified as a source of a dragging gesture. A MOUSE_CLICKED static EventType
delivered when the mouse button has been clicked (pressed and released on the same node). This is what
we’ll be using for our i3D BoardGame. You can also trap events for the mouse being pressed and the mouse
being released. A MOUSE_PRESSED static EventType
button is pressed, and the MOUSE_RELEASED static EventType
mouse button is released.
You can also process events when your mouse moves over a Node and then off of that Node again
without any mouse click occurring! A MOUSE_ENTERED static EventType
when a mouse enters a Node object but is not clicked (this is called a hover). A MOUSE_ENTERED_TARGET
static EventType
boundary). Similarly, a MOUSE_EXITED static EventType
exits a Node object. The MOUSE_EXITED_TARGET static EventType
mouse first exits a Node object (crosses an edge boundary).
Finally, the MOUSE_MOVED static EventType
within a Node object when no buttons are being pressed or released. The MOUSE_DRAGGED static
EventType
button (called a drag operation).