JavaFX 多重转换

我们可以利用节点上所有变换的综合效果。为此,JavaFX 提供了addAll(Transform obj1, Transform obj2....)方法,该方法可以在<node-obj>.getTransforms()方法返回的引用上匿名调用。

以下示例在矩形上实现所有转换。

多重转换的例子

以下示例说明了对对象的所有转换的实现。在这里,我们创建了一个矩形并对其应用了平移、旋转、缩放和剪切等变换。

package com.yiidian;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Scale;
import javafx.scene.transform.Shear;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;

public class MultipleTransformations extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        //creating Rectangle and circle
        Rectangle rect = new Rectangle(50,10,200,250);
        Circle c = new Circle(300,260,100);

        //setting properties for the rectangle and circle
        c.setFill(Color.LIGHTBLUE);
        c.setStroke(Color.BLACK);
        rect.setStroke(Color.BLACK);
        rect.setFill(Color.LIMEGREEN);
        rect.setStrokeWidth(2);

        //creating the translate transform
        Translate trans=new Translate();
        trans.setX(100);
        trans.setY(10);

        //creating Rotate transform
        Rotate rotate = new Rotate();
        rotate.setAngle(30);
        rotate.setPivotX(50);
        rotate.setPivotY(100);

        //Creating Scale Transform
        Scale scale = new Scale();
        scale.setX(1.5);
        scale.setY(1.2);
        scale.setPivotX(50);
        scale.setPivotY(50);

        //creating Shear Transform
        Shear shear = new Shear();
        shear.setX(0.3);

        //applying all the transforms to the rectangle
        rect.getTransforms().addAll(trans,rotate,scale,shear);

        Group root = new Group();
        root.getChildren().addAll(rect,c);
        Scene scene = new Scene(root,550,500);
        primaryStage.setScene(scene);
        primaryStage.setTitle("一点教程网:Multiple Transformation Example");
        primaryStage.show();

    }
    public static void main(String[] args) {
        launch(args);
    }
}

输出结果为:

热门文章

优秀文章