Javafx 简明教程
JavaFX - VLineTo Path Object
路径元素 VLineTo 用于从当前位置绘制一条垂直线到指定坐标中的一个点。
The path element VLineTo is used to draw a vertical line to a point in the specified coordinates from the current position.
它由名为 VLineTo 的类表示。此类属于包 javafx.scene.shape 。
It is represented by a class named VLineTo. This class belongs to the package javafx.scene.shape.
这个类的属性具有 double 数据类型,即:
This class has a property of the double datatype namely −
-
Y − The y coordinate of the point to which a vertical is to be drawn from the current position.
要绘制路径元素垂直线,你需要向此属性传递一个值。可以通过在实例化时将它传递给此类的构造函数来完成此操作;或者,通过使用其各自的设置器方法来完成。
To draw the path element vertical line, you need to pass a value to this property. This can be done either by passing it to the constructor of this class at the time of instantiation; Or, by using its respective setter methods.
Steps to draw PathElement Vertical Line
要在 JavaFX 中从当前位置绘制一条到指定点的垂直线,请按照以下步骤操作。
To draw a vertical line to a specified point from the current position in JavaFX, follow the steps given below.
Step 1: Creating a Class
创建 Java 类并继承包 javafx.application 的 Application 类并实现此类的 start() 方法。然后通过实例化 Path 类创建 path 对象,如下所示。
Create a Java class and inherit the Application class of the package javafx.application and implement the start() method of this class. Then create a path object by instantiating the Path class 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: Create a Path
创建 MoveTo 路径元素并将 XY 坐标设置为线段的起点,为坐标 (100, 150)。可以通过使用类 MoveTo 的方法 setX() 和 setY() 来实现,如下所示。
Create the MoveTo path element and set 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 VLineTo
通过实例化名为 VLineTo 的类创建路径元素垂直线,该类属于包 javafx.scene.shape ,如下所示。
Create the path element vertical line by instantiating the class named VLineTo, which belongs to the package javafx.scene.shape as follows.
//Creating an object of the class VLineTo
VLineTo vLineTo = new VLineTo();
Step 4:Setting Properties to the Element Vertical Line
指定从当前位置绘制垂直线的点的坐标。可以通过使用其各自的设置器方法设置属性 x 和 y 来实现,如下面的代码块所示。
Specify the coordinates of the point to which a vertical 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 vertical line element
lineTo.setX(500.0f);
lineTo.setY(150.0f);
Step 5: Adding Elements to the Observable List of the Path Class
将前面步骤中创建的 MoveTo 和 VlineTo 路径元素添加到 Path 类的 observable 列表中,如下所示 −
Add the path elements MoveTo and VlineTo 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(VlineTo);
Step 6: Launching Application
创建 VLineTo 路径对象后,按照给定的步骤正确启动应用程序 −
Once the VLineTo 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 类从当前点绘制到指定位置的垂直线。将以下代码保存在名为 VLineToExample.java 的文件中。
Following is a program which draws a vertical line from the current point to a specified position using the class Path of JavaFX. Save this code in a file with the name − VLineToExample.java.
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.shape.VLineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
public class VLineToExample extends Application {
@Override
public void start(Stage stage) {
//Creating an object of the Path class
Path path = new Path();
//Moving to the starting point
MoveTo moveTo = new MoveTo();
moveTo.setX(100.0);
moveTo.setY(150.0);
//Instantiating the VLineTo class
VLineTo vLineTo = new VLineTo();
//Setting the properties of the path element vertical line
vLineTo.setY(10.0);
//Adding the path elements to Observable list of the Path class
path.getElements().add(moveTo);
path.getElements().add(vLineTo);
//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 vertical 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 VLineToExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls VLineToExample
执行后,上面程序会生成一个 JavaFX 窗口,显示一条垂直线,该线从当前位置绘制到指定点,如下所示。
On executing, the above program generates a JavaFX window displaying a vertical line, which is drawn from the current position to the specified point, as shown below.
Example 2
以下程序使用 JavaFX 的 Path 类使用垂直线和水平线绘制一个矩形。将以下代码保存在名为 VLineToRectangle.java 的文件中。
Following is a program which draws a rectangle using both vertical lines and horizontal lines using the class Path of JavaFX. Save this code in a file with the name − VLineToRectangle.java.
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.shape.HLineTo;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
public class VLineToRectangle extends Application {
@Override
public void start(Stage stage) {
//Creating an object of the Path class
Path path = new Path();
//Drawing a triangular path
MoveTo moveTo = new MoveTo();
moveTo.setX(200.0);
moveTo.setY(150.0);
HLineTo hLineTo = new HLineTo();
hLineTo.setX(100.0);
MoveTo moveTo2 = new MoveTo();
moveTo2.setX(100.0);
moveTo2.setY(150.0);
LineTo lineTo = new LineTo();
lineTo.setX(150.0);
lineTo.setY(50.0);
MoveTo moveTo3 = new MoveTo();
moveTo3.setX(150.0);
moveTo3.setY(50.0);
LineTo lineTo2 = new LineTo();
lineTo2.setX(200.0);
lineTo2.setY(150.0);
//Adding the path elements to Observable list of the Path class
path.getElements().add(moveTo);
path.getElements().add(hLineTo);
path.getElements().add(moveTo2);
path.getElements().add(lineTo);
path.getElements().add(moveTo3);
path.getElements().add(lineTo2);
//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 Triangular 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 VLineToRectangle.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls VLineToRectangle
执行后,上面程序会生成一个 JavaFX 窗口,显示一个矩形。
On executing, the above program generates a JavaFX window displaying a rectangle.