JavaFX | Rectangle2D Class

05 Nov, 2019
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:
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 notimport 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.0Lower right point of the rectangle is = 200.0, 200.0Width and Height of the rectangle is = 100.0, 100.0The rectangle contains point 150, 150 = trueThe rectangle contains point 50, 50 = falseThe rectangle intersects another rectangle with width = 100, height = 100, minX = 50, & minY = 50: trueJava 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 notimport 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 detailsUpper left point of the rectangle is = 100.0, 100.0Lower right point of the rectangle is = 200.0, 200.0Width and Height of the rectangle is = 100.0, 100.0Rectangle_2 detailsUpper left point of the rectangle is = 100.0, 100.0Lower right point of the rectangle is = 200.0, 200.0Width and Height of the rectangle is = 100.0, 100.0These two rectangles intersect = true
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 notimport 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.0Lower right point of the rectangle is = 200.0, 200.0Width and Height of the rectangle is = 100.0, 100.0The rectangle contains point 150, 150 = trueThe rectangle contains point 50, 50 = falseThe rectangle intersects another rectangle with width = 100, height = 100, minX = 50, & minY = 50: true
// 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 notimport 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.0Lower right point of the rectangle is = 200.0, 200.0Width and Height of the rectangle is = 100.0, 100.0The rectangle contains point 150, 150 = trueThe rectangle contains point 50, 50 = falseThe 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 notimport 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 detailsUpper left point of the rectangle is = 100.0, 100.0Lower right point of the rectangle is = 200.0, 200.0Width and Height of the rectangle is = 100.0, 100.0Rectangle_2 detailsUpper left point of the rectangle is = 100.0, 100.0Lower right point of the rectangle is = 200.0, 200.0Width and Height of the rectangle is = 100.0, 100.0These two rectangles intersect = true
// Java Program to create two objects of// Rectangle2D and display its details and// check whether it intersects each other or notimport 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 detailsUpper left point of the rectangle is = 100.0, 100.0Lower right point of the rectangle is = 200.0, 200.0Width and Height of the rectangle is = 100.0, 100.0
Rectangle_2 detailsUpper left point of the rectangle is = 100.0, 100.0Lower right point of the rectangle is = 200.0, 200.0Width and Height of the rectangle is = 100.0, 100.0These 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
ManasChhabra2
JavaFX
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Introduction to Java
Constructors in Java
Exceptions in Java
Generics in Java
Functional Interfaces in Java
Java Programming Examples
Strings in Java
Differences between JDK, JRE and JVM
Abstraction in Java