Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaJavaFX | Point2D Class

JavaFX | Point2D Class

Point2D class is a part of JavaFX. This class defines a 2-dimensional point in space. The Point2D class represents a 2D point by its x, y coordinates. It inherits java.lang.Object class.
Constructor of the class: 
 

  • Point2D(double x, double y): Create a point2D object with specified x and y coordinates.

Commonly Used Methods:

Method Explanation
distance(double x1, double y1) Calculates the distance between this point and point (x1, y1).
distance(Point2D p) Calculates the distance between this point and point p.
equals(java.lang.Object obj) Returns whether this object is equal to the specified object or not
getX() Returns the x coordinate of the point
getY() Returns the y coordinate of the point
hashCode() Returns a hash code value for the point.

Below programs will illustrate the use of the Point2D class: 
 

1.Java program to create a point 2D object and display its coordinates and find its distance from origin: In this program we create a Point2D object named point2d_1 by using its x, y coordinates as arguments. We will get the values of x, y using the getX(), getY() function and after that display it. We are also calculating the distance of the points from the origin.

Java




// 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));
    }
}


Output: 

x coordinate = 20.0, y coordinate = 150.0
distance from origin = 151.32745950421557

2.Java program to create 3 Point2D objects and display their coordinates and distance from the origin and check which of the 3 points are similar and their distances between two points: In this program we create 3 Point2D object named point2d_1, point2d_2, point2d_3 by passing its x, y coordinates as arguments. We get the x, y values using the getX(), getY() function and then display it. We are also calculating the distance of the point from the origin and displaying it for each of the three points. We are also displaying whether any two points are equal or not using equals() function and the distance between two points using the distance() function.
 

Java




// 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("Distance between point 1 and point 2 = "
                                  + point2d_1.distance(point2d_2));
 
        System.out.println("Distance between point 2 and point 3 = "
                                   + point2d_2.distance(point2d_3));
 
        System.out.println("Distance 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));
    }
}


Output: 

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
Distance between point 1 and point 2 = 0.0
Distance between point 2 and point 3 = 106.30145812734649
Distance between point 3 and point 1 = 106.30145812734649

Note: The above programs might not run in an online IDE. Please use an offline compiler.
Reference: https://docs.oracle.com/javafx/2/api/javafx/geometry/Point2D.html
 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments