Javafx 简明教程

JavaFX - Drawing a Polyline

折线被定义为通过结合多个具有某些顶点的线段形成的一个连续结构。这些顶点被称为端点。因此,可以通过指定这些端点来构造折线,这些线段将通过这些端点绘制。

Polyline is defined as a continuous structure that is formed by combining multiple line segments with some vertices. These vertices are addressed as endpoints. Thus, a polyline can be constructed by specifying these endpoints through which these line segments are to be drawn.

它包含各种属性,如 Points、width、color、start 和 end caps、type of join、stroke pattern 等。折线与多边形相同,但折线在末尾不闭合。或者由一个或多个线段组成的连续线。

It consists of various properties like Points, width, color, start and end caps, type of join, stroke pattern etc. A Polyline is same as a polygon except that a polyline is not closed in the end. Or, continuous line composed of one or more line segments.

简而言之,我们可以说多边形是一个由共面线段形成的开放图形。

In short, we can say a polygon is an open figure formed by coplanar line segments.

polyline

Polyline in JavaFX

在 JavaFX 中,折线由名为 Polygon 的类表示。该类属于 javafx.scene.shape. 包。

In JavaFX, a Polyline is represented by a class named Polygon. This class belongs to the package javafx.scene.shape..

通过实例化该类,可以在 JavaFX 中创建折线节点。您需要以双数组的形式传递折线应定义的点的 x、y 坐标。

By instantiating this class, you can create polyline node in JavaFX. You need to pass the x, y coordinates of the points by which the polyline should be defined in the form of a double array.

您可以将双数组作为该类的构造函数的参数传递,如下所示 −

You can pass the double array as a parameter of the constructor of this class as shown below −

Polyline polyline = new Polyline(doubleArray);

或者,通过如下使用 getPoints() 方法 −

Or, by using the getPoints() method as follows −

polyline.getPoints().addAll(new Double[]{List of XY coordinates separated by commas });

Steps to Draw Polyline

要在 JavaFX 中绘制折线,请按照以下步骤操作。

To Draw a Polyline in JavaFX, follow the steps given below.

Step 1: Creating a Polyline

可以通过实例化名为 Line 的类在 JavaFX 中创建一条线,该类属于 javafx.scene.shape 包。您可以按照如下方式在 start() 方法中实例化此类。

You can create a line in JavaFX by instantiating the class named Line which belongs to a package javafx.scene.shape. You can instantiate this class in start() method as follows.

public class ClassName extends Application {
   @Override
   public void start(Stage primaryStage) throws Exception {
      //Creating an object of the class Polyline
      Polyline polyline = new Polyline();
   }
}

Step 2: Setting Properties to the Polyline

指定包含所需折线(本例中的六角形)的点的 XY 坐标的双数组,并用逗号分隔。您可以使用 Polyline 类的 getPoints() 方法执行此操作,如下面的代码块所示。

Specify a double array holding the XY coordinates of the points of the required polyline (hexagon in this example) separated by commas. You can do this by using the getPoints() method of the Polyline class as shown in the following code block.

//Adding coordinates to the hexagon
polyline.getPoints().addAll(new Double[]{
   200.0, 50.0,
   400.0, 50.0,
   450.0, 150.0,
   400.0, 250.0,
   200.0, 250.0,
   150.0, 150.0,
});

Step 3: Adding Polyline Object to Group

start() 方法中,通过将上一步创建的折线对象作为参数值传递给其构造函数,实例化名为 Group 的类,该类属于 javafx.scene 包。

In the start() method, instantiate the class named Group, which belongs to the package javafx.scene, by passing the Polyline object, created in the previous step, as a parameter value to its constructor.

Group root = new Group(polyline);

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 生成折线的程序。将此代码保存在名为 PolylineExample1.java 的文件中。

Following is a program which generates a polyline using JavaFX. Save this code in a file with the name PolylineExample1.java.

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

public class PolylineExample1 extends Application {
   @Override
   public void start(Stage stage) {
      //Creating a polyline
      Polyline polyline = new Polyline();

      //Adding coordinates to the polygon
      polyline.getPoints().addAll(new Double[]{
         200.0, 50.0,
         400.0, 50.0,
         450.0, 150.0,
         400.0, 250.0,
         200.0, 250.0,
         150.0, 150.0,
      });

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

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

      //Setting title to the Stage
      stage.setTitle("Drawing a Polyline");

      //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 PolylineExample1.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls PolylineExample1

在执行时,以上程序生成了一个 JavaFX 窗口,显示一个如下图所示的折线。

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

drawing polyline

Example

在此示例中,我们尝试绘制一个具有 4 个顶点的折线。您可以将文件名保存为 PolylineExample2.java

In this example, let us try to draw a polyline of 4 vertices. You can save the file name as PolylineExample2.java.

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

public class PolylineExample2 extends Application {
   @Override
   public void start(Stage stage) {
      //Creating a Polyline
      Polyline polyline = new Polyline();

      //Adding coordinates to the polygon
      polyline.getPoints().addAll(new Double[]{
         300.0, 50.0,
         450.0, 150.0,
         300.0, 250.0,
         150.0, 150.0,
      });

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

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

      //Setting title to the Stage
      stage.setTitle("Drawing a Polyline");

      //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 PolylineExample2.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls PolylineExample2

执行后,上述程序会生成一个 JavaFX 窗口,该窗口显示一个由 4 个顶点组成的折线,如下所示:

On executing, the above program generates a JavaFX window displaying a 4 vertices Polyline as shown below.

drawing polyline2