/*
* @(#)Point.java 1.38 03/12/19
*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.awt;
import java.awt.geom.Point2D;
/**
* A point representing a location in (x, y) coordinate space, specified
* in integer precision.
*
* @version 1.38, 12/19/03
* @author Sami Shaio
* @since JDK1.0
*/
public class Point extends Point2D implements java.io.Serializable {
/**
* The x coordinate.
* If no x coordinate is set it will default to 0.
*
* @serial
* @see #getLocation()
* @see #move(int, int)
*/
public int x;
/**
* The y coordinate.
* If no y coordinate is set it will default to 0.
*
* @serial
* @see #getLocation()
* @see #move(int, int)
*/
public int y;
/*
* JDK 1.1 serialVersionUID
*/
private static final long serialVersionUID = -5276940640259749850L;
/**
* Constructs and initializes a point at the origin
* (0, 0) of the coordinate space.
* @since JDK1.1
*/
public Point() {
this(0, 0);
}
/**
* Constructs and initializes a point with the same location as
* the specified Point
object.
* @param p a point
* @since JDK1.1
*/
public Point(Point p) {
this(p.x, p.y);
}
/**
* Constructs and initializes a point at the specified
* (x, y) location in the coordinate space.
* @param x the x coordinate
* @param y the y coordinate
*/
public Point(int x, int y) {
this.x = x;
this.y = y;
}
/**
* Returns the X coordinate of the point in double precision.
* @return the X coordinate of the point in double precision
*/
public double getX() {
return x;
}
/**
* Returns the Y coordinate of the point in double precision.
* @return the Y coordinate of the point in double precision
*/
public double getY() {
return y;
}
/**
* Returns the location of this point.
* This method is included for completeness, to parallel the
* getLocation
method of Component
.
* @return a copy of this point, at the same location
* @see java.awt.Component#getLocation
* @see java.awt.Point#setLocation(java.awt.Point)
* @see java.awt.Point#setLocation(int, int)
* @since JDK1.1
*/
public Point getLocation() {
return new Point(x, y);
}
/**
* Sets the location of the point to the specified location.
* This method is included for completeness, to parallel the
* setLocation
method of Component
.
* @param p a point, the new location for this point
* @see java.awt.Component#setLocation(java.awt.Point)
* @see java.awt.Point#getLocation
* @since JDK1.1
*/
public void setLocation(Point p) {
setLocation(p.x, p.y);
}
/**
* Changes the point to have the specified location.
*
* This method is included for completeness, to parallel the
* setLocation
method of Component
.
* Its behavior is identical with move(int, int)
.
* @param x the x coordinate of the new location
* @param y the y coordinate of the new location
* @see java.awt.Component#setLocation(int, int)
* @see java.awt.Point#getLocation
* @see java.awt.Point#move(int, int)
* @since JDK1.1
*/
public void setLocation(int x, int y) {
move(x, y);
}
/**
* Sets the location of this point to the specified double coordinates.
* The double values will be rounded to integer values.
* Any number smaller than Integer.MIN_VALUE
* will be reset to MIN_VALUE
, and any number
* larger than Integer.MAX_VALUE
will be
* reset to MAX_VALUE
.
*
* @param x the x coordinate of the new location
* @param y the y coordinate of the new location
* @see #getLocation
*/
public void setLocation(double x, double y) {
this.x = (int) Math.floor(x+0.5);
this.y = (int) Math.floor(y+0.5);
}
/**
* Moves this point to the specified location in the
* (x, y) coordinate plane. This method
* is identical with setLocation(int, int)
.
* @param x the x coordinate of the new location
* @param y the y coordinate of the new location
* @see java.awt.Component#setLocation(int, int)
*/
public void move(int x, int y) {
this.x = x;
this.y = y;
}
/**
* Translates this point, at location (x, y),
* by dx
along the x axis and dy
* along the y axis so that it now represents the point
* (x
+
dx
,
* y
+
dy
).
* @param dx the distance to move this point
* along the x axis
* @param dy the distance to move this point
* along the y axis
*/
public void translate(int dx, int dy) {
this.x += dx;
this.y += dy;
}
/**
* Determines whether or not two points are equal. Two instances of
* Point2D
are equal if the values of their
* x
and y
member fields, representing
* their position in the coordinate space, are the same.
* @param obj an object to be compared with this Point2D
* @return true
if the object to be compared is
* an instance of Point2D
and has
* the same values; false
otherwise.
*/
public boolean equals(Object obj) {
if (obj instanceof Point) {
Point pt = (Point)obj;
return (x == pt.x) && (y == pt.y);
}
return super.equals(obj);
}
/**
* Returns a string representation of this point and its location
* in the (x, y) coordinate space. This method is
* intended to be used only for debugging purposes, and the content
* and format of the returned string may vary between implementations.
* The returned string may be empty but may not be null
.
*
* @return a string representation of this point
*/
public String toString() {
return getClass().getName() + "[x=" + x + ",y=" + y + "]";
}
}