Javafx 简明教程
JavaFX - Drawing a Polygon
多边形在地理上被定义为由许多共面的线段从头到尾连接而形成的封闭形状。“多边形”一词源自希腊语,“poly”表示“多”,“gonia”表示“角”。
Polygon is geometrically defined as a closed shape formed by a number of coplanar line segments connected from end to end. The name "polygon" is derived from the Greek words, "poly" meaning "many" and "gonia" meaning "angles".
多边形由两个参数描述,即其边的长度和其内角的度量。
A polygon is described by two parameters, namely, the length of its sides and the measures of its interior angles.
根据边数和角数,有多种类型的多边形。它们如下所列 −
There are various types of Polygons based on the numbers of sides and angles. They are listed below −
-
If a polygon has three sides, then it is referred to as a triangle.
-
If a polygon has four sides, then it is known as quadrilateral. Shapes like rectangles, squares, parallelogram etc., are all types of quadrilaterals.
-
If a polygon has five sides, then it is known as a pentagon. Similarly, the polygon with six sides is called hexagon, seven sides is heptagon, eight sides is octagon etc.
Polygon in JavaFX
在 JavaFX 中,多边形由名为 Polygon 的类表示。该类属于包 javafx.scene.shape.
In JavaFX, a polygon 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 a polygon node in JavaFX. You need to pass the x, y coordinates of the points by which the polygon 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 −
Polygon polygon = new Polygon(doubleArray);
或者,通过如下使用 getPoints() 方法 −
Or, by using the getPoints() method as follows −
polygon.getPoints().addAll(new Double[]{ List of XY coordinates separated by commas });
Steps to Draw Polygon
要在 JavaFX 中绘制多边形,请按照以下给出的步骤进行操作。
To draw a polygon in JavaFX, follow the steps given below.
Step 1: Creating a Polygon
在 start() 方法内,通过实例化属于 javafx.scene.shape 包中的 Polygon 类来创建 JavaFX 中的多边形。可以按照如下方式实例化此类。
Create a polygon in JavaFX by instantiating the class named Polygon which belongs to a package javafx.scene.shape within the start() method. You can instantiate this class as follows.
public class ClassName extends Application {
public void start(Stage primaryStage) throws Exception {
//Creating an object of the class Polygon
Polygon hexagon = new Polygon();
}
}
Step 2: Setting Properties to the Polygon
使用 Polygon 类的 getPoints() 方法,指定一个双数组,其中包含必需的多边形(本例中的六边形)各点的 XY 坐标,用逗号分隔,如下所示。
Specify a double array holding the XY coordinates of the points of the required polygon (hexagon in this example) separated by commas, using the getPoints() method of the Polygon class, as follows.
//Adding coordinates to the hexagon
hexagon.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 Polygon Object to Group
在 start() 方法中,通过将上一步创建的多边形对象作为参数值传递给其构造函数,实例化属于 javafx.scene 包的 Group 类。
In the start() method, instantiate the class named Group, which belongs to the package javafx.scene, by passing the Polygon object, created in the previous step, as a parameter value to its constructor.
Group root = new Group(hexagon);
Step 4: Launching Application
创建二维对象后,请按照下面给出的步骤正确启动应用程序:
Once the 2D 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 的多边形(六边形)的程序。将此代码保存在名为 PolygonExample.java 的文件中。
Following is a program which generates a Polygon (hexagon) using JavaFX. Save this code in a file with the name PolygonExample.java.
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Polygon;
import javafx.stage.Stage;
public class PolygonExample extends Application {
@Override
public void start(Stage stage) {
//Creating a Polygon
Polygon polygon = new Polygon();
//Adding coordinates to the polygon
polygon.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(polygon);
//Creating a scene object
Scene scene = new Scene(root, 600, 300);
//Setting title to the Stage
stage.setTitle("Drawing a Polygon");
//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 PolygonExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls PolygonExample
执行后,以上程序将生成一个 JavaFX 窗口,显示一个如下所示的多边形。
On executing, the above program generates a JavaFX window displaying a polygon as shown below.
Example
现在,让我们尝试绘制六边形之外的多边形,比如菱形。将此代码保存在名为 RhombusExample.java 的文件中。
Now, let us try to draw polygons other than a hexagon, say a rhombus. Save this code in a file with the name RhombusExample.java.
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Polygon;
import javafx.stage.Stage;
public class RhombusExample extends Application {
@Override
public void start(Stage stage) {
//Creating a Polygon
Polygon polygon = new Polygon();
//Adding coordinates to the polygon
polygon.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(polygon);
//Creating a scene object
Scene scene = new Scene(root, 600, 300);
//Setting title to the Stage
stage.setTitle("Drawing a Polygon");
//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 RhombusExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls RhombusExample
执行后,以上程序将生成一个 JavaFX 窗口,显示一个如下所示的菱形。
On executing, the above program generates a JavaFX window displaying a rhombus as shown below.