Java Reference
In-Depth Information
Understanding the TouchEvent Class
With more and more devices being equipped with a touch screen, built-in support for touch events makes JavaFX a
state-of-the art platform for creating applications that leverage multitouch capabilities, by which we mean that the
platform is able to track more than one touchpoint in a single set of events.
The
TouchEvent
class provides the
getTouchPoint()
method, which returns a specific touch point. The methods
on this
TouchPoint
are similar to the methods on a
MouseEvent
, for example, you can retrieve the relative and absolute
positions by calling
getX()
and
getY()
, or
getSceneX()
and
getSceneY()
, or
getScreenX()
and
getScreenY()
.
The
TouchEvent
class also allows the developer to get information about the other touch points that belong to the
same set. By calling
getEventSetId()
, you get the unique identifier of the set of
TouchEvent
instances, and the list of
all touch points in the set can be obtained by calling
getTouchPoints()
, which returns a list of
TouchPoint
instances.
Understanding the GestureEvent Class
Besides handling multitouch events, JavaFX also supports the creation and dispatching of gesture events. Gestures
are increasingly used on smartphones, tablets, touch screens, and other input devices. They provide an intuitive way
of performing an action, for example, by having the user swipe his or her finger. The
GestureEvent
class currently has
four subclasses, each representing a specific gesture:
RotateEvent
,
ScrollEvent
,
SwipeEvent
, and
ZoomEvent
. All of
these events have methods comparable to the
MouseEvent
for retrieving the position of the action—the
getX()
and
getY()
,
getSceneX()
and
getSceneY()
, and the
getScreenX()
and
getScreenY()
methods.
The specific subclasses all allow for retrieving a more detailed type of the event. A
SwipeEvent
, for example can
be a swipe to the right or the left, to the top or the bottom. This information is obtained by calling the
getEventType()
method on the
GestureEvent
.
Animating Nodes in the Scene
One of the strengths of JavaFX is the ease with which you can create graphically rich UIs. Part of that richness is
the ability to animate nodes that live in the
Scene
. At its core, animating a node involves changing the value of its
properties over a period of time. Examples of animating a node include the following.
•
Gradually increasing the size of a node when the mouse enters its bounds, and gradually
decreasing the size when the mouse exits its bounds. Note that this requires scaling the node,
which is referred to as a transform.
•
Gradually increasing or decreasing the opacity of a node to provide a fade-in or fade-out
effect, respectively.
•
Gradually altering values of properties in a node that change its location, causing it to move
from one location to another. This is useful, for example, when creating a game such as Pong.
A related capability is detecting when a node has collided with another node.
Animating a node involves the use of the
Timeline
class, located in the
javafx.animation
package. Depending
on the requirements of an animation and personal preference, use one of two general techniques:
Timeline
class directly and supply key frames that specify values and
actions at specific points in time.
•
Create an instance of the
javafx.animation.Transition
subclasses to define and associate specific transitions
with a node. Examples of transitions include causing a node to move along a defined path over
a period of time, and rotating a node over a period of time. Each of these transition classes
extends the
Timeline
class.
•
Use the
We now cover these techniques, showing examples of each, beginning with the first one listed.
Search WWH ::
Custom Search