Java Reference
In-Depth Information
you are reading from a remote source, such as an RSS feed, the text length difference
should not break the ticker. Next, you set the
TranslateTransition
to a linear
transition (
Interpolator.LINEAR
) to interpolate evenly between the start and end
values. To see more interpolator types or to see how to create custom interpolators, see
the Javadoc on
javafx.animation.Interpolator
s. Finally, in the example
the cycle count is set to 1, which will animate the ticker once based on the specified
duration. The following code snippet details creating a
TranslateTransition
that animates a
Text
node from right to left:
final TranslateTransition ticker = new
TranslateTransition();
ticker.setNode(news);
int newsLength = news.getText().length();
ticker.setDuration(Duration.millis((newsLength * 4/300)
* 15000));
ticker.setFromX(scene.widthProperty().doubleValue());
ticker.setToX(-scene.widthProperty().doubleValue()
- (newsLength * 5));
ticker.setFromY(19);
ticker.setInterpolator(Interpolator.LINEAR);
ticker.setCycleCount(1);
When the ticker's news has scrolled completely off of the ticker area to the far left
of the scene, you will want to stop and replay the news feed from the start (the far
right). To do this, you create an instance of an
EventHandler<ActionEvent>
object via a lambda expression, to be set on the ticker (
TranslateTransition
)
object using the
setOnFinished()
method. Here is how you replay the
Trans-
lateTransition
animation:
// when window resizes width wise the ticker will know
how far to move
// when ticker has finished reset and replay ticker
animation
ticker.setOnFinished((ActionEvent ae) -> {
ticker.stop();
ticker.setFromX(scene.getWidth());
ticker.setDuration(new Duration((newsLength * 4/300)
* 15000));