Polygon is a part of the JavaFX library. Polygon class creates a polygon with the given set of x and y coordinates. Polygon class inherits the shape class.
Constructors of the class are:
- Polygon(): creates a empty polygon with no set of defined coordinates of points(vertices)
- Polygon(double points[])creates a polygon with a set of defined coordinates of points(vertices)
Commonly used methods:
method | explanation |
---|---|
getPoints() | Gets the coordinates of the Polygon vertices. |
setFill(Paint p) | sets the fill for the polygon |
Below programs will illustrate the Polygon class of JavaFX:
- Program to create a polygon with a given set of vertices: This program creates a Polygon indicated by the name polygon. The coordinates for the vertices of the polygon are passed as arguments. The Polygon will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a Group is created, and the polygon is attached. The group is attached to the scene. Finally, the show() method is called to display the final results.
// 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);
   Â
}
}
Output:
- Program to create a polygon with a given set of vertices and specified fill: This program creates a Polygon indicated by the name polygon. The coordinates for the vertices of the polygon are passed as arguments. The function set Fill() is used to set the fill of the polygon. The Polygon will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a Group is created, and the polygon is attached. The group is attached to the scene. Finally, the show() method is called to display the final results.
// 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);
   Â
}
}
Output:
Note: The above programs might not run in an online IDE please use an offline IDE.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/shape/Polygon.html