Javafx 简明教程
JavaFX - QuadCurveTo Path Object
二次曲线或四次曲线通常定义为由二次方程式定义的曲线。在 JavaFX 中,我们利用 6 种不同的属性来创建这个四次曲线节点。若要使用四次曲线创建复杂形状,我们必须每次指定这些属性。
Quadratic Curve or QuadCurve is generally defined as the curve that is defined by a quadratic equation. In JavaFX, we make use of 6 different properties to create this QuadCurve node. And to create a complex shape using QuadCurve, we would have to specify these properties everytime it is required.
JavaFX 通过提供四次曲线路径对象,将这个过程简化,该对象采用较少的属性来绘制它。
JavaFX makes this process simpler by providing a QuadCurve path object that takes lesser number of properties to draw it.
我们将在本章后面的部分详细了解四次曲线路径对象。
We will learn about the QuadCurve Path object in detail further in this chapter.
QuadCurve Path Object
路径元素二次曲线用于将一个 quadratic curve 从当前位置绘制到指定坐标中的点。
The path element quadratic curve is used to draw a quadratic curve to a point in the specified coordinates from the current position.
它由名为 QuadraticCurveTo 的类表示。该类属于 javafx.scene.shape 包。
It is represented by a class named QuadraticCurveTo. This class belongs to the package javafx.scene.shape.
此类具有 4 个双数据类型的属性,即 −
This class has 4 properties of the double datatype namely −
-
setX − The x coordinate of the point to which a curve is to be drawn from the current position.
-
setY − The y coordinate of the point to which a curve is to be drawn from the current position.
-
controlX − The x coordinate of the control point of the curve.
-
controlY − The y coordinate of the control point of the curve.
若要绘制二次曲线,你需将值传递给这些属性。可以通过在实例化时按照相同顺序将它们传递给该类的构造函数来实现;或者通过使用它们各自的赋值方法实现。
To draw a quadratic curve, you need to pass values to these properties. This can be done either by passing them to the constructor of this class, in the same order, at the time of instantiation; Or, by using their respective setter methods.
Steps to draw PathElement Quadratic Curve
若要从当前位置在 JavaFX 中绘制一条二次曲线到指定点,请遵循以下给出的步骤。
To draw a quadratic curve to a specified point from the current position in JavaFX, follow the steps given below.
Step 1: Creating a Path Object
实例化 Path 类以在 Application 类的 start() 方法内创建 Path 对象,如下所示 −
Instantiate the Path class to create a Path object within the start() method of Application class as shown below −
public class ClassName extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
//Creating a Path object
Path path = new Path();
}
}
Step 2: Create a Path
创建 MoveTo 路径元素并将 XY 坐标设置为开始点的线条坐标(100、150)。可以通过按以下方式使用类 MoveTo 的 setX() 和 setY() 方法来实现。
Create the MoveTo path element and set the XY coordinates to the starting point of the line to the coordinates (100, 150). This can be done by using the methods setX() and setY() of the class MoveTo as shown below.
//Moving to the starting point
MoveTo moveTo = new MoveTo();
moveTo.setX(100.0f);
moveTo.setY(150.0f);
Step 3: Creating an Object of the Class QuadCurveTo
通过按以下方式实例化属于包 javafx.scene.shape 的名为 QuadCurveTo 的类来创建路径元素二次曲线。
Create the path element Quadratic Curve by instantiating the class named QuadCurveTo which belongs to the package javafx.scene.shape as follows.
//Creating an object of the class QuadCurveTo
QuadCurveTo quadCurveTo = new QuadCurveTo()
Step 4: Setting Properties to the Quadratic Curve Element
指定从当前位置绘制二次曲线的点的坐标。然后,你应通过它们的赋值方法为 x、y、controlx、controlY 和控制点坐标设置属性,如下所示。
Specify the coordinates of the point to which a Quadratic Curve is to be drawn from the current position. Then you should set the properties x, y, controlx, controlY and the coordinates of the control point by their setter methods as shown below.
//Setting properties of the class QuadCurve
quadCurveTo.setX(500.0f);
quadCurveTo.setY(220.0f);
quadCurveTo.setControlX(250.0f);
quadCurveTo.setControlY(0.0f);
Step 5: Adding Elements to the Observable List of the Path Class
将前述步骤中创建的路径元素 MoveTo 和 QuadraticCurveTo 添加到 Path 类的可观察列表,如下所示 −
Add the path elements MoveTo and QuadraticCurveTo created in the previous steps to the observable list of the Path class as follows −
//Adding the path elements to Observable list of the Path class
path.getElements().add(moveTo);
path.getElements().add(quadCurveTo)
Step 6: Launching Application
一旦创建了 QuadCurveTo 路径对象,按照以下给出的步骤正确启动应用程序 −
Once the QuadCurveTo path object is created, follow the given steps below to launch the application properly −
-
Firstly, instantiate the class named Scene by passing the Group object as a parameter value to its constructor. To this constructor, you can also pass dimensions of the application screen as optional parameters.
-
Then, set the title to the stage using the setTitle() method of the Stage class.
-
Now, a Scene object is added to the stage using the setScene() method of the class named Stage.
-
Display the contents of the scene using the method named show().
-
Lastly, the application is launched with the help of the launch() method.
Example 1
以下是一个程序,使用 JavaFX 名为 Path 的类从当前点绘制二次曲线到指定位置。将该代码保存在名为 QuadCurveToExample.java 的文件中。
Following is a program which draws a quadratic curve from the current point to a specified position using the class named Path of JavaFX. Save this code in a file with the name QuadCurveToExample.java.
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.QuadCurveTo;
public class QuadCurveToExample extends Application {
@Override
public void start(Stage stage) {
//Creating an object of the class named Path
Path path = new Path();
//Moving to the starting point
MoveTo moveTo = new MoveTo();
moveTo.setX(100.0);
moveTo.setY(150.0);
//Instantiating the class QuadCurve
QuadCurveTo quadCurveTo = new QuadCurveTo();
//Setting properties of the class QuadCurve
quadCurveTo.setX(500.0f);
quadCurveTo.setY(220.0f);
quadCurveTo.setControlX(250.0f);
quadCurveTo.setControlY(0.0f);
//Adding the path elements to Observable list of the Path class
path.getElements().add(moveTo);
path.getElements().add(quadCurveTo);
//Creating a Group object
Group root = new Group(path);
//Creating a scene object
Scene scene = new Scene(root, 600, 300);
//Setting title to the Stage
stage.setTitle("Drawing a QuadCurve through a specified path");
//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 QuadCurveToExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls QuadCurveToExample
执行时,上述程序生成一个显示二次曲线的 JavaFX 窗口。它从当前位置绘制到指定点,如下所示。
On executing, the above program generates a JavaFX window displaying a quadratic curve. This is drawn from the current position to the specified point as shown below.
Example 2
在此示例中,让我们尝试对这个四次曲线路径应用一些动画。这里,我们绘制一个示例四次曲线并对其应用淡出过渡。将该代码保存在名为 QuadCurveToAnimation.java 的文件中。
In this example, let us try to apply some animation to this QuadCurve Path. Here, we are drawing a sample QuadCurve and applying the Fade Transition to it. Save this code in a file with the name QuadCurveToAnimation.java.
import javafx.application.Application;
import javafx.animation.FadeTransition;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.QuadCurveTo;
import javafx.util.Duration;
public class QuadCurveToAnimation extends Application {
@Override
public void start(Stage stage) {
//Creating an object of the class named Path
Path path = new Path();
//Moving to the starting point
MoveTo moveTo = new MoveTo();
moveTo.setX(100.0);
moveTo.setY(150.0);
//Instantiating the class QuadCurve
QuadCurveTo quadCurveTo = new QuadCurveTo();
//Setting properties of the class QuadCurve
quadCurveTo.setX(500.0f);
quadCurveTo.setY(220.0f);
quadCurveTo.setControlX(250.0f);
quadCurveTo.setControlY(0.0f);
//Adding the path elements to Observable list of the Path class
path.getElements().add(moveTo);
path.getElements().add(quadCurveTo);
//Creating the fade Transition
FadeTransition fadeTransition = new FadeTransition();
fadeTransition.setDuration(Duration.millis(1000));
//Setting the node for Transition
fadeTransition.setNode(path);
//Setting the property fromValue of the transition (opacity)
fadeTransition.setFromValue(1.0);
//Setting the property toValue of the transition (opacity)
fadeTransition.setToValue(0.3);
//Setting the cycle count for the transition
fadeTransition.setCycleCount(50);
//Setting auto reverse value to false
fadeTransition.setAutoReverse(false);
//Playing the animation
fadeTransition.play();
//Creating a Group object
Group root = new Group(path);
//Creating a scene object
Scene scene = new Scene(root, 600, 300);
//Setting title to the Stage
stage.setTitle("Drawing a QuadCurve through a specified path");
//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 QuadCurveToAnimation.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls QuadCurveToAnimation
执行时,上述程序生成一个 JavaFX 窗口,显示一个逐渐消失的二次曲线。它从当前位置绘制到指定点,如下所示。
On executing, the above program generates a JavaFX window displaying a quadratic curve that is fading out. This is drawn from the current position to the specified point as shown below.