Rectangle2D class is a part of JavaFX. Rectangle2D class creates a rectangle with the given coordinates of the upper left corner of the rectangle and the width and height or it is defined by a location (minX, minY) and dimension (width x height).
Constructors of the class:
- Rectangle2D(double minX, double minY, double width, double height): Creates a new Rectangle2D with specified width height and the coordinates of upper left corner of the Rectangle
Commonly Used Methods:
Method | Explanation |
---|---|
contains(double x, double y) | Checks if the specified coordinates are inside the boundary of Rectangle2D. |
contains(double x, double y, double w, double h) | Checks whether the specified rectangle lies inside the given rectangle |
contains(Point2D p) | Checks if the specified Point2D coordinates are inside the boundary of Rectangle2D. |
contains(Rectangle2D r) | Checks whether the specified Rectangle2D lies inside the given rectangle |
getHeight() | Returns the height of the rectangle |
getWidth() | Returns the width of the rectangle |
getMaxX() | The x coordinate of the lower-right corner of this Rectangle2D. |
getMinX() | The x coordinate of the upper-left corner of this Rectangle2D. |
getMaxY() | The y coordinate of the lower-right corner of this Rectangle2D. |
getMinY() | The y coordinate of the upper-left corner of this Rectangle2D. |
intersects(double x, double y, double w, double h) | Checks whether the specified rectangle intersects the Rectangle2D object |
intersects(Rectangle2D r) | Checks whether the specified Rectangle2D (r) intersects the Rectangle2D object |
Below programs illustrate the use of Rectangle2D class:
-
Java Program to create an object of Rectangle2D and display its details and whether it contains a point and check whether it intersects a rectangle or not: This program creates a Rectangle2D object named rectangle with minX, minY, height and width as parameters. The details of the Rectangle2D object is displayed using the display function. The display function displays the upper left coordinate of the rectangle and the bottom right corner of the rectangle and its width and height using the getMinX(), getMinY(), getMaxX(), getMaxY(), getHeight(), and getWidth() function. We will see whether the rectangle contains a point using the contains() function and also check whether it intersects the other rectangle using the intersects function and display the results.
// Java Program to create an object of
// Rectangle2D and display its details
// and whether it contains a point and
// whether it intersects a rectangle or not
import
javafx.geometry.*;
import
java.util.*;
class
Rectangle_1 {
// Main Method
public
static
void
main(String args[])
{
try
{
// rectangle object
Rectangle2D rectangle =
new
Rectangle2D(
100
,
100
,
100
,
100
);
// display the rectangle
display(rectangle);
// Check whether the rectangle contains a point
System.out.println(
"The rectangle contains point 150, 150 = "
+ rectangle.contains(
new
Point2D(
150
,
150
)));
System.out.println(
"The rectangle contains point 50, 50 = "
+ rectangle.contains(
new
Point2D(
50
,
50
)));
// Check Whether the rectangle
// intersects another rectangle
System.out.print(
"The rectangle intersects another rectangle "
+
"with width = 100, height = 100, minX = 50, & minY = 50: "
+ rectangle.intersects(
50
,
50
,
100
,
100
));
}
catch
(Exception e)
{
System.err.println(e.getMessage());
}
}
// display function
public
static
void
display(Rectangle2D rectangle)
{
// display the details of a rectangle
System.out.println(
"Upper left point of the rectangle is = "
+ rectangle.getMinX() +
", "
+ rectangle.getMinY());
System.out.println(
"Lower right point of the rectangle is = "
+ rectangle.getMaxX() +
", "
+ rectangle.getMaxY());
System.out.println(
"Width and Height of the rectangle is = "
+ rectangle.getWidth() +
", "
+ rectangle.getHeight());
}
}
Output:
Upper left point of the rectangle is = 100.0, 100.0
Lower right point of the rectangle is = 200.0, 200.0
Width and Height of the rectangle is = 100.0, 100.0
The rectangle contains point 150, 150 = true
The rectangle contains point 50, 50 = false
The rectangle intersects another rectangle with width = 100, height = 100, minX = 50, & minY = 50: true -
Java Program to create two objects of Rectangle2D and display its details and check whether it intersects each other or not: This program creates two Rectangle2D objects named rectangle_1, and rectangle_2 with minX, minY, height, and width as parameters. The details of the Rectangle2D object is displayed using the display function. The display function displays the upper left coordinate of the rectangle and the bottom right corner of the rectangle and its width and height using the getMinX(), getMinY(), getMaxX(), getMaxY(), getHeight(), and getWidth() function. We will check whether it intersects the other rectangle or not using the intersects function and display the results.
// Java Program to create two objects of
// Rectangle2D and display its details and
// check whether it intersects each other or not
import
javafx.geometry.*;
import
java.util.*;
class
Rectangle_1 {
// Main Method
public
static
void
main(String args[])
{
try
{
// rectangle object
Rectangle2D rectangle_1 =
new
Rectangle2D(
100
,
100
,
100
,
100
);
Rectangle2D rectangle_2 =
new
Rectangle2D(
100
,
100
,
100
,
100
);
// display the rectangle details
System.out.println(
"Rectangle_1 details"
);
display(rectangle_1);
System.out.println(
""
);
System.out.println(
"Rectangle_2 details"
);
display(rectangle_2);
// display whether these two
// rectangle intersects or not
System.out.println(
"These two rectangles intersect = "
+ rectangle_1.intersects(rectangle_2));
}
catch
(Exception e) {
System.err.println(e.getMessage());
}
}
// display method
public
static
void
display(Rectangle2D r)
{
// display the details of a rectangle
System.out.println(
"Upper left point of the rectangle is = "
+ r.getMinX() +
", "
+ r.getMinY());
System.out.println(
"Lower right point of the rectangle is = "
+ r.getMaxX() +
", "
+ r.getMaxY());
System.out.println(
"Width and Height of the rectangle is = "
+ r.getWidth() +
", "
+ r.getHeight());
}
}
Output:
Rectangle_1 details
Upper left point of the rectangle is = 100.0, 100.0
Lower right point of the rectangle is = 200.0, 200.0
Width and Height of the rectangle is = 100.0, 100.0Rectangle_2 details
Upper left point of the rectangle is = 100.0, 100.0
Lower right point of the rectangle is = 200.0, 200.0
Width and Height of the rectangle is = 100.0, 100.0
These two rectangles intersect = true
Note: The above programs might not run in an online IDE. Please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/geometry/Rectangle2D.html