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