Java Reference
In-Depth Information
Using the PathTransition Class
As shown in Listing 2-7, defining a
PathTransition
includes supplying an instance of type
Path
to the path property
that represents the geometric path that the node is to travel. Here we're creating a
Path
instance that defines an arc
beginning at 100 pixels on the x axis and 50 pixels on the y axis, ending at 300 pixels on the x axis and 50 pixels on the y
axis, with 350 pixel horizontal and vertical radii. This is accomplished by creating a
Path
that contains the
MoveTo
and
ArcTo
path elements. Take a look at the
javafx.scene.shape
package in the JavaFX API docs for more information on
the
PathElement
class and its subclasses, which are used for creating a path.
■
the properties in the
ArcTo
class are fairly intuitive except for
sweepFlag
. if
sweepFlag
is true, the line joining
the center of the arc to the arc itself sweeps through increasing angles. Otherwise, it sweeps through decreasing angles.
Tip
Another property of the
PathTransition
class is orientation, which controls whether the node's orientation
remains unchanged or stays perpendicular to the path's tangent as it moves along the path. Listing 2-7 uses the
OrientationType.ORTHOGONAL_TO_TANGENT
constant to accomplish the latter, as the former is the default.
Drawing an Ellipse
As shown in Listing 2-7, drawing an
Ellipse
is similar to drawing a
Circle
, the difference being that an additional
radius is required (
radiusX
and
radiusY
instead of just
radius
).
Now that you've learned how to animate nodes by creating a timeline and by creating transitions, we create a very
simple Pong-style game that requires animating a ping pong ball. In the process, you learn how to detect when the
ball has hit a paddle or wall in the game.
The Zen of Node Collision Detection
When animating a node, you sometimes need to know when the node has collided with another node. To
demonstrate this capability, our colleague Chris Wright developed a simple version of the Pong-style game that we
call ZenPong. Originally we asked him to build the game with only one paddle, which brought the famous Zen koan
(philosophical riddle), “What is the sound of one hand clapping,” to mind. Chris had so much fun developing the
game that he snuck a second paddle in, but we're still calling this example ZenPong. Figure
2-11
shows this very
simple form of the game when first invoked.
Search WWH ::
Custom Search