Skip to main content

Point class in java

The java.awt.Point class is a part of the Java Abstract Window Toolkit (AWT) package, which provides a set of APIs for creating and managing graphical user interfaces (GUIs) in Java. The Point class represents a location in a two-dimensional coordinate system using integer precision.

The Point class has two public fields, x and y, which represent the horizontal and vertical coordinates, respectively. The class provides several methods for manipulating these fields, such as getLocation() and setLocation(), which return or set the coordinates of the Point object, respectively.

In addition to these basic methods, the Point class also provides several convenience methods for performing common operations on points, such as distance() and move(). The distance() method calculates the distance between two points, while the move() method moves the point to a new location based on a specified offset.

The Point class is often used in conjunction with other AWT classes to create graphical user interfaces. For example, it is commonly used to specify the location of a component within a container, or to define the start and end points of a line or shape.

Here are all the methods of the java.awt.Point class:

  • Point() - Constructs a new Point object at (0,0).
  • Point(int x, int y) - Constructs a new Point object with the specified coordinates.
  • Point(Point p) - Constructs a new Point object that is a copy of the specified Point.
  • double distance(double px, double py) - Calculates the distance between this Point and the specified coordinates.
  • double distance(Point pt) - Calculates the distance between this Point and the specified Point.
  • double distanceSq(double px, double py) - Calculates the square of the distance between this Point and the specified coordinates.
  • double distanceSq(Point pt) - Calculates the square of the distance between this Point and the specified Point.
  • boolean equals(Object obj) - Returns true if the specified object is a Point with the same coordinates as this Point, and false otherwise.
  • int hashCode() - Returns a hash code value for this Point.
  • int x - The X coordinate of the Point.
  • int y - The Y coordinate of the Point.
  • void move(int x, int y) - Moves this Point to the specified coordinates.
  • void setLocation(int x, int y) - Sets the location of this Point to the specified coordinates.
  • void setLocation(double x, double y) - Sets the location of this Point to the specified coordinates.
  • void setLocation(Point p) - Sets the location of this Point to the same coordinates as the specified Point.
  • void setLocation(double x, double y) - Sets the location of this Point to the specified coordinates.
  • Point getLocation() - Returns a new Point object with the same coordinates as this Point.
  • void translate(int dx, int dy) - Translates this Point by the specified amount.
  • String toString() - Returns a string representation of this Point.

Declaration of this class as follows.

public class Point 
extends java.awt.geom.Point2D 
implements java.io.Serializable

This reference is belong to javadoc

Public Constructors

There is two types of public constructor is defined inside this class there syntax as follows.

public java.awt.Point(java.awt.Point)
public java.awt.Point(int,int)
public java.awt.Point()

Example of those constructor.

import java.awt.Point;
public class Example
{
    public static void main(String[] arg)
    {
        // Create instance
        Point p1 = new Point();
        Point p2 = new Point(-3, 6);
        //Display point instance value
        System.out.println(p1);
        System.out.println(p2);
    }
}
Instance of point class in java
java.awt.Point[x=0,y=0]
java.awt.Point[x=-3,y=6]

java.awt.Point equals(Object) method example

// equals() method in java.awt.Point class
import java.awt.Point;
public class Example
{
    public static void main(String[] arg)
    {
        // Create instance
        Point p1 = new Point(-3, 6);
        Point p2 = new Point(-3, 6);
        Point p3 = new Point(1, 6);
        // Test equals method
        System.out.println(p1.equals(p2));
        System.out.println(p2.equals(p3));
        System.out.println(p3.equals(p3));
    }
}
true
false
true

java.awt.Point getLocation() method example

// getLocation() method in java.awt.Point class
import java.awt.Point;
public class Example
{
    public static void main(String[] arg)
    {
        // Create instance
        Point p1 = new Point(1, 2);
        // getLocation
        Point p2 = p1.getLocation();
        // Display value
        System.out.println(p1);
        System.out.println(p2);
    }
}
getLocation() method in Point class in java
java.awt.Point[x=1,y=2]
java.awt.Point[x=1,y=2]

java.awt.Point getX() method example

// getX() method in java.awt.Point class 
import java.awt.Point;
public class Example
{
    public static void main(String[] arg)
    {
        // Create instance
        Point p1 = new Point(4, 2);
        Point p2 = new Point(10, 20);
        // Display x instance value
        System.out.println(p1.getX());
        System.out.println(p2.getX());
    }
}
4.0
10.0

