How it works...
This recipe shows you how to animate a shape to morph it to another shape smoothly using
the DelegateShape class. This class is a descendent of Shape . It does not represent an
actual shape itself, however, it uses its internal Interpolator instance to calculate the
necessary in-between shapes between a start and an end shape.
In our code snippet, we declare three starting shapes: a Rectangle , a Circle , and a
Polygon . Although these Shape instances are Interpolatable , it is impossible to
interpolate between them, as they are of different types (Interpolators can only interpolate
values of the same types). The DelegateShape class, however, can be assigned an object
of type Shape through its shape:Shape property. That object instance can then be
interpolated to another instance of Shape over time.
In the code snippet, the ShapeDelegate.shape property is initially assigned an instance
of the Polygon shape. Using a Timeline , the ShapeDelegate.shape is interpolated to
a Rectangle over a period of two seconds. At four seconds, ShapeDelegate.shape is
interpolated from Rectangle to Circle . Lastly, ShapeDelegate.shape is interpolated
from Circle to Polygon two seconds later. The entire animation produces a smooth
animation of the shapes morphing from one figure to another.
F Creating simple animation with the Transition API
F Composing animation with the Transition API
F Building animation with the KeyFrame API
Using data binding to drive animation
As you create more elaborate animation, you will run into situations where you want to
synchronize object movements in your animation sequences. You can do that by declaring
several instances of Timeline , or you can automate the synchronization of your objects
using bound variables. This recipe shows you how to use data binding to update object
properties automatically during an animation sequence.
This recipe uses the Timeline and KeyFrame classes to create animation sequences. If you
are not familiar with keyframe-based animation, review the recipe Building animation with
the KeyFrame API . This recipe also includes the notion of data binding covered under Using
binding and triggers to update variables in Chapter 1 , Getting Started with JavaFX .