📜  JavaFX动画的stroke变换

📅  最后修改于: 2020-10-14 03:27:36             🧑  作者: Mango

JavaFX StrokeTransition

可以对节点的笔触颜色进行动画处理,以使笔触颜色可以在指定的持续时间内在两个颜色值之间波动。

在JavaFX中,类javafx.animation.FillTransition表示填充过渡。我们需要实例化此类以创建适当的填充过渡。

物产

下表描述了该类的属性以及setter方法。

Property Description Setter Methods
duration This is an object type property of the class Duration. It represents the duration of the stroke Transition. setDuration(Duration duration)
fromValue This is a color type property. It represents the initial value of the color for the stroke transition. setFromValue(Color value)
shape This is an object type property of the class Shape. It represents the shape onto which the Stroke transition will be applied. setShape(Shape shape)
toValue This is color type property. It represents the target value of the color for the stroke transition. setToValue(Color value)

建设者

该类中有五个构造函数。

  • public StokeTransition():使用默认参数创建StrokeTransition的新实例。
  • public StokeTransition(Duration duration):使用指定的持续时间值创建Stroke Transition的新实例
  • 公共StokeTransition(持续时间,Color fromValue,Color toValue):使用指定的持续时间,颜色的初始值和颜色的目标值创建StrokeTransition的新实例。
  • public StokeTransition(Duration duration,Shape shape):使用指定的持续时间和要应用过渡的形状创建StrokeTransition的新实例。
  • 5public StokeTransition(持续时间,形状,颜色fromValue,Color toValue):使用指定的持续时间,形状,颜色初始值和颜色目标值创建StrokeTransition的新实例。

在以下示例中,圆圈的笔划从黑色变为紫色。

package application;
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 Stroke_Transition extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
//Creating Circle 
Circle cir = new Circle(200,150,100);
//Setting stroke and color for the circle
cir.setStroke(Color.BLUE);
cir.setFill(Color.RED);
cir.setStrokeWidth(10);

//Instantiating StrokeTransition class 
StrokeTransition stroke = new StrokeTransition();

//The transition will set to be auto reserved by setting this to true
stroke.setAutoReverse(true);

//setting cycle count for the Stroke transition 
stroke.setCycleCount(500);

//setting duration for the Stroke Transition 
stroke.setDuration(Duration.millis(500));

//setting the Initial from value of the Stroke color
stroke.setFromValue(Color.BLACK);

//setting the target value of the Stroke color 
stroke.setToValue(Color.PURPLE);

//setting polygon as the shape onto which the Stroke transition will be applied 
stroke.setShape(cir);

//playing the Stroke transition 
stroke.play();

//Configuring Group and Scene 
Group root = new Group();
root.getChildren().addAll(cir);
Scene scene = new Scene(root,420,300,Color.WHEAT);
primaryStage.setScene(scene);
primaryStage.setTitle("Stroke Transition example");
primaryStage.show();

}
}

输出: