Java黑皮书15.27(控制一个移动的文本)
package sample;
import javafx.application.Application;
import javafx.animation.PathTransition;
import javafx.animation.Timeline;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
Text text = new Text("Programming is fun");
pane.getChildren().add(text);
Line line = new Line(-80, 200, 680, 200);
PathTransition pathTransition = new PathTransition();
pathTransition.setDuration(Duration.millis(2000));
pathTransition.setPath(line);
pathTransition.setNode(text);
pathTransition.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
pathTransition.setCycleCount(Timeline.INDEFINITE);
pathTransition.play();
pane.setOnMousePressed(e -> pathTransition.pause());
pane.setOnMouseReleased(e -> pathTransition.play());
Scene scene = new Scene(pane, 600, 400);
primaryStage.setTitle("控制一个移动的文本");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
运行结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1bf2334b1ef0952dd63f9a7798ea3946.png)