JavaFX Polygon
In the JavaFX application, in order to draw a polygon, the Polygon class is used. The Polygon allows us to join any number of points in the space. All methods needed for this purpose are present in the javafx.scene.shape.Polygon class. We can create any polygon like triangle, square, diamond, pentagon, or hexagon using Polygon in JavaFX. The addAll() method with all points in the double array is passed to polygon in order to draw the particular shape.
JavaFX 2D Shape – Triangular Polygon
Example:
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Polygon;
public class PolygonUI extends Application {
@Override
public void start(Stage primaryStage) throws Exception
{
Group box = new Group();
Polygon newpoly = new Polygon();
newpoly.getPoints().addAll(new Double[]{
100.0,100.0,
100.0, 400.0,
400.0, 100.0 });
box.getChildren().addAll(newpoly);
Scene scene = new Scene(box,600,00);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFX 2D shapes - Triangular Polygon Example");
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Output:
In order to create the Polygon in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.stage.Stage, javafx.scene.Scene, javafx.scene.Group, javafx.scene.shape.Polygon.
Then we have created one class named PolygonUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold Polygon, a Group object is created which is then passed to the Scene class object.
Polygon is created with three points passed in the addAll() method which is later passed in the polygon constructor.
The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is invoked. In output Frame like container is displayed with the title, " JavaFX 2D shapes – Triangular Polygon Example”. Also, it displays Polygon with defined X and Y coordinates.
JavaFX 2D Shape – Diamond shape Polygon
Example:
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Polygon;
public class PolygonUI extends Application {
@Override
public void start(Stage primaryStage) throws Exception
{
Group box = new Group();
Polygon newpoly = new Polygon();
newpoly.getPoints().addAll(new Double[]{
350.0, 100.0,
500.0, 200.0,
350.0, 300.0,
200.0, 200.0, });
box.getChildren().addAll(newpoly);
Scene scene = new Scene(box,600,500);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFX 2D shapes - Diamond shape polygon Example");
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Output:
In order to create the Diamond shape Polygon in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.stage.Stage, javafx.scene.Scene, javafx.scene.Group, javafx.scene.shape.Polygon.
Then we have created one class named PolygonUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold Diamond shape Polygon, a Group object is created which is then passed to the Scene class object.
Diamond shape Polygon is created with four points passed in the addAll() method which is later passed in the polygon constructor.
The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is invoked. In output Frame like container is displayed with the title, " JavaFX 2D shapes – Diamond shape Polygon Example". Also, it displays a Diamond shape Polygon with defined X and Y coordinates.
JavaFX 2D Shape – Polygon with color filled in it
Example:
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
public class PolygonUI extends Application {
@Override
public void start(Stage primaryStage) throws Exception
{
Group box = new Group();
Polygon newpoly = new Polygon();
newpoly.getPoints().addAll(new Double[]{
100.0,100.0,
100.0, 400.0,
400.0, 100.0 });
newpoly.setFill(Color.BLUE);
box.getChildren().addAll(newpoly);
Scene scene = new Scene(box,600,00);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFX 2D shapes - Triangular Polygon Example");
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Output:
In order to create the Polygon with color in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.stage.Stage, javafx.scene.paint.Color, javafx.scene.Scene, javafx.scene.Group, javafx.scene.shape.Polygon.
Then we have created one class named PolygonUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold colored Polygon, a Group object is created which is then passed to the Scene class object.
Polygon is created with three points passed in the addAll() method which is later passed in the polygon constructor and the setFill() method is used to fill color.
The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is invoked. In output Frame like container is displayed with the title, " JavaFX 2D shapes – Triangular Polygon Example". Also, it displays a Triangular Polygon with defined X and Y coordinates and blue color filled in it.