多边形是JavaFX库的一部分。多边形类使用给定的x和y坐标集创建多边形。多边形类继承形状类。
null
该类的构造函数是:
- 多边形() :创建没有定义点(顶点)坐标集的空多边形
- 多边形(双点[]) 使用一组定义的点(顶点)坐标创建多边形
常用方法:
方法 | 解释 |
---|---|
getPoints() | 获取多边形顶点的坐标。 |
设置填充(油漆p) | 设置多边形的填充 |
以下程序将演示JavaFX的多边形类:
- 程序创建具有给定顶点集的多边形 :此程序创建一个由名称Polygon指示的多边形。多边形顶点的坐标作为参数传递。多边形将在场景中创建,而场景又将位于舞台内。函数setTitle()用于为舞台提供标题。然后创建一个组,并附着多边形。这群人依附于现场。最后,调用show()方法来显示最终结果。
// Java Program to create a polygon with a given set of vertices
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.Button;
import
javafx.scene.layout.*;
import
javafx.scene.paint.Color;
import
javafx.scene.shape.Polygon;
import
javafx.scene.control.*;
import
javafx.stage.Stage;
import
javafx.scene.Group;
public
class
polygon_0
extends
Application {
// launch the application
public
void
start(Stage stage)
{
// set title for the stage
stage.setTitle(
"creating polygon"
);
// coordinates of the points of polygon
double
points[] = {
10
.0d,
140
.0d,
30
.0d,
110
.0d,
40
.0d,
50
.0d,
50
.0d,
40
.0d,
110
.0d,
30
.0d,
140
.0d,
10
.0d };
// create a polygon
Polygon polygon =
new
Polygon(points);
// create a Group
Group group =
new
Group(polygon);
// create a scene
Scene scene =
new
Scene(group,
500
,
300
);
// set the scene
stage.setScene(scene);
stage.show();
}
public
static
void
main(String args[])
{
// launch the application
launch(args);
}
}
输出:
- 该程序用于创建具有给定顶点集和指定填充的多边形 :此程序创建一个由名称Polygon指示的多边形。多边形顶点的坐标作为参数传递。函数set Fill()用于设置多边形的填充。多边形将在场景中创建,而场景又将位于舞台内。函数setTitle()用于为舞台提供标题。然后创建一个组,并附着多边形。这群人依附于现场。最后,调用show()方法来显示最终结果。
// Java Program to create a polygon with a
// given set of vertices and specified fill
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.Button;
import
javafx.scene.layout.*;
import
javafx.scene.paint.Color;
import
javafx.scene.shape.Polygon;
import
javafx.scene.control.*;
import
javafx.stage.Stage;
import
javafx.scene.Group;
public
class
polygon_1
extends
Application {
// launch the application
public
void
start(Stage stage)
{
// set title for the stage
stage.setTitle(
"creating polygon"
);
// coordinates of the points of polygon
double
points[] = {
10
.0d,
140
.0d,
30
.0d,
110
.0d,
40
.0d,
50
.0d,
50
.0d,
40
.0d,
110
.0d,
30
.0d,
140
.0d,
10
.0d };
// create a polygon
Polygon polygon =
new
Polygon(points);
// set fill for the polygon
polygon.setFill(Color.BLUE);
// create a Group
Group group =
new
Group(polygon);
// create a scene
Scene scene =
new
Scene(group,
500
,
300
);
// set the scene
stage.setScene(scene);
stage.show();
}
public
static
void
main(String args[])
{
// launch the application
launch(args);
}
}
输出:
注: 以上程序可能无法在联机IDE中运行,请使用脱机IDE。
参考: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/shape/Polygon.html
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END