JavaFX | Rectangle and Rounded Rectangle with examples

19 Aug, 2021
Rectangle class is a part of JavaFX. Rectangle class creates a rectangle with specified width and height and position. By default Rectangle has sharp corners but the edges can be rounded by applying a arc height and width. 
Constructor : 
Rectangle(): creates a empty instance of rectangleRectangle(double w, double h): creates a rectangle with a specified width and heightRectangle(double x, double y, double w, double h): creates a rectangle with a specified width and height and positionRectangle(double w, double h, Paint f): creates a rectangle with a specified width and height and fill
Rectangle(): creates a empty instance of rectangle
Rectangle(double w, double h): creates a rectangle with a specified width and height
Rectangle(double x, double y, double w, double h): creates a rectangle with a specified width and height and position
Rectangle(double w, double h, Paint f): creates a rectangle with a specified width and height and fill
Commonly used methods  
Sample program to illustrate the use of Rectangle class
This program creates a Rectangle indicated by the name rectangle( the coordinates of the position and the height and width is passed as arguments). The Rectangle will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a Group is created, and the circle is attached. The group is attached to the scene. Finally, the show() method is called to display the final results. 
Program to create a rectangle and add it to the scene  
Java
// Java Program to create a rectangle and add it to the sceneimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.scene.paint.Color;import javafx.scene.shape.Rectangle;import javafx.scene.control.*;import javafx.stage.Stage; import javafx.scene.Group;public class Rectangle_0 extends Application {     // launch the application    public void start(Stage stage)    {        // set title for the stage        stage.setTitle("creating Rectangle");         // create a rectangle        Rectangle rectangle = new Rectangle(100.0d, 100.0d, 120.0d, 80.0d);         // create a Group        Group group = new Group(rectangle);         // create a scene        Scene scene = new Scene(group, 500, 300);         // set the scene        stage.setScene(scene);         stage.show();    }     public static void main(String args[])    {        // launch the application        launch(args);    }}
Output 
 
Program to create a rounded rectangle and set a fill and add it to the scene
This program creates a Rectangle indicated by the name rectangle( the coordinates of the position and the height and width is passed as arguments). The rounded corners will be set using setArcHeight() and setArcWidth() function .The fill for the rectangle will be set using setFill() function. The Rectangle will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a Group is created, and the circle is attached. The group is attached to the scene. Finally, the show() method is called to display the final results. 
Java
// Java Program to create a rounded rectangle// and set a fill and add it to the sceneimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.scene.paint.Color;import javafx.scene.shape.Rectangle;import javafx.scene.control.*;import javafx.stage.Stage; import javafx.scene.Group;public class Rectangle_1 extends Application {     // launch the application    public void start(Stage stage)    {        // set title for the stage        stage.setTitle("creating Rectangle");         // create a rectangle        Rectangle rectangle = new Rectangle(100.0d, 100.0d, 120.0d, 80.0d);         // set fill for rectangle        rectangle.setFill(Color.BLUE);         // set rounded corners        rectangle.setArcHeight(10.0d);        rectangle.setArcWidth(10.0d);         // create a Group        Group group = new Group(rectangle);         // create a scene        Scene scene = new Scene(group, 500, 300);         // set the scene        stage.setScene(scene);         stage.show();    }     public static void main(String args[])    {        // launch the application        launch(args);    }}
Output: 
Note: The following programs might not run in an online IDE please use an offline compiler 
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/shape/Rectangle.html 
ManasChhabra2
sweetyty
kalrap615
JavaFX
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Introduction to Java
Constructors in Java
Exceptions in Java
Generics in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
Factory method design pattern in Java
Java Program to Remove Duplicate Elements From the Array