Javafx 简明教程

JavaFX - Drawing a Line

通常, line 是一个在多维平面上无限长的几何结构(比如说,XY 平面)。此几何图形没有任何起点和终点;也没有诸如宽度、深度之类的可度量尺寸。即使一条线是一个一维物体,它仍然可以在二个或更高维度的平面上存在。

Generally, a line is a geometrical structure which is infinitely long on a multi-dimensional plane (Say, an XY plane). This geometrical figure does not have any start and end points; neither does it have measurable dimensions like width, depth etc. Even though a line is a one-dimensional object, it can still exist in two or higher dimensional planes.

然而,此线的一段具有起点和终点,称为“线段”,在日常几何中使用;这也是我们在本章中学习使用 JavaFX 创建的图形。它如以下图像所示绘制。

However, a segment of this line with both start and end points, called the "line segment" is used in everyday geometry; which is also the figure that we learn how to create using JavaFX, in this chapter. It is drawn as shown in the image below.

line

Line in JavaFX

在 JavaFX 中,线或线段由名为 Line 的类表示。此类属于包 javafx.scene.shape 。通过实例化此类,您可以在 JavaFX 中创建线节点。

In JavaFX, a line, or a line segment, is represented by a class named Line. This class belongs to the package javafx.scene.shape. By instantiating this class, you can create a line node in JavaFX.

此类具有 4 个双数据类型的属性,即 −

This class has 4 properties of the double datatype namely −

  1. startX − The x coordinate of the start point of the line.

  2. startY − The y coordinate of the start point of the line.

  3. endX − The x coordinate of the end point of the line.

  4. endY − The y coordinate of the end point of the line.

若要绘制一条线,需要向这些属性传递值,方法是在实例化时按同一顺序将它们传递给此类的构造函数中。

To draw a line, you need to pass values to these properties, either by passing them to the constructor of this class, in the same order, at the time of instantiation.

Steps to Draw a Line in JavaFX

按照下面给出的步骤在 JavaFX 中绘制一条线。

Follow the steps given below to Draw a Line in JavaFX.

Step 1: Creating a Line

可以通过实例化名为 Line 的类来在 JavaFX 中创建一条线,该类属于包 javafx.scene.shape 。按如下所示来实例化此类:

You can create a line in JavaFX by instantiating the class named Line which belongs to a package javafx.scene.shape. Instantiate this class as shown below −

//Creating a line object
Line line = new Line();

请注意,实施 JavaFX 图形的全部代码(包括 Line 类的实例化)都必须编写在 Application 类的方法 start() 中,如下所示:

Note that the entire code to implement JavaFX graphics, which includes the instantiation of Line class, must be written within the start() method of Application class, as shown below −

public class ClassName extends Application {
   public void start(Stage primaryStage) throws Exception {
      // Write code here
      Line line = new Line();
   }
}

Step 2: Setting Properties to the Line

通过使用各自的 setter 方法,按如下代码块中所示来设置 startX、startY、endX 和 endY 属性,指定在 XY 平面中绘制线的坐标。

Specify the coordinates to draw the line on an XY plane by setting the properties startX, startY, endX and endY, using their respective setter methods as shown in the following code block.

line.setStartX(100.0);
line.setStartY(150.0);
line.setEndX(500.0);
line.setEndY(150.0);

Step 3: Adding Line Object to Group

通过如下实例化包 javafx.sceneGroup 类,将其 Line 对象作为参数值传递给其构造函数:

Instantiate the Group class of package javafx.scene, by passing the Line object as a parameter value to its constructor as follows −

Group root = new Group(line);

Step 4: Launching Application

创建二维对象后,请按照下面给出的步骤正确启动应用程序:

Once the 2D object is created, follow the given steps below to launch the application properly −

  1. 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.

  2. Then, set the title to the stage using the setTitle() method of the Stage class.

  3. Now, a Scene object is added to the stage using the setScene() method of the class named Stage.

  4. Display the contents of the scene using the method named show().

  5. Lastly, the application is launched with the help of the launch() method.

Example

以下是使用 JavaFX 生成的直线程序。将此代码保存到名为 DrawingLine.java 的文件中。

Following is the program which generates a straight line using JavaFX. Save this code in a file with the name DrawingLine.java.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class DrawingLine extends Application{
   @Override
   public void start(Stage stage) {
      //Creating a line object
      Line line = new Line();

      //Setting the properties to a line
      line.setStartX(100.0);
      line.setStartY(150.0);
      line.setEndX(500.0);
      line.setEndY(150.0);

      //Creating a Group
      Group root = new Group(line);

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

      //Setting title to the scene
      stage.setTitle("Sample application");

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

      //Displaying the contents of a scene
      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 DrawingLine.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls DrawingLine

执行时,上述程序生成一个 JavaFX 窗口,显示一条直线,如下所示。

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

drawing line

Drawing Multiple Lines

还可以通过使用 addAll() 函数将所有线组合在一起,在单一应用程序中绘制多条线。以下是说明示例。在这里,我们使用四条线绘制一个正方形,并将背景颜色着为“粉红”。将此代码保存到名为 DrawingMultipleLines.java 的文件中。

You can also draw multiple lines on a single application by grouping all the lines together using addAll() function. An example demonstrating the same is as follows. In here, we are drawing a square using four lines and coloring the background "pink". Save this code in a file with the name DrawingMultipleLines.java.

Example

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class DrawingMultipleLines extends Application{
   @Override
   public void start(Stage stage) {
      //Creating a line object
      Line line1 = new Line();

      //Setting the properties to a line
      line1.setStartX(100.0);
      line1.setStartY(150.0);
      line1.setEndX(200.0);
      line1.setEndY(150.0);

	  // Setting Properties to other lines
	  // without setter methods
	  Line line2 = new Line(200, 150, 200, 250);
	  Line line3 = new Line(200, 250, 100, 250);
	  Line line4 = new Line(100, 250, 100, 150);

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

      root.getChildren().addAll(line1,line2,line3,line4);

      //Creating a Scene
      Scene scene = new Scene(root, 400, 400, Color.PINK);

      //Setting title to the scene
      stage.setTitle("Sample application");

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

      //Displaying the contents of a scene
      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 DrawingMultipleLines.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls DrawingMultipleLines

上面程序的输出窗口将按如下所示获得。

The output window of the program above will be obtained as shown below.

drawing multiple lines