多读书多实践,勤思考善领悟

JavaFX 2D Shapes ArcType形状弧的类型

本文于1666天之前发表,文中内容可能已经过时。

在JavaFX中,你可以绘制三种弧形 -

  • Open 打开 - 完全没有关闭的弧被称为开放弧。
  • Chord 弦 - 弦是一种由直线封闭的弧。
  • Round 圆形 - 圆弧是通过将起点和终点连接到椭圆中心而闭合的弧。

您可以使用方法setType()通过传递以下任何属性来设置弧的类型- ArcType.OPEN,ArcType.CHORD,ArcType.Round

画弧的步骤

要在JavaFX中绘制弧,请按照下面给出的步骤操作。

第1步:创建一个类

创建一个Java类并继承包javafx.applicationApplication类,并按如下方式实现此类的start()方法。

1
2
3
4
5
public class ClassName extends Application {  
@Override
public void start(Stage primaryStage) throws Exception {
}
}

第2步:创建弧

您可以通过实例化属于包javafx.scene.shape的名为Arc的类在JavaFX中创建弧。您可以实例化此类,如下所示。

1
2
//Creating an object of the class Arc         
Arc arc = new Arc();

第3步:将属性设置为弧

指定椭圆中心的x,y坐标(此弧是其中的一部分)。这些坐标包括 - radiusX,radiusY,起始角度和弧的长度,使用它们各自的setter方法,如下面的代码块所示。

您还可以使用setType()方法设置弧的类型(圆形,和弦打开)。

1
2
3
4
5
6
7
8
//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);
arc.setType(ArcType.ROUND);

第4步:设置弧的类型

您可以使用方法setType()设置弧的类型,如以下代码块所示。

1
2
//Setting the type of the arc 
arc.setType(ArcType.ROUND);

第5步:创建组对象

start()方法中,通过实例化名Group的类来创建组对象,该类属于包javafx.scene

将上一步中创建的Arc(节点)对象作为参数传递给Group类的构造函数。这应该是为了将它添加到组,如下所示 -

1
Group root = new Group(arc);

第6步:创建场景对象

通过实例化属于包javafx.scene的名为Scene的类来创建场景。对此类传递上一步中创建的Group对象(root)

除了根对象之外,您还可以传递两个表示屏幕高度和宽度的双参数以及Group类的对象,如下所示。

1
Scene scene = new Scene(group ,600, 300);

第7步:设置舞台的标题

您可以使用Stage类setTitle()方法将标题设置为舞台。所述primaryStage是被传递给场景类作为参数的开始方法Stage对象。

使用primaryStage对象,将场景标题设置为Sample Application,如下所示。

1
primaryStage.setTitle("Sample Application");

第8步:将场景添加到舞台

您可以使用名为Stage的类的方法setScene()将Scene对象添加到舞台。使用此方法添加前面步骤中准备的Scene对象,如下所示。

1
primaryStage.setScene(scene);

第9步:显示舞台的内容

显示场景的使用命名的方法的内容显示()的的阶段类,如下所示。

1
primaryStage.show();

第10步:启动应用程序

通过从main方法调用Application类的静态方法launch()来启动JavaFX应用程序,如下所示。

1
2
3
public static void main(String args[]){   
launch(args);
}

以下是生成弧的程序。将此代码保存在名为ArcExample.java的文件中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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 {
@Override
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文件。

1
2
javac ArcExample.java 
java ArcExample

在执行时,上述程序生成一个显示弧的JavaFX窗口,如下面的屏幕截图所示。