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類的用法:
- 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 - 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.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
注意:以上程序可能無法在在線IDE中運行。請使用離線編譯器。
參考: https://docs.oracle.com/javase/8/javafx/api/javafx/geometry/Rectangle2D.html
相關用法
- JavaFX 類 Pos用法及代碼示例
- JavaFX 類 Tab用法及代碼示例
- JavaFX 類 FileChooser用法及代碼示例
- JavaFX 類 TextAlignment用法及代碼示例
- JavaFX 類 DirectoryChooser用法及代碼示例
- JavaFX 類 TextFlow用法及代碼示例
- JavaFX 類 FontWeight用法及代碼示例
- JavaFX 類 ClosePath用法及代碼示例
- JavaFX 類 Popup用法及代碼示例
- JavaFX 類 TitledPane用法及代碼示例
- JavaFX 類 SplitPane用法及代碼示例
- JavaFX 類 LineTo用法及代碼示例
- JavaFX 類 StackPane用法及代碼示例
- JavaFX 類 VLineTo用法及代碼示例
- JavaFX 類 VBox用法及代碼示例
注:本文由純淨天空篩選整理自andrew1234大神的英文原創作品 JavaFX | Rectangle2D Class。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。