Table of contents
1.
Introduction
2.
How to create a rectangle?
2.1.
Create a Class
2.2.
Create a rectangle
2.3.
Set the properties to the rectangle
2.4.
Create a group object
2.5.
Create a scene objects
2.6.
Set the title to stage
2.7.
Add the scene to the stage
2.8.
Display the content
2.9.
Launch the application
3.
Methods related to JavaFX Rectangle
3.1.
Let's look at some of the popular techniques:
4.
Example
4.1.
Code 
4.2.
Output 
5.
Frequently Asked Questions
5.1.
What is a rectangle?
5.2.
Will the above programs work in an online IDE?
5.3.
What is the width property used for?
6.
Conclusion
Last Updated: Mar 27, 2024

JavaFX Rectangle

Author Ayushi Poddar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Many shapes in the JavaFX package can be used in applications, including rectangles, circles, lines, ellipses, and more. Let's take a closer look at the rectangle's shape. A geometric figure called a rectangle has four sides, and the angle formed by the two adjacent sides is 900 degrees. The opposite sides of the rectangle shape are always equal, and the corners are sharp. This is a javafx.scene.shape.Rectangle class object that was instantiated. The next sections will cover JavaFX Rectangle's methods, constructors, and examples.

In general, a rectangle is a four-sided polygon with all interior angles being right angles and having two pairs of parallel and contemporary sides. Height and width are its two defining characteristics. Height is the rectangle's vertical length, and width is the length of the rectangle measured horizontally.

A class called Rectangle in JavaFX serves as the representation of a rectangle. This class is a part of the javafx.scene.shape package. You can build a Rectangle node in JavaFX by instantiating this class.

This class has four double datatype attributes precisely:

X – The x coordinate of the rectangle's upper-left starting point.

Y – The y coordinate of the rectangle's upper-left starting point.

Width − The width of the rectangle.

Height − The height of the rectangle.

How to create a rectangle?

Following are the steps to create a rectangle using JavaFX : 

Create a Class

Create a Java class, and inherit the start() method from the package javafx.application's Application class, and then implement this method as shown below.

public class ClassName extends Application {  
   @Override     
   public void commence(Stage primaryStage) throws Exception {      
   }    
}

Create a rectangle

By instantiating the Rectangle class from the javafx.scene.shape package, you may build a rectangle in JavaFX. You can instantiate this class as shown below.

Rectangle rectangle = new Rectangle();

Set the properties to the rectangle

Give the height and width of the rectangle that has to be drawn, as well as the x and y coordinates of the beginning point (upper left). The following code block illustrates how to set the properties x, y, height, and width using their respective setter methods.

Create a group object

In the start() method, instantiate the Group class from the javafx.scene package to produce a group object. In order to add the Rectangle (node) object to the group as described below, pass it as a parameter to the Group class's function Object() 

line.setStartX(100.0);
line.setStartY(250.0);
line.setEndX(600.0);
line.setEndY(250.0);

Create a scene objects

By instantiating the Scene class from the javafx.scene package, you can create a Scene. Pass the Group object (root) that was created in the previous step to this class. You can pass an object of the Group class together with two double parameters representing the screen's height and width in addition to the root object, as seen in the example below.

Group root = new Group(rectangle);

Set the title to stage

Use the setTitle() method of the Stage class to change the stage's title. A Stage object is used as the primary stage and is given as a parameter to the scene class's start method. Set the scene's title to Sample Application using the primaryStage object.

Scene scene = new Scene(group ,600, 300)

Add the scene to the stage

Using the setScene() method of the Stage class, you may add a Scene object to the stage. Using the technique given below, add the Scene object that was ready in the preceding phases.

primaryStage.setTitle("Application Example");

Display the content

The Stage class's show() method can be used to display the scene's contents.

primaryStage.show()

Launch the application

The static method launch() present in the Application class should be called from the main function to start the JavaFX application.

public static void main(String args[]){   
   launch(args);      
}

Methods related to JavaFX Rectangle

Let's look at some of the popular techniques:

  • getArcHeight() will return the value of the ArcHeight property.
  • getArcWidth() will return the value of the ArcWidth property.
  • setArcHeight(double value) will set the value of the ArcHeight property.
  • setArcWidth(double value) will set the value of the ArcWidth property.
  • getHeight() will return the value of the Height property.
  • getWidth() will return the value of the width property.
  • setHeight(double value) will set the value of the Height property.
  • setWidth(double value) will set the value of the Width property.
  • getX() returns the value of the X property.
  • getY() will return the value of the Y property.
  • toString() will return the rectangle object's string representation 

Example

Here is the JavaFX program that creates a rectangle. Put this code in a file called RectangleExample.java and save it.

Code 

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 
import javafx.scene.shape.Rectangle;
public class ExampleOfRectangle extends Application { 
   @Override 
   public void commence(Stage stage) {
      Rectangle rectangle = new Rectangle(); 
      rectangle.setX(200.0f); 
      rectangle.setY(75.0f); 
      rectangle.setWidth(400.0f); 
      rectangle.setHeight(200.0f);     
      Group root = new Group(rectangle); 
      Scene scene = new Scene(root, 600, 300); 
      stage.setTitle("Drawing a Rectangle"); 
      stage.setScene(scene);
      stage.show(); 
   }      
   public static void main(String args[]){ 
      launch(args); 
   } 
}

Output 

Reactangle

 

Frequently Asked Questions

What is a rectangle?

A geometric figure called a rectangle has four sides, and the angle formed by the two adjacent sides is 900 degrees. The opposite sides of this shape are always equal, and the corners are sharp.

Will the above programs work in an online IDE?

No, the programs might not run in an online IDE, so it is advised to use an offline compiler.

What is the width property used for?

This property defines the width of the rectangle.

Conclusion

This article extensively discussed the implementation and use of the JavaFX Rectangle. We hope this blog has helped you gain more knowledge of JavaFX rectangles. Check out our articles on Coding Ninjas.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blog to help other ninjas grow. Happy Coding!

 

 

Live masterclass