Updated March 24, 2023
Introduction to JavaFX Rectangle
In the JavaFX package, there are several shapes such as Rectangle, circle, line, ellipse, etc. that can be used in applications. In this, we are looking into the shape rectangle in detail. The rectangle is a geometrical figure that contains 4 sides, out of which, the angle between the 2 neighboring sides is 900. The corners are sharp and the opposite sides of this shape are always equal. This is instantiated from class javafx.scene.shape.Rectangle. The methods, constructors, and examples of JavaFX Rectangle will be discussed in the following sections.
JavaFX Rectangle Constructors
Following are the constructors for JavaFX Rectangle Constructors:
- Rectangle(): An empty Rectangle instance will be created.
- Rectangle(double w, double h): A Rectangle instance will be created with the given width and height.
- Rectangle(double x, double y, double w, double h): A Rectangle instance will be created with the given width, height, and position.
- Rectangle(double w, double h, paint fill): A Rectangle instance will be created with the given width, height and fill.
Methods of JavaFX Rectangle
Let us see some of the commonly used methods:
- getArcHeight(): ArcHeight property’s value will be returned.
- getArcWidth(): ArcWidth property’s value will be returned.
- setArcHeight(double value): ArcHeight property’s value will be set.
- setArcWidth(double value): ArcWidth property’s value will be set.
- getHeight(): Height property’s value will be returned.
- getWidth(): Width property’s value will be returned.
- setHeight(double value): Height property’s value will be set.
- setWidth(double value): Width property’s value will be set.
- getX(): X property’s value will be returned.
- getY(): Y property’s value will be returned.
- toString(): String representation will be returned for the object of rectangle.
Properties of JavaFX Rectangle
Below are the properties:
- widthProperty(): Width will be defined for the rectangle.
- heightProperty(): Height will be defined for the rectangle.
- xProperty(): X coordinate will be defined for the rectangle’s upper left corner.
- yProperty(): Y coordinate will be defined for the rectangle’s upper left corner.
- arcHeightProperty(): Vertical diameter will be defined for rectangle’s arc in 4 corners.
- arcWidthProperty(): Horizontal diameter will be defined for rectangle’s arc in 4 corners.
How to Create a JavaFX Rectangle?
In order to create the same, the following steps can be performed:
Step 1 – Create a class and define the start() method: Firstly, Create a class that extends from the application class.
public class JavaFXRectangleExample extends Application {
}
After that, implement the method start() which indicates the starting point of application.
public void start(Stage s) {
}
Step 2 – Create a Rectangle: Instantiate the class javafx.scene.shape.Rectangle. Following is the syntax that is used for the rectangle.
//create object of rectangle
Rectangle r = new Rectangle();
Step 3 – Set different Properties of Rectangle: Now, mention the different properties such as x coordinate, y coordinate, height, width, etc.
//set the x coordinate
r.setX(60);
//set the y coordinate
r.setY(60);
//set the width
r.setWidth(300);
//set height
r.setHeight(150);
Step 4 – Create a Group: The created rectangle can be added to the group by passing it as an argument of the constructor. Even if it is not passed as shown in the sample programs in the next section, the program won’t show any error.
//create a group gp
Group gp = new Group();
Step 5 – Create a Scene: The scene is created by instantiating the class javafx. once the group is created. Then only the group can be passed as one of the arguments in scene.
Scene sc = new Scene(gp, 400, 200);
Step 6 – Set the title for the created stage in the first step: The title can be created in this step or immediately after creating the stage in the first step.
//set title for the stage s
s.setTitle("Rectangle Example");
Step 7 – Add or set the scene to the stage: The scene can be added using the method setScene() as depicted below.
s.setScene(sc);
Step 8 – Display the results or contents of the created stage and launch the application: Stage contents are displayed by calling the method show() at the end.
s.show();
Once it is done, launch the application using.
launch(args)
Examples
Now let us see some of the JavaFX programs that implement the JavaFX Rectangle:
Example #1
Java program to Create a Rectangle.
Code:
//java program to create a rectangle
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
//class that extends Application class
public class JavaFXRectangleExample extends Application {
//main method
public static void main(String[] args) {
Application.launch(args);
}
//application starts at this point
@Override
public void start(Stage s) {
//set title for the stage s
s.setTitle("Rectangle Example");
//create a group gp
Group gp = new Group();
//create a scene sc
Scene sc = new Scene(gp, 700, 300, Color.RED);
//create a rectangle
Rectangle r = new Rectangle();
//set the x coordinate
r.setX(60);
//set the y coordinate
r.setY(60);
//set the width
r.setWidth(300);
//set height
r.setHeight(150);
//add child nodes
gp.getChildren().add(r);
//set the scene sc
s.setScene(sc);
//display the results
s.show();
}
}
Output: It can be seen that a black rectangle is displayed with a red background.
Example #2
Code
//java program to create rectangle with rounded corners
import javafx.application.Application;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
//class that extends application class
public class JavaFXRectangleExample extends Application{
//application starts at this point
@Override
public void start(Stage s) throws Exception {
//set the title for the stage s
s.setTitle("Creation of a rectangle");
//create a group gp
Group gp = new Group();
//create a rectangle r
Rectangle r=new Rectangle();
//set x value
r.setX(30);
//set y value
r.setY(30);
//set width
r.setWidth(250);
//set height
r.setHeight(245);
//set arc height
r.setArcHeight(34);
//set arc width
r.setArcWidth(35);
//set colr
r.setFill(Color.BLACK);
//add child node
gp.getChildren().addAll(r);
//create scene
Scene sc = new Scene(gp,600,500,Color.RED);
//create scene
s.setScene(sc);
//display result
s.show();
}
//main method
public static void main(String[] args) {
launch(args);
}
}
Output: Here also, a black rectangle is displayed with a red background. But, the difference is that the corners of the rectangle are rounded.
Conclusion
In JavaFX, Rectangle is a shape that contains 4 sides in which the opposite sides are equal. If all sides are equal, it will be considered as a square. This document clearly explains all the aspects of JavaFX Rectangle.
Recommended Articles
This is a guide to JavaFX Rectangle. Here we discuss the constructors, methods, properties, and steps to create a JavaFX rectangle with the program. You can also go through our other related articles to learn more-