Javafx 简明教程
JavaFX - Drawing an Arc
在简单几何中,圆弧被定义为椭圆或圆的圆周的一部分。或者,简单地说,它是一个连接两个端点的曲线。圆弧还圈住圆心的一个小于等于 360 度的角度。它由以下属性描述 −
An arc in simple geometry is defined as a portion of a circumference of an ellipse or a circle. Or, simply put, it is a curve that is joins two end points. An arc also encloses an angle less than or equal to 360 degrees at the centre of the circle. It is described by the following properties −
-
length − The distance along the arc.
-
angle − The angle the curve makes at the centre of the circle.
-
radiusX − The width of the full Ellipse of which the current arc is a part of.
-
radiusY − The height of the full Ellipse of which the current arc is a part of.
如果 radiusX 和 radiusY 相同,那么圆弧就是圆周一部分。
If both radiusX and radiusY are same, then the arc is a part of a circle circumference.
Arc in JavaFX
在 JavaFX 中,圆弧由名为 Arc 的类表示。此类属于 javafx.scene.shape 包。
In JavaFX, an arc is represented by a class named Arc. This class belongs to the package javafx.scene.shape.
通过实例化此类,您可以在 JavaFX 中创建圆弧节点。
By instantiating this class, you can create an arc node in JavaFX.
此类有一些浮点数据类型属性,即 −
This class has a few properties of the double datatype namely −
-
centerX − The x coordinate of the center of the arc.
-
centerY − The y coordinate of the center of the arc.
-
radiusX − The width of the full ellipse of which the current arc is a part of.
-
radiusY − The height of the full ellipse of which the current arc is a part of.
-
startAngle − The starting angle of the arc in degrees.
-
length − The angular extent of the arc in degrees.
为了绘制圆弧,您需要向这些属性传递值,可以通过在实例化时按照相同顺序将值传递给此类的构造函数或使用其各自的 setter 方法来传递。
To draw an arc, 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, or, by using their respective setter methods.
Types of Arc
在 JavaFX 中,您可以绘制三种类型的圆弧,即 −
In JavaFX, you can draw three kinds of arc’s namely −
-
Open − An arc which is not closed at all is known as an open arc.
-
Chord − A chord is a type of an arc which is closed by straight line.
-
Round − The Round arc is an arc which is closed by joining the starting and ending point to the center of the ellipse.
你可以使用 setType() 方法通过传递以下任意属性来设置弧类型 − ArcType.OPEN, ArcType.CHORD, ArcType.Round 。
You can set the type of the arc using the method setType() by passing any of the following properties − ArcType.OPEN, ArcType.CHORD, ArcType.Round.
Steps to Draw Arc
要在 JavaFX 中绘制弧,请遵循以下步骤。
To Draw an arc in JavaFX, follow the steps given below.
Step 1: Creating an Arc
你可以通过实例化名为 Arc 的类来在 JavaFX 中创建弧,该类属于一个 javafx.scene.shape 包。你可以在 Application 类的 start() 方法中实例化此类,如下所示。
You can create an arc in JavaFX by instantiating the class named Arc which belongs to a package javafx.scene.shape. You can instantiate this class inside the start() method of Application class as shown below.
public class ClassName extends Application {
public void start(Stage primaryStage) throws Exception {
//Creating an object of the class Arc
Arc arc = new Arc();
}
}
Step 2: Setting Properties to the Arc
使用其各自的 setter 方法(如以下代码块中所示)指定此弧所属椭圆的中心 x、y 坐标(包括:radiusX、radiusY、起始角度和弧长)。
Specify the x, y coordinates of the center of the Ellipse (of which this arc is a part of). These coordinates include – radiusX, radiusY, start angle and length of the arc using their respective setter methods as shown in the following code block.
//Setting the properties of the arc
arc.setCenterX(300.0f);
arc.setCenterY(150.0f);
arc.setRadiusX(90.0f);
arc.setRadiusY(90.0f);
arc.setStartAngle(40.0f);
arc.setLength(239.0f);
Step 3: Setting the Type of the Arc
你也可以通过 setType() 方法设置弧类型(圆弧、和弦、开弧),如下面的代码块所示。
You can also set the type of the arc (round, chord open) by using the setType() method as shown in the following code block.
//Setting the type of the arc
arc.setType(ArcType.ROUND);
Step 4: Adding Arc Object to Group
在 start() 方法中,通过将上一步创建的弧对象作为其构造函数的参数值来实例化一个 Group 类 −
In the start() method, instantiate a Group class by passing the arc object, which was created in the previous step, as a parameter value to its constructor −
Group root = new Group(arc);
Step 5: 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
以下是生成弧的程序。将此代码保存到名为 ArcExample.java 的文件中。
Following is a program which generates an arc. Save this code in a file with the name ArcExample.java.
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
public class ArcExample extends Application {
public void start(Stage stage) {
//Drawing an arc
Arc arc = new Arc();
//Setting the properties of the arc
arc.setCenterX(300.0f);
arc.setCenterY(150.0f);
arc.setRadiusX(90.0f);
arc.setRadiusY(90.0f);
arc.setStartAngle(40.0f);
arc.setLength(239.0f);
//Setting the type of the arc
arc.setType(ArcType.ROUND);
//Creating a Group object
Group root = new Group(arc);
//Creating a scene object
Scene scene = new Scene(root, 600, 300);
//Setting title to the Stage
stage.setTitle("Drawing an Arc");
//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 ArcExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls ArcExample
运行后,上述程序会生成一个 JavaFX 窗口,其中显示一个弧,如下图所示。
On executing, the above program generates a JavaFX window displaying an arc as shown in the following screenshot.
Example
让我们尝试在以下示例中绘制另一个类型为“开弧”的弧。将此代码保存到名为 ArcOpen.java 的文件中。
Let us try to draw another arc of type "Open" in the following example. Save this code in a file with the name ArcOpen.java.
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
public class ArcOpen extends Application {
public void start(Stage stage) {
//Drawing an arc
Arc arc = new Arc();
//Setting the properties of the arc
arc.setCenterX(300.0f);
arc.setCenterY(150.0f);
arc.setRadiusX(90.0f);
arc.setRadiusY(90.0f);
arc.setStartAngle(40.0f);
arc.setLength(239.0f);
//Setting the type of the arc
arc.setType(ArcType.OPEN);
//Creating a Group object
Group root = new Group(arc);
//Creating a scene object
Scene scene = new Scene(root, 600, 300);
//Setting title to the Stage
stage.setTitle("Drawing an Open Arc");
//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 ArcOpen.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls ArcOpen
运行后,上述程序会生成一个 JavaFX 窗口,其中显示一个弧,如下图所示。
On executing, the above program generates a JavaFX window displaying an arc as shown in the following screenshot.
Example
现在,我们将尝试在以下示例中绘制另一个类型为“和弦”的弧。将此代码保存到名为 ArcChord.java 的文件中。
Now we will try to draw another arc of type "Chord" in the following example. Save this code in a file with the name ArcChord.java.
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
public class ArcChord extends Application {
public void start(Stage stage) {
//Drawing an arc
Arc arc = new Arc();
//Setting the properties of the arc
arc.setCenterX(300.0f);
arc.setCenterY(150.0f);
arc.setRadiusX(90.0f);
arc.setRadiusY(90.0f);
arc.setStartAngle(40.0f);
arc.setLength(239.0f);
//Setting the type of the arc
arc.setType(ArcType.CHORD);
//Creating a Group object
Group root = new Group(arc);
//Creating a scene object
Scene scene = new Scene(root, 600, 300);
//Setting title to the Stage
stage.setTitle("Drawing a Chord Arc");
//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 ArcChord.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls ArcChord
运行后,上述程序会生成一个 JavaFX 窗口,其中显示一个弧,如下图所示。
On executing, the above program generates a JavaFX window displaying an arc as shown in the following screenshot.