×

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 -Polygon

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

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.

JavaFX 2D Shape -Polygon

Related Topics

JavaFX Color Adjust

In the JavaFX application, in order to apply various color adjust effect, we use ColorAdjust class. It has the ability to apply various color adjust effect such as hue, saturation,...

5 minutes read.

JavaFX Light Distant

In the JavaFX application, in order to apply various Light Distant effect from a single source, we use this class. It has the ability to apply light from a distant...

4 minutes read.

JavaFX Transformation

In the JavaFX application, in order to apply various transformation effects, we use Transform class. It has the ability to create the transformation on the given object. This transformation changes...

3 minutes read.

JavaFX Event Handling

JavaFX gives way to handle various events in web application, desktop applications and graphical applications. In the JavaFX application, in order to allow user to interact with application various events...

4 minutes read.

JavaFX Lighting

In the JavaFX application, in order to apply various Lighting effect from a single source, we use this class. It has the ability to lighten a node from a given...

4 minutes read.

JavaFX Inline Styles

In the JavaFX application, in order to apply various Inline css effect we will use the various css classes available in the JavaFX. It is used to apply the various...

4 minutes read.

JavaFX Pause Transition

In the JavaFX application, in order to generate the pause transition, we have to apply pause transition on any particular shape. It has the ability to take a particular pause...

4 minutes read.

JavaFX Architecture

JavaFX architecture consists of many built-in components such as JavaFX public API, Scene Graph, Quantum Toolkit, Prism, Glass windowing toolkit, web view, media engines. These components help to develop rich...

3 minutes read.

JavaFX Stroke Transition

In the JavaFX application, in order to generate the stroke transition, we have to apply stroke transition on any particular shape. It has the ability to give different color strokes...

4 minutes read.

JavaFX Ellipse

In the JavaFX application, in order to draw an Ellipse, the Ellipse class is used. The Ellipse allows us to join any points in the space to draw the unique...

7 minutes read.

JavaFX RadioButton

In the JavaFX application, to get the user information in the form of choice from the user, a form is created using radio buttons that allow the user to select...

3 minutes read.

JavaFX Image Input

In the JavaFX application, in order to apply various image input effect, we use the ImageInput class. It has the ability to fill the specified shape with a given image...

4 minutes read.

JavaFX Motion Blur

In the JavaFX application, in order to apply various Motion blur effect, we use Motion Blur class. It has the ability to apply the Motion blur effect on the given...

4 minutes read.

JavaFX Menu

In the JavaFX application, in order to create a menu, menu items, and menu bar, Menu, MenuItem, and MenuBar class is used. The menu allows us to choose options among...

4 minutes read.

JavaFX Scatter Chart

In the JavaFX application, in order to create Scatter Chart, the ScatterChart class is used. The Scatter Chart allows us to plot data along the x and y axis of...

4 minutes read.

JavaFX Bloom

In the JavaFX application, in order to apply various bloom effect, we use the Bloom class. It has the ability to bloom the colors of the shape. All methods needed...

4 minutes read.

JavaFX Translation

In the JavaFX application, in order to apply various Translate effect, we use this class. It has the ability to Translate the given object according to the specified parameter. All...

4 minutes read.

JavaFX GridPane

In the JavaFX application, in order to set Grid Pane as a layout, the GridPane class is used. The GridPane layout allows us to arrange the components in the form...

4 minutes read.

JavaFX ProgressBar

In the JavaFX application, in order to show the progress of work ProgressBar is used. It shows how much work is done. In order to use ProgressBar in JavaFX, javafx.scene.control.ProgressBar...

4 minutes read.

JavaFX Pie Chart

In the JavaFX application, in order to create Pie Chart, the PieChart class is used. The Pie Chart allows us to plot data along with the sectors of the circle....

3 minutes read.