Point class in java

java.awt.Point is a class in java programming language. Inside of this class exists 10 public methods. Declaration of this class as follows.

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

Point public method

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

MethodDescription
boolean equals(Object obj) It determines whether or not two points are equal.
Point getLocation() It returns the location of this point.
double getX() It returns the X coordinate of this in double precision.
double getY() It returns the Y coordinate of this in double precision.
void move(int x, int y) It moves this point to the specified location in the coordinate plane.
void setLocation(double x, double y) It sets the location of this point to the specified double coordinates.
void setLocation(int x, int y) It changes the point to have the specified location.
void setLocation(Point p) It sets the location of the point to the specified location.
String toString() It returns a string representation of this point and its location in the coordinate space.
void translate(int dx, int dy) It translates this point, at location , by dx along the x axis and dy along the y axis so that it now represents the point (x+dx,y+dy).

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]


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