Javafx 简明教程
JavaFX - LineTo Path Object
在几何中,线是最基本的图形之一,可以用不同的方式与其他图形结合以创建更复杂的结构。例如,多边形是一个由若干条线组成的封闭式几何图形。在 JavaFX 中,使用基本二维形状线来构建如此复杂的图形非常繁琐。因此,我们使用路径对象在二维平面上从一个位置绘制一条线到另一个位置。
In geometry, a line is one of the most basic figures, which can be combined with other figures in different ways to create more complex structures. For example, a polygon is a closed geomtrical figure that is composed of several lines. In JavaFX, using a primitive 2D shape line to construct such complex figures is quite cumbersome. Thus, we make use of path objects to draw a line from one position to another on a two dimensional plane.
使用 Path 类,我们可以从一个位置在平面上绘制一个 2D 形状到另一个位置以创建一个路径对象。之前,我们见过通过随意使用 2D 形状构建复杂图形的方式,比如奥运会符号、房子等。在本章中,我们将学习如何使用路径构造这种复杂图形。
Using the Path class, we can draw a 2D shape from one position to another on a plane to create a path object. Previously, we have seen constructing complex figures like Olympics symbols, House etc. by casually using 2D shapes. In this chapter, let us learn how to construct such complex figures using path.
JavaFX Path Object LineTo
路径元素 line 用于从当前位置绘制一条直线到指定坐标中的点。
The path element line is used to draw a straight line to a point in the specified coordinates from the current position.
它由一个名为 LineTo 的类表示。此类属于包 javafx.scene.shape 。
It is represented by a class named LineTo. This class belongs to the package javafx.scene.shape.
此类有 2 个 double 数据类型的属性:
This class has 2 properties of the double datatype namely −
-
X − The x coordinate of the point to which a line is to be drawn from the current position.
-
Y − The y coordinate of the point to which a line is to be drawn from the current position.
要绘制一条线,需要向这些属性传递值。这可以通过在实例化时按相同顺序将它们传递给此类的构造函数来完成。也可以通过使用它们各自的 setter 方法来完成。
To draw a line, you need to pass values to these properties. This can be either done 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 Line
若要从 JavaFX 中的当前位置绘制一条线到指定点,请遵循以下步骤。
To draw a line to a specified point from the current position in JavaFX, follow the steps given below.
Step 1: Creating a Path Class Object
创建 Java 类并继承包 javafx.application 的 Application 类,并在该类中实现 start() 方法。然后,按照以下方式在其中创建一个路径类对象。
Create a Java class and inherit the Application class of the package javafx.application and implement the start() method in this class. Then create a path class object within it as follows.
public class ClassName extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
//Creating a Path object
Path path = new Path();
}
}
Step 2: Setting the Path
创建 MoveTo 路径元素,并将 XY 坐标设置为线的起点坐标 (100, 150)。这可以使用 MoveTo 类的 setX() 和 setY() 方法来完成,如下所示。
Create the MoveTo path element and set the XY coordinates to 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 LineTo
通过实例化属于包 javafx.scene.shape 的名为 LineTo 的类,创建路径元素线,如下所示。
Create the path element line by instantiating the class named LineTo which belongs to the package javafx.scene.shape as follows.
//Creating an object of the class LineTo
LineTo lineTo = new LineTo();
Step 4: Setting Properties to the Line Element
指定要从当前位置绘制线的点的坐标。这可以通过使用它们各自的设置器方法设置 x 和 y 属性来完成,如下面的代码块所示。
Specify the coordinates of the point to which a line is to be drawn from the current position. This can be done by setting the properties x and y using their respective setter methods as shown in the following code block.
//Setting the Properties of the line element
lineTo.setX(500.0f);
lineTo.setY(150.0f);
Step 5: Adding Elements to the Observable List of the Path Class
将前面的步骤中创建的路径元素 MoveTo 和 LineTo 添加到 Path 类的可观察列表中,如下所示 −
Add the path elements MoveTo and LineTo created in the previous steps to the observable list of the Path class as shown below −
//Adding the path elements to Observable list of the Path class
path.getElements().add(moveTo);
path.getElements().add(lineTo);
Step 6: Launching Application
一旦创建了 LineTo 路径对象,请按照以下给定的步骤正确启动应用程序 −
Once the LineTo 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 类从当前点绘制一条直线到指定位置。将此代码保存在一个名为 LineToExample.java 的文件中。
The following program shows how to draw a straight line from the current point to a specified position using the class Path of JavaFX. Save this code in a file with the name LineToExample.java.
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.stage.Stage;
public class LineToExample extends Application {
@Override
public void start(Stage stage) {
//Creating a Path object
Path path = new Path();
//Moving to the starting point
MoveTo moveTo = new MoveTo();
moveTo.setX(100.0f);
moveTo.setY(150.0f);
//Instantiating the LineTo class
LineTo lineTo = new LineTo();
//Setting the Properties of the line element
lineTo.setX(500.0f);
lineTo.setY(150.0f);
//Adding the path elements to Observable list of the Path class
path.getElements().add(moveTo);
path.getElements().add(lineTo);
//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 Line");
//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 LineToExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls LineToExample
执行后,上述程序将生成一个 JavaFX 窗口,显示一条从当前位置绘制到指定位置的直线,如下所示。
On executing, the above program generates a JavaFX window displaying a straight line, which is drawn from the current position to the specified point, as shown below.
Example 2
不仅仅是单线,您还可以向 JavaFX 应用程序添加多条线,以创建更复杂的形状。在此示例中,我们将尝试绘制一个简单的十字形。将此代码保存在一个名为 LineToCross.java 的文件中。
Not just a single line, you can also add multiple lines to the JavaFX application in order to create more complex shapes. In this example, we will try to draw a simple cross figure. Save this code in a file with the name LineToCross.java.
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.stage.Stage;
public class LineToCross extends Application {
@Override
public void start(Stage stage) {
//Creating a Path object
Path path = new Path();
//Moving to the starting point
MoveTo moveTo = new MoveTo();
moveTo.setX(100.0f);
moveTo.setY(150.0f);
//Instantiating the LineTo class
LineTo lineTo = new LineTo();
//Setting the Properties of the line element
lineTo.setX(500.0f);
lineTo.setY(150.0f);
//Adding the path elements to Observable list of the Path class
path.getElements().add(moveTo);
path.getElements().add(lineTo);
//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 Line");
//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 LineToCross.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls LineToCross
执行后,上述程序将生成一个 JavaFX 窗口,显示一个使用两条不同直线绘制的十字形结构,如下所示。
On executing, the above program generates a JavaFX window displays a cross structure, which is drawn using two different lines, as shown below.