1 /* Copyright (C) 1999, 2002 Free Software Foundation
3 This file is part of libjava.
5 This software is copyrighted work licensed under the terms of the
6 Libjava License. Please consult the file "LIBJAVA_LICENSE" for
10 import java.awt.geom.Point2D;
12 /* Written using "Java Class Libraries", 2nd edition, plus online
13 * API docs for JDK 1.2 beta from http://www.javasoft.com.
14 * Status: Believed complete and correct, except that neither toString
15 * nor hashCode have been compared with JDK output.
19 * This class represents a point on the screen using cartesian coordinates.
21 * @author Per Bothner <bothner@cygnus.com>
22 * @author Aaron M. Renn (arenn@urbanophile.com)
23 * @date February 8, 1999.
25 public class Point extends Point2D implements java.io.Serializable
28 * @serial The X coordinate of the point.
33 * @serial The Y coordinate of the point.
38 * Initializes a new instance of <code>Point</code> representing the
44 * Initializes a new instance of <code>Point</code> with coordinates
45 * identical to the coordinates of the specified points.
47 * @param point The point to copy the coordinates from.
49 public Point (Point p) { this.x = p.x; this.y = p.y; }
52 * Initializes a new instance of <code>Point</code> with the specified
55 * @param x The X coordinate of this point.
56 * @param y The Y coordinate of this point.
58 public Point (int x, int y) { this.x = x; this.y = y; }
61 * Tests whether or not this object is equal to the specified object.
62 * This will be true if and only if the specified objectj:
65 * <li>Is not <code>null</code>.
66 * <li>Is an instance of <code>Point</code>.
67 * <li>Has X and Y coordinates equal to this object's.
70 * @param obj The object to test against for equality.
72 * @return <code>true</code> if the specified object is equal to this
73 * object, <code>false</code> otherwise.
75 public boolean equals (Object obj)
77 if (! (obj instanceof Point))
79 Point p = (Point) obj;
80 return this.x == p.x && this.y == p.y;
84 * Returns a hash value for this point.
86 * @param A hash value for this point.
88 public int hashCode () { return x ^ y; }
91 * Returns the location of this object as a point. A pretty useless
92 * method. It is included to mimic the <code>getLocation</code> method
97 public Point getLocation () { return new Point(this); }
100 * Sets this object's coordinates to the specified values. This method
101 * is identical to the <code>setLocation(int, int)</code> method.
103 * @param x The new X coordinate.
104 * @param y The new Y coordinate.
106 public void move (int x, int y) { this.x = x; this.y = y; }
109 * Sets this object's coordinates to the specified values. This method
110 * is identical to the <code>move()</code> method.
112 * @param x The new X coordinate.
113 * @param y The new Y coordinate.
115 public void setLocation (int x, int y) { this.x = x; this.y = y; }
118 * Sets this object's coordinates to match those of the specified point.
120 * @param point The point to copy the coordinates from.
122 public void setLocation (Point pt) { this.x = pt.x; this.y = pt.y; }
125 * Changes the coordinates of this point such that the specified
126 * <code>dx</code> parameter is added to the existing X coordinate and
127 * <code>dy</code> is added to the existing Y coordinate.
129 * @param dx The amount to add to the X coordinate.
130 * @param dy The amount to add to the Y coordinate.
132 public void translate (int x, int y) { this.x += x; this.y += y; }
135 * Returns a string representation of this object.
137 * @return A string representation of this object.
139 public String toString ()
141 return getClass().getName() + "[x:"+x+",y:"+y+']';
144 public double getX() { return x; }
145 public double getY() { return y; }
147 public void setLocation (double x, double y)
148 { this.x = (int) x; this.y = (int) y; }