Point2D class in java

java.awt.geom.Point2D is a class in java programming language. Inside of this class exists 13 public methods. Declaration of this class as follows.

public abstract class java.awt.geom.Point2D 
implements java.lang.Cloneable

Point2D public method

There are following useful methods which is define the inside of java.awt.geom.Point2D class.

MethodDescription
Object clone() It creates a new object of the same class and with the same contents as this object.
double distance(double px, double py) It returns the distance from this to a specified point.
static double distance(double x1, double y1, double x2, double y2) It returns the distance between two points.
double distance(Point2D pt) It returns the distance from this to a specified Point2D.
double distanceSq(double px, double py) It returns the square of the distance from this to a specified point.
static double distanceSq(double x1, double y1, double x2, double y2) It returns the square of the distance between two points.
double distanceSq(Point2D pt) It returns the square of the distance from this to a specified Point2D.
boolean equals(Object obj) It determines whether or not two points are equal.
abstract double getX() It returns the X coordinate of this in double precision.
abstract double getY() It returns the Y coordinate of this in double precision.
int hashCode() It returns the hashcode for this Point2D.
abstract void setLocation(double x, double y) It sets the location of this to the specified double coordinates.
void setLocation(Point2D p) It sets the location of this to the same coordinates as the specified Point2D object.

This reference is belong to javadoc

Point2D is abstract class, Variable of this class capable to hold the reference of subclass (Point, Point2D.Double, Point2D.Float). For example.

java.awt.Point Example

import java.awt.geom.Point2D;
import java.awt.Point;
public class Example
{
    public static void main(String[] argv)
    {
        // Create an instance
        Point2D point = new Point(50, 60);
        System.out.println(point);
    }
}
java.awt.Point instance
java.awt.Point[x=50,y=60]

java.awt.geom.Point2D.Float Example

import java.awt.geom.Point2D;
public class Example
{
    public static void main(String[] argv)
    {
        // Create an instance using Point2D.Float
        Point2D point = new Point2D.Float(15.3f, 40.4f);
        System.out.println(point);
    }
}
Point2D.Float instance
Point2D.Float[15.3, 40.4]

java.awt.geom.Point2D.Double Example

import java.awt.geom.Point2D;
public class Example
{
    public static void main(String[] argv)
    {
        // Create an instance using Point2D.Double
        Point2D point = new Point2D.Double(50.20, 60.55);
        System.out.println(point);
    }
}
Point2D.Double instance
Point2D.Double[50.2, 60.55]

java.awt.geom.Point2D clone() method example

// clone() method in java.awt.geom.Point2D class 
// Useful packages
import java.awt.geom.Point2D;
class Example {
    public static void main(String[] args) {
        
        // Create Instance
        Point2D.Double  p1 = new Point2D.Double(30.20, 50.35);
        Object   p2 = p1.clone();
        
        // After clone Display result 
        System.out.println(p1);
        System.out.println(p2);

        // Change value
        p1.x = 999.20;

        // After update Display result 
        System.out.println(p1);
        System.out.println(p2);
    }
}
Point2D clone method
Point2D.Double[30.2, 50.35]
Point2D.Double[30.2, 50.35]
Point2D.Double[999.2, 50.35]
Point2D.Double[30.2, 50.35]


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved