当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


JavaFX 类 Rectangle2D用法及代码示例


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

该类的构造函数:

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

常用方法:


方法 说明
contains(double x, double y) 检查指定的坐标是否在Rectangle2D的边界内。
contains(double x, double y, double w, double h) 检查指定的矩形是否位于给定的矩形内
contains(Point2D p) 检查指定的Point2D坐标是否在Rectangle2D的边界内。
contains(Rectangle2D r) 检查指定的Rectangle2D是否位于给定的矩形内
getHeight() 返回矩形的高度
getWidth() 返回矩形的宽度
getMaxX() 此Rectangle2D右下角的x坐标。
getMinX() 此Rectangle2D左上角的x坐标。
getMaxY() 此Rectangle2D右下角的y坐标。
getMinY() 此Rectangle2D左上角的y坐标。
intersects(double x, double y, double w, double h) 检查指定的矩形是否与Rectangle2D对象相交
intersects(Rectangle2D r) 检查指定的Rectangle2D(r)是否与Rectangle2D对象相交

以下示例程序旨在说明Rectangle2D类的用法:

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

    输出:

    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

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

    输出:

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

    Rectangle_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

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

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



相关用法


注:本文由纯净天空筛选整理自andrew1234大神的英文原创作品 JavaFX | Rectangle2D Class。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。