- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a Polygon using JavaFX?
A polygon is a closed figure formed using n number of lines existing in the same plane. In JavaFX a polygon is represented by the javafx.scene.shape.Polygon class.
To create a polygon you need to −
Instantiate this class.
Pass the start and endpoints, of the line segments to draw a polygon to the class either by passing them as arguments to the constructor or, using the getPoints() method as −
polygon.getPoints().addAll(new Double[]{ List of XY coordinates separated by commas });
Add the created node (shape) to the Group object.
Example
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.stage.Stage; import javafx.scene.shape.Polygon; public class DrawingPolygon extends Application { public void start(Stage stage) { //Drawing a polygon Polygon polygon1 = new Polygon(); //Setting the properties of the ellipse polygon1.getPoints().addAll(new Double[]{ 250.0, 50.0, 175.0, 150.0, 175.0, 250.0, 200.0, 250.0, 350.0, 150.0 }); //Setting other properties polygon1.setFill(Color.DARKCYAN); polygon1.setStrokeWidth(8.0); polygon1.setStroke(Color.DARKSLATEGREY); //Drawing a polygon Polygon polygon2 = new Polygon(); //Setting the properties of the ellipse polygon2.getPoints().addAll(new Double[]{ 410.0, 160.0, 430.0, 130.0, 470.0, 130.0, 490.0, 160.0, 470.0, 200.0, 430.0, 200.0 }); //Setting other properties polygon2.setFill(Color.CHOCOLATE); polygon2.setStrokeWidth(8.0); polygon2.setStroke(Color.BROWN); //Setting the Scene Group root = new Group(polygon1, polygon2); Scene scene = new Scene(root, 595, 300, Color.BEIGE); stage.setTitle("Drawing Polygon"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
Output
Advertisements