Point2D類是JavaFX的一部分。此類定義空間中的二維點。 Point2D類通過其x,y坐標表示2D點。它繼承了java.lang.Object類。
該類的構造函數:
- Point2D(double x, double y):創建具有指定x和y坐標的point2D對象。
常用方法:
方法 | 說明 |
---|---|
distance(double x1, double y1) | 計算此點與點(x1,y1)之間的距離。 |
distance(Point2D p) | 計算該點與點p之間的距離。 |
equals(java.lang.Object obj) | 返回此對象是否等於指定的對象 |
getX() | 返回該點的x坐標 |
getY() | 返回該點的y坐標 |
hashCode() | 返回該點的哈希碼值。 |
下麵的程序將說明Point2D類的用法:
- Java程序創建點2D對象並顯示其坐標並找到其距原點的距離:在此程序中,我們通過使用其x,y坐標作為參數來創建一個名為point2d_1的Point2D對象。我們將使用getX(),getY()函數獲得x,y的值,然後顯示它。我們還在計算點到原點的距離。
// Java program to create a point 2D // object and display its coordinates // and find its distance from origin import javafx.geometry.Point2D; public class Point2D_1 { // Main Method public static void main(String args[]) { // Create a point2D object Point2D point2d_1 = new Point2D(20.0f, 150.0f); double x, y; // get the coordinates of the point x = point2d_1.getX(); y = point2d_1.getY(); // display the coordinates of the point System.out.println("x coordinate = " + x + ", y coordinate = " + y); // print its distance from origin System.out.println("distance from origin = " + point2d_1.distance(0, 0)); } }
輸出:
x coordinate = 20.0, y coordinate = 150.0 distance from origin = 151.32745950421557
- Java程序創建3個Point2D對象並顯示其坐標和距原點的距離,並檢查3個點中的哪個點相似以及兩個點之間的距離:在此程序中,我們通過傳遞其x,y坐標作為參數來創建3個Point2D對象,它們分別名為point2d_1,point2d_2,point2d_3。我們使用getX(),getY()函數獲得x,y值,然後顯示它。我們還在計算點到原點的距離,並為三個點中的每一個顯示它。我們還使用equals()函數顯示兩個點是否相等,以及使用distance()函數顯示兩個點之間的距離。
// Java program to create 3 Point2D objects and display // their coordinates and distance from origin and // check which of the 3 points are similar and // their distances between two points import javafx.geometry.Point2D; public class Point2D_2 { // Main Method public static void main(String args[]) { // Create three point2D objects Point2D point2d_1 = new Point2D(120.0f, 50.0f); Point2D point2d_2 = new Point2D(120.0f, 50.0f); Point2D point2d_3 = new Point2D(200.0f, 120.0f); // Display the coordinates of the 3 points display(point2d_1); display(point2d_2); display(point2d_3); // Check whether any point is equal to other or not System.out.println("Point 1 equals Point 2 = " + point2d_1.equals(point2d_2)); System.out.println("Point 2 equals Point 3 = " + point2d_2.equals(point2d_3)); System.out.println("Point 3 equals Point 1 = " + point2d_3.equals(point2d_1)); // distance between two points System.out.println("Distane between point 1 and point 2 = " + point2d_1.distance(point2d_2)); System.out.println("Distane between point 2 and point 3 = " + point2d_2.distance(point2d_3)); System.out.println("Distane between point 3 and point 1 = " + point2d_3.distance(point2d_1)); } // display method public static void display(Point2D point2d) { double x, y; // get the coordinates of the point x = point2d.getX(); y = point2d.getY(); // display the coordinates of the point System.out.println("x coordinate = " + x + ", y coordinate = " + y); // print its distance from origin System.out.println("Distance from origin = " + point2d.distance(0, 0)); } }
輸出:
x coordinate = 120.0, y coordinate = 50.0 Distance from origin = 130.0 x coordinate = 120.0, y coordinate = 50.0 Distance from origin = 130.0 x coordinate = 200.0, y coordinate = 120.0 Distance from origin = 233.23807579381202 Point 1 equals Point 2 = true Point 2 equals Point 3 = false Point 3 equals Point 1 = false Distane between point 1 and point 2 = 0.0 Distane between point 2 and point 3 = 106.30145812734649 Distane between point 3 and point 1 = 106.30145812734649
注意:以上程序可能無法在在線IDE中運行。請使用離線編譯器。
參考:https://docs.oracle.com/javafx/2/api/javafx/geometry/Point2D.html
相關用法
- JavaFX 類 Pos用法及代碼示例
- JavaFX 類 Tab用法及代碼示例
- JavaFX 類 TextAlignment用法及代碼示例
- JavaFX 類 FileChooser用法及代碼示例
- JavaFX 類 DirectoryChooser用法及代碼示例
- JavaFX 類 TextFlow用法及代碼示例
- JavaFX 類 Font用法及代碼示例
- JavaFX 類 FontWeight用法及代碼示例
- JavaFX 類 ClosePath用法及代碼示例
- JavaFX 類 Popup用法及代碼示例
- JavaFX 類 TitledPane用法及代碼示例
- JavaFX 類 SplitPane用法及代碼示例
- JavaFX 類 LineTo用法及代碼示例
- JavaFX 類 StackPane用法及代碼示例
- JavaFX 類 VLineTo用法及代碼示例
注:本文由純淨天空篩選整理自andrew1234大神的英文原創作品 JavaFX | Point2D Class。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。