JavaFX Tutorial Index

JavaFX Tutorial JavaFX Installation JavaFX Application Structure JavaFX Architecture

JavaFX Applications Charts

JavaFX Bar Chart JavaFX Bubble Chart JavaFX Pie Chart JavaFX Scatter Chart JavaFX Area Chart JavaFX Line Chart

JavaFX 2D Shapes

JavaFX Arc JavaFX Circle JavaFX Ellipse JavaFX Line JavaFX Polygon JavaFX Rectangle JavaFX Color JavaFX Gradient Color

JavaFX 3D Shapes

JavaFX Box JavaFX Cylinder JavaFX Sphere

JavaFX Animations

JavaFX Rotate Transition JavaFX Translate Transition JavaFX Fade Transition JavaFX Fill Transition JavaFX Parallel Transition JavaFX Path Transition JavaFX Pause Transition JavaFX Scale Transition JavaFX Sequential Transition JavaFX Stroke Transition

JavaFX CSS

JavaFX ID Selector JavaFX Inline Styles JavaFX Selectors

JavaFX Effect

JavaFX Blend JavaFX Bloom JavaFX Color Adjust JavaFX Color Input JavaFX Drop Shadow JavaFX Gaussian Blur JavaFX Glow JavaFX Image Input JavaFX Inner Shadow JavaFX Light Distant JavaFX Light Point JavaFX Light Spot JavaFX Lighting JavaFX Motion Blur JavaFX Reflection JavaFX Shadow

JavaFX Layouts

JavaFX Layouts JavaFX BorderPane JavaFX GridPane JavaFX StackPane JavaFX HBox JavaFX TilePane

JavaFX Event Handling

JavaFX Event Handling JavaFX Event Filters JavaFX Convenience Methods

JavaFX Transformation

JavaFX Transformation JavaFX Scaling JavaFX Rotation JavaFX Translation JavaFX Shear

JavaFX UI

JavaFX Menu JavaFX Button JavaFX Button Styling JavaFX CheckBox JavaFX File Chooser JavaFX HyperLink JavaFX Label JavaFX UI Controls JavaFX PasswordField JavaFX ProgressBar JavaFX RadioButton JavaFX ScrollBar JavaFX Slider JavaFX TextField

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