×

JavaFX Rectangle

In the JavaFX application, in order to draw a rectangle, the Rectangle class is used. The Rectangle allows us to join any four points with x and y coordinates in the space. All methods needed for this purpose are present in the javafx.scene.shape.Rectangle class.

Properties and corresponding methods to create Rectangle:

1. X

This property of the Rectangle class allows us to define the starting point of Rectangle which is X co-ordinate and the setX() method helps to set it.

2. width

This property of the Rectangle class allows us to define the width of the Rectangle and the setWidth() method helps to set it.

3. Y

This property of the Rectangle class allows us to define the starting point of the line which is Y co-ordinate and setY() method helps to set it.

4. height

This property of the Rectangle class allows us to define the height of the Rectangle and the setHeight() method helps to set it.

5. ArcHeight

It is used to set the height of the arc at 4 corners of the rectangle.

6. ArcWidth

It is used to set the width of the arc at 4 corners of the rectangle.

JavaFX 2D Shape -Rectangle

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.Rectangle;


  




public class RectangleUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
    Rectangle newrect = new Rectangle(); 
    newrect.setX(50); 
    newrect.setY(50); 
    newrect.setWidth(300); 
    newrect.setHeight(300);
    newrect.setFill(Color.BLUE);  


         Group box = new Group();  
         box.getChildren().addAll(newrect);


         box.getChildren().addAll();      
         Scene scene = new Scene(box,600,400);  
         
           primaryStage.setScene(scene);  
           primaryStage.setTitle(" JavaFX 2D shapes - Rectangle Example ");  
           primaryStage.show();  
        }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the Rectangle 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.paint.Color, javafx.scene.shape.Rectangle.

Then we have created one class named RectangleUI 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 a Rectangle, a Group object is created which is then passed to the Scene class object.

The rectangle is created with the help of an empty constructor and X, Y, Width, and Height is set using setters and the setFill() method is used to set the color of the Rectangle.

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 called in the main() method. In output Frame like container is displayed with the title, " JavaFX 2D shapes - Rectangle Example”. Also, it displays a rectangle with defined x, y coordinates, and width, height with Blue color filled in it.

JavaFX 2D Shape -Rectangle

JavaFX 2D Shape – Rounded corner Rectangle

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.Rectangle;


  
public class RectangleUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
    Rectangle newrect = new Rectangle(); 
    newrect.setX(50); 
    newrect.setY(50); 
    newrect.setWidth(300); 
    newrect.setHeight(300);
    newrect.setArcHeight(35);  
    newrect.setArcWidth(35);  
    newrect.setFill(Color.BLUE);  


         Group box = new Group();  
         box.getChildren().addAll(newrect);


         box.getChildren().addAll();      
         Scene scene = new Scene(box,600,400);  
         
           primaryStage.setScene(scene);  
           primaryStage.setTitle(" JavaFX 2D shapes - Rectangle Example ");  
           primaryStage.show();  
        }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the Rectangle with rounded corners 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.paint.Color, javafx.scene.shape.Rectangle.

Then we have created one class named RectangleUI 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 a Rectangle with rounded corners, a Group object is created which is then passed to the Scene class object.

The rectangle is created with the help of an empty constructor and X, Y, Width, and Height is set using setters and the setFill() method is used to set the color of the Rectangle. Also using setArcHeight() and setArcWidth(), we can set height and width of corners of rectangle.

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 called in the main() method. In output Frame like container is displayed with the title, " JavaFX 2D shapes - Rectangle Example". Also, it displays a rectangle with defined x, y coordinates, and width, height with Blue color filled in it with rounded corners.

JavaFX 2D Shape -Rectangle

Related Topics

JavaFX HBox

In the JavaFX application, in order to set HBox as a layout, the HBox class is used. The HBox layout allows us to arrange the components in one row. All...

3 minutes read.

JavaFX BorderPane

In the JavaFX application, in order to set Border Pane as a layout, the BorderPane class is used. The BorderPane layout allows us to arrange the components in the left,...

4 minutes read.

JavaFX Light Point

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

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

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

3 minutes read.

JavaFX Line

In the JavaFX application, in order to draw a line, the Line class is used. The line allows us to join any two points with x and y coordinates in...

3 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 Scale Transition

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

4 minutes read.

JavaFX Light Spot

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

4 minutes read.

JavaFX Scaling

In the JavaFX application, in order to apply various Scaling effects, we use this class. It has the ability to change the size of the given object. All methods needed...

5 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 Line Chart

In the JavaFX application, in order to create a Line Chart, LineChart class is used. The LineChart allows us to plot markers point according to the entities on number axes....

4 minutes read.

JavaFX ScrollBar

In the JavaFX application, in order to scroll over a range of values, ScrollBar is used. It is used to view long page contents. In order to use ScrollBar in...

4 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 Button

The button is one of the UI control present in the JavaFX. The javafx.scene.control.Button class is used for creating a button in javafx example. It is also possible to create...

5 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 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 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 Tutorial

Introduction of JavaFX: The most popular applications called Rich Internet Application are build using the JavaFX. This application can run across many platforms. Various devices such as mobile, computers, and tablets...

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