JavaFX - 几何过渡
淡入淡出过渡
以下是演示 JavaFX 中淡入淡出过渡的程序。将此代码保存在名为 FadeTransitionExample.java 的文件中。
import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class FadeTransitionExample extends Application {
@Override
public void start(Stage stage) {
//绘制一个圆圈
Circle circle = new Circle();
//设置圆圈的位置
circle.setCenterX(300.0f);
circle.setCenterY(135.0f);
//设置圆圈的半径
circle.setRadius(100.0f);
//设置圆圈的颜色
circle.setFill(Color.BROWN);
//设置圆圈的描边宽度
circle.setStrokeWidth(20);
//创建淡入淡出过渡
FadeTransition fadeTransition = new FadeTransition(Duration.millis(1000));
//设置过渡的节点
fadeTransition.setNode(circle);
//设置过渡的属性 fromValue(不透明度)
fadeTransition.setFromValue(1.0);
//设置过渡的 toValue 属性(不透明度)
fadeTransition.setToValue(0.3);
//设置过渡的循环次数
fadeTransition.setCycleCount(50);
//将自动反转值设置为 false
fadeTransition.setAutoReverse(false);
//播放动画
fadeTransition.play();
//创建一个 Group 对象
Group root = new Group(circle);
//创建一个 scene 对象
Scene scene = new Scene(root, 600, 300);
//设置 Stage 的标题
stage.setTitle("淡入淡出过渡示例");
//将 scene 添加到舞台(Stage)
stage.setScene(scene);
//显示舞台(Stage)的内容
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
使用以下命令从命令提示符编译并执行保存的 java 文件。
javac FadeTransitionExample.java java FadeTransitionExample
执行时,上述程序将生成一个 JavaFX 窗口,如下所示。
填充过渡
以下是演示 JavaFX 中填充过渡的程序。将此代码保存在名为 FillTransitionExample.java 的文件中。
import javafx.animation.FillTransition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class FillTransitionExample extends Application {
@Override
public void start(Stage stage) {
//绘制一个圆圈
Circle circle = new Circle();
//设置圆圈的位置
circle.setCenterX(300.0f);
circle.setCenterY(135.0f);
//设置圆圈的半径
circle.setRadius(100.0f);
//设置圆圈的颜色
circle.setFill(Color.BROWN);
//设置圆圈的描边宽度
circle.setStrokeWidth(20);
//创建填充过渡
FillTransition fillTransition = new FillTransition(Duration.millis(1000));
//设置过渡的形状
fillTransition.setShape(circle);
//设置过渡的起始值(颜色)
fillTransition.setFromValue(Color.BLUEVIOLET);
//设置过渡的 toValue(颜色)
fillTransition.setToValue(Color.CORAL);
//设置过渡的循环次数
fillTransition.setCycleCount(50);
//将自动反转值设置为 false
fillTransition.setAutoReverse(false);
//播放动画
fillTransition.play();
//创建一个 Group 对象
Group root = new Group(circle);
//创建一个 scene 对象
Scene scene = new Scene(root, 600, 300);
//设置 Stage 的标题
stage.setTitle("填充过渡示例");
//将场景添加到舞台(Stage)
stage.setScene(scene);
//显示舞台(Stage)的内容
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
使用以下命令从命令提示符编译并执行保存的 java 文件。
javac FillTransitionExample.java java FillTransitionExample
执行时,上述程序将生成一个 JavaFX 窗口,如下所示。
描边过渡
以下是演示 JavaFX 中 Stoke Transition 的程序。将此代码保存在名为 StrokeTransitionExample.java 的文件中。
import javafx.animation.StrokeTransition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class StrokeTransitionExample extends Application {
@Override
public void start(Stage stage) {
//绘制一个圆圈
Circle circle = new Circle();
//设置圆圈的位置
circle.setCenterX(300.0f);
circle.setCenterY(135.0f);
//设置圆圈的半径
circle.setRadius(100.0f);
//设置圆圈的颜色
circle.setFill(Color.BROWN);
//设置圆圈的描边宽度
circle.setStrokeWidth(20);
//创建描边过渡
StrokeTransition strokeTransition = new StrokeTransition();
//设置过渡的持续时间
strokeTransition.setDuration(Duration.millis(1000));
//设置过渡的形状
strokeTransition.setShape(circle);
//设置过渡的 fromValue 属性(颜色)
strokeTransition.setFromValue(Color.BLACK);
//设置过渡的 toValue 属性(颜色)
strokeTransition.setToValue(Color.BROWN);
//设置过渡的循环计数
strokeTransition.setCycleCount(50);
//将自动反转值设置为 false
strokeTransition.setAutoReverse(false);
//播放动画
strokeTransition.play();
//创建 Group 对象
Group root = new Group(circle);
//创建 scene 对象
Scene scene = new Scene(root, 600, 300);
//设置 Stage 的标题
stage.setTitle("Stroke transition example");
//将 scene 添加到 stage
stage.setScene(scene);
//显示 stage 的内容
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
使用以下命令从命令提示符编译并执行保存的 java 文件。
javac StrokeTransitionExample.java java StrokeTransitionExample
执行时,上述程序将生成一个 JavaFX 窗口,如下所示。

javafx_animations.html