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 verticesimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.layout.*;importjavafx.scene.paint.Color;importjavafx.scene.shape.Polygon;importjavafx.scene.control.*;importjavafx.stage.Stage;ÂÂimportjavafx.scene.Group;publicclasspolygon_0extendsApplication {   Â// launch the application   Âpublicvoidstart(Stage stage)   Â{       Â// set title for the stage       Âstage.setTitle("creating polygon");       Â// coordinates of the points of polygon       Âdoublepoints[] = {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 =newPolygon(points);       Â// create a Group       ÂGroup group =newGroup(polygon);       Â// create a scene       ÂScene scene =newScene(group,500,300);       Â// set the scene       Âstage.setScene(scene);       Âstage.show();   Â}   Âpublicstaticvoidmain(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 fillimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.layout.*;importjavafx.scene.paint.Color;importjavafx.scene.shape.Polygon;importjavafx.scene.control.*;importjavafx.stage.Stage;ÂÂimportjavafx.scene.Group;publicclasspolygon_1extendsApplication {   Â// launch the application   Âpublicvoidstart(Stage stage)   Â{       Â// set title for the stage       Âstage.setTitle("creating polygon");       Â// coordinates of the points of polygon       Âdoublepoints[] = {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 =newPolygon(points);       Â// set fill for the polygon       Âpolygon.setFill(Color.BLUE);       Â// create a Group       ÂGroup group =newGroup(polygon);       Â// create a scene       ÂScene scene =newScene(group,500,300);       Â// set the scene       Âstage.setScene(scene);       Âstage.show();   Â}   Âpublicstaticvoidmain(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

