Saturday, May 5, 2012

Create text with TranslateTransition animation effect

Example:


package javafx_text;

import javafx.animation.Interpolator;
import javafx.animation.Timeline;
import javafx.animation.TranslateTransition;
import javafx.animation.TranslateTransitionBuilder;
import javafx.application.Application;
import javafx.geometry.VPos;
import javafx.scene.Group;
import javafx.scene.GroupBuilder;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.*;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_Text extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("java-buddy.blogspot.com");
Group root = new Group();

String Santa_Claus_Is_Coming_To_Town =
"You better watch out\n"
+ "You better not cry\n"
+ "Better not pout\n"
+ "I'm telling you why\n"
+ "Santa Claus is coming to town\n"
+ "\n"
+ "He's making a list\n"
+ "And checking it twice;\n"
+ "Gonna find out Who's naughty and nice\n"
+ "Santa Claus is coming to town\n"
+ "\n"
+ "He sees you when you're sleeping\n"
+ "He knows when you're awake\n"
+ "He knows if you've been bad or good\n"
+ "So be good for goodness sake!\n"
+ "\n"
+ "O! You better watch out!\n"
+ "You better not cry\n"
+ "Better not pout\n"
+ "I'm telling you why\n"
+ "Santa Claus is coming to town\n"
+ "Santa Claus is coming to town\n";

Text textSong = TextBuilder.create()
.text(Santa_Claus_Is_Coming_To_Town)
.layoutX(50)
.textOrigin(VPos.TOP)
.textAlignment(TextAlignment.JUSTIFY)
.fill(Color.BLUE)
.font(Font.font("SansSerif", FontPosture.ITALIC, 18))
.build();

TranslateTransition translateTransition = TranslateTransitionBuilder.create()
.node(textSong)
.fromY(500)
.toY(-500)
.duration(new Duration(10000))
.interpolator(Interpolator.LINEAR)
.cycleCount(Timeline.INDEFINITE)
.build();


Group myGroup = GroupBuilder.create()
.children(textSong)
.build();

root.getChildren().add(myGroup);

primaryStage.setScene(new Scene(root, 500, 400));
primaryStage.show();

translateTransition.play();
}
}


No comments:

Post a Comment