Javafx 简明教程
JavaFX - CubicCurveTo Path Object
三次曲线是一个二维结构,是一种贝塞尔曲线。贝塞尔曲线被定义为通过一组控制点 (P0…Pn) 的曲线。当控制点的数量为 4(或曲线的阶数为 3)时,它被称为三次曲线。
A Cubic curve is a two dimensional structure that is a type of a Bezier curve. A Bezier curve is defined as a curve that passes through a set of control points (P0…Pn). It is called a Cubic curve when the number of control points are 4 (or, if the order of the curve is 3).
JavaFX 还提供一个 CubicCurve 路径对象来绘制更复杂的形状。在这里,我们仅考虑三次曲线的端点和控制点,以构造路径对象。让我们进一步详细了解如何构造它。
JavaFX also provides a CubicCurve Path object to draw a more complex shape. Here, we will be considering only the end points of the Cubic Curve and the control points in order to construct a Path object. Let us see how to construct it in detail further.
CubicCurve Path Object
路径元素三次曲线用于绘制一个 cubic curve 到从当前位置到指定坐标中的点。
The path element cubic curve is used to draw a cubic curve to a point in the specified coordinates from the current position.
它由名为 CubicCurveTo 的类表示。此类属于包 javafx.scene.shape 。
It is represented by a class named CubicCurveTo. This class belongs to the package javafx.scene.shape.
此类有 6 个 double 数据类型的属性,即 −
This class has 6 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.
-
setX − The y coordinate of the point to which a curve is to be drawn from the current position.
-
controlX1 − The x coordinate of the 1st control point of the curve.
-
controlY1 − The y coordinate of the 1st control point of the curve.
-
controlX2 − The x coordinate of the 2nd control point of the curve.
-
controlY2 − The y coordinate of the 2nd control point of the curve.
要绘制三次曲线,你需要将值传递给这些属性。可以通过将这些值传递给此类的构造函数来完成。这些值应与实例化时的顺序相同;或者使用它们的相应 setter 方法。
To draw a cubic curve, you need to pass values to these properties. This can be done by passing them to the constructor of this class. These should be in the same order as they were at the time of instantiation; Or, by using their respective setter methods.
Steps to draw PathElement Cubic Curve
要在 JavaFX 中从当前位置绘制三次曲线到指定点,请按照如下给定的步骤操作。
To draw a cubic curve to a specified point from the current position in JavaFX, follow the steps given below.
Step 1: Creating a Class
如以下所示,在应用程序类的 start() 方法内创建一个三次曲线路径对象。
Create a Cubic Curve Path object inside the start() method of the 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 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 CubicCurveTo
通过实例化名为 CubicCurveTo 的类创建路径元素二次曲线,该类属于 javafx.scene.shape 包,如下所示 −
Create the path element quadratic curve by instantiating the class named CubicCurveTo, which belongs to the package javafx.scene.shape as shown below −
//Creating an object of the class CubicCurveTo
CubicCurveTo cubicCurveTo= new CubicCurveTo ();
Step 4: Setting Properties to the Cubic Curve Element
指定要从当前位置绘制三次曲线的点的坐标。然后你应该通过它们的 setter 方法设置属性 x、y、controlX1、controlY1、controlX2、controlY2 和控制点的坐标,如下所示。
Specify the coordinates of the point to which a cubic curve is to be drawn from the current position. Then you should set the properties x, y, controlX1, controlY1, controlX2, controlY2 and the coordinates of the control point by their setter methods as shown below.
//Setting properties of the class CubicCurve
cubicCurveTo.setControlX1(400.0f);
cubicCurveTo.setControlY1(40.0f);
cubicCurveTo.setControlX2(175.0f);
cubicCurveTo.setControlY2(250.0f);
cubicCurveTo.setX(500.0f);
cubicCurveTo.setY(150.0f);
Step 5: Adding Elements to the Observable List of Path Class
将前面步骤创建的路径元素 → MoveTo 和 CubicCurveTo 添加到 Path 类的 observable 列表,如下所示 −
Add the path elements → MoveTo and CubicCurveTo, 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(cubicCurveTo);
Step 6: Launching Application
创建 CubicCurveTo 路径对象后,按照下面给出的步骤正确启动应用程序 −
Once the CubicCurveTo 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
下面是用 JavaFX 的 Path 类从当前点绘制到指定位置的三次曲线的程序。将此代码保存在名称为 CubicCurveToExample.java 的文件中。
Following is the program which draws a cubic 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 CubicCurveToExample.java.
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.shape.CubicCurveTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
public class CubicCurveToExample 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 CubicCurve
CubicCurveTo cubicCurveTo = new CubicCurveTo();
//Setting properties of the class CubicCurve
cubicCurveTo.setControlX1(400.0f);
cubicCurveTo.setControlY1(40.0f);
cubicCurveTo.setControlX2(175.0f);
cubicCurveTo.setControlY2(250.0f);
cubicCurveTo.setX(500.0f);
cubicCurveTo.setY(150.0f);
//Adding the path elements to Observable list of the Path class
path.getElements().add(moveTo);
path.getElements().add(cubicCurveTo);
//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 cubic 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 CubicCurveToExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls CubicCurveToExample
执行时,上述程序会生成一个 JavaFX 窗口,显示三次曲线。这是从当前位置绘制到指定点的,如下所示。
On executing, the above program generates a JavaFX window displaying a Cubic curve. This is drawn from the current position to the specified point as shown below.
Example
另一个示例中,我们尝试将 Stroke 过渡动画应用于三次曲线路径。在此,将该 2D 形状的描边设置为将其颜色从黑色更改为棕色。将此代码保存在名称为 CubicCurveToAnimation.java 的文件中。
In another example, we are trying to apply Stroke Transition Animation on Cubic Curve Path. Here, the stroke of said 2D shape is set to change its color from BLACK to BROWN. Save this code in a file with the name CubicCurveToAnimation.java.
import javafx.application.Application;
import javafx.animation.StrokeTransition;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.shape.CubicCurveTo;
import javafx.util.Duration;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.paint.Color;
public class CubicCurveToAnimation 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 CubicCurve
CubicCurveTo cubicCurveTo = new CubicCurveTo();
//Setting properties of the class CubicCurve
cubicCurveTo.setControlX1(400.0f);
cubicCurveTo.setControlY1(40.0f);
cubicCurveTo.setControlX2(175.0f);
cubicCurveTo.setControlY2(250.0f);
cubicCurveTo.setX(500.0f);
cubicCurveTo.setY(150.0f);
//Adding the path elements to Observable list of the Path class
path.getElements().add(moveTo);
path.getElements().add(cubicCurveTo);
//creating stroke transition
StrokeTransition st = new StrokeTransition();
//Setting the duration of the transition
st.setDuration(Duration.millis(1000));
//Setting the shape for the transition
st.setShape(path);
//Setting the fromValue property of the transition (color)
st.setFromValue(Color.BLACK);
//Setting the toValue property of the transition (color)
st.setToValue(Color.BROWN);
//Setting the cycle count for the transition
st.setCycleCount(50);
//Setting auto reverse value to false
st.setAutoReverse(false);
//Playing the animation
st.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 cubic curve");
//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 CubicCurveToAnimation.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls CubicCurveToAnimation
执行时,上述程序会生成一个 JavaFX 窗口,该窗口显示具有描边过渡的三次曲线。这是从当前位置绘制到指定点的,如下所示。
On executing, the above program generates a JavaFX window displaying a Cubic curve with the Stroke transition. This is drawn from the current position to the specified point as shown below.