个性化阅读
专注于IT技术分析

JavaFX路径转换

点击下载

本文概述

它允许节点在指定的持续时间内通过指定的路径进行动画处理。在JavaFX中, 通过实例化类javafx.scene.shape.Path定义路径。

通过以规则的间隔更新节点的x和y坐标来完成沿该路径的平移。仅在将方向设置为OrientationType.ORTHOGONAL_TO_TANGENT的情况下才能进行旋转。

在JavaFX中, 类javafx.animation.PathTransition表示路径转换。我们需要实例化此类以创建适当的路径转换。

属性

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

属性 描述 设置方法
duration 此属性是Duration类的对象类型。这代表了过渡的寿命。 setDuraton(Duration duration)
node 这是Node类的对象。这表示过渡将应用到的节点。 setNode(Node node)
orientation 这是PathTransition.OrientationType引用的对象类型属性。它代表节点沿路径的垂直方向。 SetOrientation(PathTransition.OrientationType方向类型)
path 这是Shape类的对象类型属性。它指定了动画路径的轮廓所经历的形状。 setPath(Shape shape)

构造函数

该类中有三个构造函数。

  1. public PathTransition():使用默认参数创建路径转换的实例
  2. public PathTransition(Duration duration, Shape path):使用指定的持续时间和路径创建路径过渡的实例
  3. public PathTransition(Duration duration, Shape path, Node node):使用指定的持续时间, 路径和节点创建PathTransition的实例。

在以下示例中, 我们创建了一个多边形, 并在其上应用了路径过渡, 该路径过渡模拟了摆的路径。

package application;
import javafx.animation.PathTransition;
import javafx.animation.PathTransition.OrientationType;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.CubicCurveTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.Polygon;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Path_Transition extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
	// TODO Auto-generated method stub
	//Creating Polygon 
	Polygon poly = new Polygon(); 
	poly.getPoints().addAll(new Double[] {320.0, 270.0, 270.0, 220.0, 270.0, 270.0, 320.0, 120.0, 370.0, 270.0, 370.0, 220.0});
	
	//Setting Colour and Stroke properties for the polygon  
	poly.setFill(Color.LIMEGREEN);
	poly.setStroke(Color.BLACK);

    //Setting up the path 
	Path path = new Path();
    path.getElements().add (new MoveTo (150f, 70f));
    path.getElements().add (new CubicCurveTo (240f, 230f, 500f, 340f, 600, 50f));
    
    //Instantiating PathTransition class 
    PathTransition pathTransition = new PathTransition();
   
    //Setting duration for the PathTransition
    pathTransition.setDuration(Duration.millis(1000));
    
    //Setting Node on which the path transition will be applied 
    pathTransition.setNode(poly);
    
    //setting path for the path transition 
    pathTransition.setPath(path);
    
    //setting orientation for the path transition 
    pathTransition.setOrientation(OrientationType.ORTHOGONAL_TO_TANGENT);
    
    //setting up the cycle count 
    pathTransition.setCycleCount(10);
    
    //setting auto reverse to be true 
    pathTransition.setAutoReverse(true);

    //Playing path transition 
    pathTransition.play();
    
    //Configuring group and scene 
    Group root = new Group();
	root.getChildren().addAll(poly);
	Scene scene = new Scene(root, 700, 350, Color.WHEAT);
	primaryStage.setScene(scene);
	primaryStage.setTitle("Path Transition Example");
	primaryStage.show();
}
public static void main(String[] args) {
	launch(args);
}

}

输出:

JavaFX路径转换
赞(0)
未经允许不得转载:srcmini » JavaFX路径转换

相关推荐

评论 抢沙发

评论前必须登录!