JavaFX | Rectangle and Rounded Rectangle with examples
Last Updated :
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 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
method |
explanation |
getArcHeight() |
returns the arc height of the rectangle |
getArcWidth() |
returns the arc width of the rectangle |
getHeight() |
returns the height of the rectangle |
getWidth() |
returns the width of the rectangle |
getX() |
Gets the value of the property x. |
getY() |
Gets the value of the property y. |
setArcHeight(double v) |
sets the arc height of the rectangle |
setArcWidth(double v) |
sets the arc width of the rectangle |
setHeight(double value) |
sets the height of the rectangle |
setWidth(double value) |
sets the width of rectangle |
setX(double value) |
set the x coordinate of position of rectangle |
setY(double value) |
set the y coordinate of position of rectangle |
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
import 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 {
public void start(Stage stage)
{
stage.setTitle( "creating Rectangle" );
Rectangle rectangle = new Rectangle( 100 .0d, 100 .0d, 120 .0d, 80 .0d);
Group group = new Group(rectangle);
Scene scene = new Scene(group, 500 , 300 );
stage.setScene(scene);
stage.show();
}
public static void main(String args[])
{
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
import 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 {
public void start(Stage stage)
{
stage.setTitle( "creating Rectangle" );
Rectangle rectangle = new Rectangle( 100 .0d, 100 .0d, 120 .0d, 80 .0d);
rectangle.setFill(Color.BLUE);
rectangle.setArcHeight( 10 .0d);
rectangle.setArcWidth( 10 .0d);
Group group = new Group(rectangle);
Scene scene = new Scene(group, 500 , 300 );
stage.setScene(scene);
stage.show();
}
public static void main(String args[])
{
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...