📜  JavaFX | Rectangle2D 类

📅  最后修改于: 2022-05-13 01:54:42.284000             🧑  作者: Mango

JavaFX | Rectangle2D 类

Rectangle2D 类是 JavaFX 的一部分。 Rectangle2D 类使用给定的矩形左上角坐标以及宽度和高度创建一个矩形,或者它由位置(minX,minY)和尺寸(宽度 x 高度)定义。

类的构造函数:

  • Rectangle2D(double minX, double minY, double width, double height) : 创建一个新的 Rectangle2D,指定宽度高度和 Rectangle 的左上角坐标

常用方法:

MethodExplanation
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

下面的程序说明了 Rectangle2D 类的使用:

  1. Java程序创建一个 Rectangle2D 对象并显示其详细信息以及它是否包含一个点并检查它是否与矩形相交:该程序创建一个名为rectangle的 Rectangle2D 对象,其参数为minXminYheightwidth 。使用 display函数显示 Rectangle2D 对象的详细信息。显示函数使用getMinX()getMinY()getMaxX()getMaxY()getHeight()getWidth()显示矩形的左上角坐标和矩形的右下角及其宽度和高度函数。我们将使用contains()函数查看矩形是否包含一个点,并使用 intersects函数检查它是否与另一个矩形相交并显示结果。
    // 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());
        }
    }
    

    输出:

  2. Java程序创建 Rectangle2D 的两个对象并显示其详细信息并检查它们是否相互交叉:该程序创建两个名为rectangle_1rectangle_2的 Rectangle2D 对象,其参数为minXminYheightwidth 。使用display函数显示 Rectangle2D 对象的详细信息。显示函数使用getMinX()getMinY()getMaxX()getMaxY()getHeight()getWidth()显示矩形的左上角坐标和矩形的右下角及其宽度和高度函数。我们将使用 intersects函数检查它是否与另一个矩形相交并显示结果。
    // 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());
        }
    }
    

    输出:

注意:上述程序可能无法在在线 IDE 中运行。请使用离线编译器。

参考: https://docs.oracle.com/javase/8/javafx/api/javafx/geometry/Rectangle2D.html