×

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 - Basic Structure of Application

Every JavaFX application is mainly divided into three main components namely Scene, Stage, and nodes. For using these components we need to import javafx.application.Application class in every javafx application. Life...

4 minutes read.

JavaFX Color Input

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

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 Arc

In the JavaFX application, in order to draw an arc, the Arc class is used. The Arc allows us to join any points in the space. All methods needed for...

5 minutes read.

JavaFX Slider

In the JavaFX application, in order to slide over a range of data having values, Slider is used. It provides a pane with a Slider to move over a range...

4 minutes read.

JavaFX PasswordField

In the JavaFX application, to get the user information and password as input from the user, a form is created using textfields and password fields which allows the user to...

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 Parallel Transition

In the JavaFX application, in order to generate the parallel transition, we have to apply parallel transition on any particular shape. It has the ability to execute multiple transactions parallelly....

4 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 Translate Transition

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

3 minutes read.

JavaFX Bubble Chart

In the JavaFX application, in order to create Bubble Chart, the BubbleChart class is used. The Bubble Chart allows us to plot three dimensional data. All methods needed for this...

4 minutes read.

JavaFX CheckBox

In the JavaFX application, to get the user information in the form of choices from the user, a form is created using checkboxes that allow the user to select two...

4 minutes read.

JavaFX Blend

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

4 minutes read.

JavaFX Installation

In order to run JavaFX applications, we need to have eclipse and java installed on our machine. Prerequisite: Operating systemAny Operating system (Windows, Linux, Mac)Memory requirementNo minimum requirementDisk SpaceNo minimum requirementJDK versionJDK...

3 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.

JavaFX HyperLink

In the JavaFX application, in order to access and use webpages, hyperlinks are used. In HTML anchor tags are used in the same way in JavaFX hyperlinks are used. To...

4 minutes read.

JavaFX Area Chart

In the JavaFX application, in order to create Area Chart, the AreaChart class is used. The Area Chart allows us to plot data along the XY plane. All methods needed...

3 minutes read.

JavaFX Label

The UI Control Label in a JavaFX is used to display the simple text. To use Label in JavaFX application javafx.scene.control.The label class is used. We can place Labels on...

3 minutes read.

JavaFX Layouts

Layouts in the JavaFX application are treated as a container for different UI elements such as Button, Label, TextArea, etc. Layouts are used as parent container. The layout is used...

3 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.