JavaFX | Polygon with examples
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 | explanantion |
|---|---|
| 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 applicationpublicvoidstart(Stage stage){// set title for the stagestage.setTitle("creating polygon");// coordinates of the points of polygondoublepoints[] = {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 polygonPolygon polygon =newPolygon(points);// create a GroupGroup group =newGroup(polygon);// create a sceneScene scene =newScene(group,500,300);// set the scenestage.setScene(scene);stage.show();}publicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:

- 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 applicationpublicvoidstart(Stage stage){// set title for the stagestage.setTitle("creating polygon");// coordinates of the points of polygondoublepoints[] = {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 polygonPolygon polygon =newPolygon(points);// set fill for the polygonpolygon.setFill(Color.BLUE);// create a GroupGroup group =newGroup(polygon);// create a sceneScene scene =newScene(group,500,300);// set the scenestage.setScene(scene);stage.show();}publicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:

- JavaFX | Box with examples
- JavaFX | Arc with examples
- JavaFX | Circle with examples
- JavaFX | Line with examples
- JavaFX | Alert with examples
- JavaFx | ColorPicker with examples
- JavaFX | RadioButton with examples
- JavaFX | QuadCurve with examples
- JavaFX | CubicCurve with examples
- JavaFX | DatePicker with examples
- JavaFX | Sphere with examples
- JavaFX | CheckMenuItem with examples
- JavaFX | Button with examples
- JavaFX | Ellipse with examples
- JavaFX | Cylinder with examples
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
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



