JavaFX |矩形2D类

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

null

类的构造函数:

  • 矩形2D(双minX、双minY、双宽度、双高度) :创建具有指定宽度高度和矩形左上角坐标的新矩形2D

常用方法:

方法 解释
包含(双x,双y) 检查指定的坐标是否在矩形2D的边界内。
包含(双x、双y、双w、双h) 检查指定的矩形是否位于给定矩形内
包含(点2D p) 检查指定的点2D坐标是否在矩形2D的边界内。
包含(矩形器) 检查指定的矩形2D是否位于给定矩形内
getHeight() 返回矩形的高度
getWidth() 返回矩形的宽度
getMaxX() 此矩形2D右下角的x坐标。
getMinX() 矩形2D左上角的x坐标。
getMaxY() 此矩形2D右下角的y坐标。
getMinY() 此矩形2D左上角的y坐标。
相交(双x,双y,双w,双h) 检查指定的矩形是否与矩形2D对象相交
相交(矩形) 检查指定的矩形2D(r)是否与矩形2D对象相交

下面的程序演示了矩形2D类的使用:

  1. Java程序,用于创建矩形2D对象并显示其详细信息,以及是否包含点,并检查其是否与矩形相交: 该程序创建一个名为 长方形 具有 貂皮 , 米尼 , 身高 宽度 作为参数。使用“显示”功能显示矩形2D对象的详细信息。显示功能使用 getMinX() , getMinY() , getMaxX() , getMaxY() , getHeight() getWidth() 作用我们将使用 包含() 函数,还可以使用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());
    }
    }

    
    

    输出:

    矩形的左上角点为=100.0,100.0 矩形的右下角点为=200.0,200.0 矩形的宽度和高度为=100.0,100.0 矩形包含点150,150=真 矩形包含点50,50=false 该矩形与另一个宽度为100、高度为100、最小值为50和最小值为50的矩形相交:true

  2. Java程序,用于创建两个矩形2D对象,并显示其细节,并检查其是否相交: 该程序创建两个名为 矩形_1 矩形_2 具有 貂皮 , 米尼 , 身高 宽度 作为参数。矩形2D对象的详细信息将使用 显示功能 .显示功能使用 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());
    }
    }

    
    

    输出:

    矩形_1细节 矩形的左上角点为=100.0,100.0 矩形的右下角点为=200.0,200.0 矩形的宽度和高度为=100.0,100.0

    矩形图2详细信息 矩形的左上角点为=100.0,100.0 矩形的右下角点为=200.0,200.0 矩形的宽度和高度为=100.0,100.0 这两个矩形相交=真

注: 上述程序可能无法在联机IDE中运行。请使用脱机编译器。

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

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享