Javafx 简明教程

JavaFX - Transformations

变换只不过是通过应用一些规则将图形变成其他东西。这些规则使你可以应用各种类型的变换,如保持对象的形状改变其位置、基于角度旋转对象、改变对象的大小等。

Transformation is nothing but changing graphics into something else by applying some rules. These rules allow you to apply various types of transformations such as shifting the position of the object by maintaining its shape, rotating the object based on an angle, changing the size of the object, etc.

使用 JavaFX,你可以对单个节点或节点组应用变换。你还可以一次对 JavaFX 节点应用一种类型的变换或多种变换。所有这些变换都由各种类表示,这些类都是 Transform 类的子类,它们属于 javafx.scene.transform 包。

Using JavaFX, you can apply transformations on either a single node or group of nodes. You can also apply a single type of transformation or multiple transformations at a time to a JavaFX node. All these transformations are represented by various classes that are all subclasses of the Transform class and these belong to the package javafx.scene.transform.

S.No

Transformation & Description

1

RotationIn rotation, we rotate the object at a particular angle θ (theta) from its origin.

2

ScalingTo change the size of an object, scaling transformation is used.

3

TranslationMoves an object to a different position on the screen.

4

ShearingA transformation that slants the shape of an object is called the Shear Transformation.

Transform 类对 JavaFX 节点实现了仿射变换。仿射变换只不过是保留对象中点、直线和这些直线的平行性在输出对象中的类型的变换。可以使用 Affine 来将这些变换应用到 JavaFX 节点上,它扩展了 Transform 类。

The Transform class implements affine transformations on JavaFX nodes. Affine transformations are nothing but the type of transformations that preserve the points, straight lines, and parallelism of these straight lines of the source object in the output object. These transformations can be applied on the JavaFX nodes with the help of Affine class extending the Transform class.

Multiple Transformations

你可以在 JavaFX 中通过两种方式对节点应用多个变换。你可以一次应用一个变换,或将多个变换组合在一起并一次应用它们。以下程序是一个示例,它同时在矩形上执行 Rotation, ScalingTranslation 变换。

You can apply multiple transformations on nodes in JavaFX, in two ways. You can apply one transformation at a time, or combine several transformations together and apply them at once. The following program is an example which performs Rotation, Scaling and Translation transformations on a rectangle simultaneously.

将此代码保存在一个名为 − 的文件中

Save this code in a file with the name −

MultipleTransformationsExample.java

MultipleTransformationsExample.java.

Example

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

public class MultipleTransformationsExample extends Application {
   @Override
   public void start(Stage stage) {
      //Drawing a Rectangle
      Rectangle rectangle = new Rectangle(50, 50, 100, 75);

      //Setting the color of the rectangle
      rectangle.setFill(Color.BURLYWOOD);

      //Setting the stroke color of the rectangle
      rectangle.setStroke(Color.BLACK);

      //creating the rotation transformation
      Rotate rotate = new Rotate();

      //Setting the angle for the rotation
      rotate.setAngle(20);

      //Setting pivot points for the rotation
      rotate.setPivotX(150);
      rotate.setPivotY(225);

      //Creating the scale transformation
      Scale scale = new Scale();

      //Setting the dimensions for the transformation
      scale.setX(1.5);
      scale.setY(1.5);

      //Setting the pivot point for the transformation
      scale.setPivotX(300);
      scale.setPivotY(135);

      //Creating the translation transformation
      Translate translate = new Translate();

      //Setting the X,Y,Z coordinates to apply the translation
      translate.setX(250);
      translate.setY(0);
      translate.setZ(0);

      //Adding all the transformations to the rectangle
      rectangle.getTransforms().addAll(rotate, scale, translate);

      //Creating a Group object
      Group root = new Group(rectangle);

      //Creating a scene object
      Scene scene = new Scene(root, 600, 300);

      //Setting title to the Stage
      stage.setTitle("Multiple transformations");

      //Adding scene to the stage
      stage.setScene(scene);

      //Displaying the contents of the stage
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

使用以下命令,从命令提示符编译并执行已保存的 java 文件。

Compile and execute the saved java file from the command prompt using the following commands.

javac --module-path %PATH_TO_FX% --add-modules javafx.controls MultipleTransformationsExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls MultipleTransformationsExample

执行后,上述程序会生成如下所示的 JavaFX 窗口。

On executing, the above program generates a JavaFX window as shown below.

multiple transformation

Transformations on 3D Objects

JavaFX 允许你沿三个坐标执行变换。然而,若要显示具有 3 个维度(长度、宽度和深度)的对象,JavaFX 使用一个名为 Z 缓冲区的概念。

JavaFX allows you to perform transformations along three coordinates. However, to display objects with 3 dimensions (length, breadth and depth), JavaFX makes use of the concept called Z-buffering.

如果你想要创建一个 3D 效果变换,请将所有三个坐标 x、y 和 z 连同 x 轴和 y 轴指定给变换构造函数。此外,为了能够在 JavaFX 中看到三维物体和变换效果,用户必须启用透视相机。

If you want to create a 3-D effect transformation, specify all three coordinates x, y and z to the transformation constructors along with x-axis and y-axis. And, to be able to see the 3-D objects and transformation effects in JavaFX, users must enable the perspective camera.

Example

以下是一个旋转和转换三维框的示例。

Following is an example which rotates and translates a 3-Dimensional box.

将此代码保存在名为 RotationExample3D.java 的文件中。

Save this code in a file with the name RotationExample3D.java.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Box;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;

public class RotationExample3D extends Application {
   @Override
   public void start(Stage stage) {
      //Drawing a Box
      Box box = new Box();

      //Setting the properties of the Box
      box.setWidth(150.0);
      box.setHeight(150.0);
      box.setDepth(150.0);

      //Creating the translation transformation
      Translate translate = new Translate();
      translate.setX(400);
      translate.setY(150);
      translate.setZ(25);

      Rotate rxBox = new Rotate(0, 0, 0, 0, Rotate.X_AXIS);
      Rotate ryBox = new Rotate(0, 0, 0, 0, Rotate.Y_AXIS);
      Rotate rzBox = new Rotate(0, 0, 0, 0, Rotate.Z_AXIS);
      rxBox.setAngle(30);
      ryBox.setAngle(50);
      rzBox.setAngle(30);
      box.getTransforms().addAll(translate,rxBox, ryBox, rzBox);

      //Creating a Group object
      Group root = new Group(box);

      //Creating a scene object
      Scene scene = new Scene(root, 600, 300);

      //Setting title to the Stage
      stage.setTitle("Drawing a cylinder");

      //Adding scene to the stage
      stage.setScene(scene);

      //Displaying the contents of the stage
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

使用以下命令,从命令提示符编译并执行已保存的 java 文件。

Compile and execute the saved java file from the command prompt using the following commands.

javac --module-path %PATH_TO_FX% --add-modules javafx.controls RotationExample3D.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls RotationExample3D

执行后,上述程序会生成如下所示的 JavaFX 窗口。

On executing, the above program generates a JavaFX window as shown below.

transformation on 3d objects