java.awt.Point getY() method example

// getY() method in java.awt.Point class 
import java.awt.Point;
public class Example
{
    public static void main(String[] arg)
    {
        // Create instance
        Point p1 = new Point(4, 2);
        Point p2 = new Point(10, 20);
        // Display y instance value
        System.out.println(p1.getY());
        System.out.println(p2.getY());
    }
}
2.0
20.0

java.awt.Point move(int, int) method example

// move(int,int) method in java.awt.Point class 
import java.awt.Point;
public class Example
{
    public static void main(String[] arg)
    {
        // Create instance
        Point p1 = new Point(4, 2);
        Point p2 = new Point(10, 20);
        System.out.println("Before Move");
        System.out.println(p1);
        System.out.println(p2);
        p1.move(3, 45);
        p2.move(6, 53);
        System.out.println("After Move");
        System.out.println(p1);
        System.out.println(p2);
    }
}
Before Move
java.awt.Point[x=4,y=2]
java.awt.Point[x=10,y=20]
After Move
java.awt.Point[x=3,y=45]
java.awt.Point[x=6,y=53]

java.awt.Point setLocation(int, int) method example

// setLocation(int,int) method in java.awt.Point class 
import java.awt.Point;
public class Example
{
    public static void main(String[] arg)
    {
        // Create instance
        Point p1 = new Point(4, 2);
        // getLocation method
        Point p2 = p1.getLocation();
        // setLocation method
        p1.setLocation(10, 20);
        System.out.println(p1);
        System.out.println(p2);
    }
}
java.awt.Point[x=10,y=20]
java.awt.Point[x=4,y=2]

java.awt.Point setLocation(double, double) method example

// setLocation(double,double) method in java.awt.Point class 
import java.awt.Point;
public class Example
{
    public static void main(String[] arg)
    {
        // Create instance
        Point p1 = new Point(4, 2);
        Point p2 = new Point(6, 8);
        // setLocation method
        p1.setLocation(10.8, 20.4);
        p2.setLocation(5.3, 6.7);
        System.out.println(p1);
        System.out.println(p2);
    }
}
java.awt.Point[x=11,y=20]
java.awt.Point[x=5,y=7]

java.awt.Point setLocation(Point) method example

// setLocation(Point) method in java.awt.Point class 
import java.awt.Point;
public class Example
{
    public static void main(String[] arg)
    {
        // Create instance
        Point p1 = new Point(4, 2);
        Point p2 = new Point(10, 20);
        System.out.println("Before setLocation");
        System.out.println(p1);
        System.out.println(p2);
        // setLocation method
        p2.setLocation(p1);
        System.out.println("After setLocation");
        System.out.println(p1);
        System.out.println(p2);
    }
}
Before setLocation
java.awt.Point[x=4,y=2]
java.awt.Point[x=10,y=20]
After setLocation
java.awt.Point[x=4,y=2]
java.awt.Point[x=4,y=2]

java.awt.Point toString() method example

// toString() method in java.awt.Point class 
import java.awt.Point;
public class Example
{
    public static void main(String[] arg)
    {
        // Create instance
        Point p1 = new Point(4, 2);
        Point p2 = new Point(6, 8);
        System.out.println(p1.toString());
        System.out.println(p2.toString());
    }
}
java.awt.Point[x=4,y=2]
java.awt.Point[x=6,y=8]

java.awt.Point translate(int, int) method example

// translate(int, int) method in java.awt.Point class 
import java.awt.Point;
public class Example
{
    public static void main(String[] arg)
    {
        // Create instance
        Point p1 = new Point(4, 2);
        Point p2 = new Point(6, 8);
        System.out.println("Before translate");
        System.out.println(p1);
        System.out.println(p2);
        p1.translate(13, 66); // 4+13, 2+66
        p2.translate(3, 5); // 6+3, 8+5
        System.out.println("After translate");
        System.out.println(p1);
        System.out.println(p2);
    }
}
Before translate
java.awt.Point[x=4,y=2]
java.awt.Point[x=6,y=8]
After translate
java.awt.Point[x=17,y=68]
java.awt.Point[x=9,y=13]




Comment

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