Java Reference
In-Depth Information
Next, you create an instance variable anchorPt
( javafx.geometry.Point2D ) that will hold the path's starting point. Later, you
will see how these variables are updated based on mouse events. Shown here are the
instance variables that maintain the currently drawn path:
Path onePath = new Path();
Point2D anchorPt;
First, let's create a shape that will be animated. In this scenario, you'll create a
cool-looking red ball. To create a spherical-looking ball, create a gradient color Radi-
alGradient that's used to paint or fill a circle shape. (Refer to Recipe 15-6 for how
to fill shapes with a gradient paint.) Once you have created the red spherical ball, you
need to create the PathTransition object to perform the path-following animation.
After instantiating a PathTransition() object, simply set the duration to four
seconds and the cycle count to one. The cycle count is the number of times the anima-
tion cycle will occur. Next, you set the node to reference the red ball ( sphere ). Then,
you set the path() method to the instance variable onePath , which contains all the
coordinates and lines that make up a drawn path. After setting the path for the sphere to
animate, you should specify how the shape will follow the path, such as perpendicular
to a tangent point on the path. The following code creates an instance of a path trans-
ition:
// animate sphere by following the path.
final PathTransition pathTransition = new
PathTransition();
pathTransition.setDuration(Duration.millis(4000));
pathTransition.setCycleCount(1);
pathTransition.setNode(sphere);
pathTransition.setPath(onePath);
pathTransition.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
After you've created the path transition, you'll want it to clean up when the anima-
tion is completed. To reset or clean up the path variable when the animation is finished,
create and add an event handler to listen to the onFinished property event on the
path transition object.
The following code snippet adds an event handler to clear the current path informa-
tion:
Search WWH ::




Custom Search