001: /*
002: * @(#)Point.java 1.23 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027: package java.awt;
028:
029: /**
030: * The <code>Point</code> class represents a location in a
031: * two-dimensional (<i>x</i>, <i>y</i>) coordinate space.
032: *
033: * @version 1.18, 08/19/02
034: * @author Sami Shaio
035: * @since JDK1.0
036: */
037: public class Point implements java.io.Serializable, Cloneable {
038: /**
039: * The <i>x</i> coordinate.
040: * @since JDK1.0
041: */
042: public int x;
043: /**
044: * The <i>y</i> coordinate.
045: * @since JDK1.0
046: */
047: public int y;
048: /*
049: * JDK 1.1 serialVersionUID
050: */
051: private static final long serialVersionUID = -5276940640259749850L;
052:
053: /**
054: * Constructs and initializes a point at the origin
055: * (0, 0) of the coordinate space.
056: * @param x the <i>x</i> coordinate.
057: * @param y the <i>y</i> coordinate.
058: * @since JDK1.1
059: */
060: public Point() {
061: this (0, 0);
062: }
063:
064: /**
065: * Constructs and initializes a point with the same location as
066: * the specified <code>Point</code> object.
067: * @param p a point.
068: * @since JDK1.1
069: */
070: public Point(Point p) {
071: this (p.x, p.y);
072: }
073:
074: /**
075: * Constructs and initializes a point at the specified
076: * (<i>x</i>, <i>y</i>) location in the coordinate space.
077: * @param x the <i>x</i> coordinate.
078: * @param y the <i>y</i> coordinate.
079: * @since JDK1.0
080: */
081: public Point(int x, int y) {
082: this .x = x;
083: this .y = y;
084: }
085:
086: /**
087: * Returns the location of this point.
088: * This method is included for completeness, to parallel the
089: * <code>getLocation</code> method of <code>Component</code>.
090: * @return a copy of this point, at the same location.
091: * @see java.awt.Component#getLocation
092: * @see java.awt.Point#setLocation(java.awt.Point)
093: * @see java.awt.Point#setLocation(int, int)
094: * @since JDK1.1
095: */
096: public Point getLocation() {
097: return new Point(x, y);
098: }
099:
100: /**
101: * Sets the location of the point to the specificed location.
102: * This method is included for completeness, to parallel the
103: * <code>setLocation</code> method of <code>Component</code>.
104: * @param p a point, the new location for this point.
105: * @see java.awt.Component#setLocation(java.awt.Point)
106: * @see java.awt.Point#getLocation
107: * @since JDK1.1
108: */
109: public void setLocation(Point p) {
110: setLocation(p.x, p.y);
111: }
112:
113: /**
114: * Changes the point to have the specificed location.
115: * <p>
116: * This method is included for completeness, to parallel the
117: * <code>setLocation</code> method of <code>Component</code>.
118: * Its behavior is identical with <code>move(int, int)</code>.
119: * @param x the <i>x</i> coordinate of the new location.
120: * @param y the <i>y</i> coordinate of the new location.
121: * @see java.awt.Component#setLocation(int, int)
122: * @see java.awt.Point#getLocation
123: * @see java.awt.Point#move(int, int)
124: * @since JDK1.1
125: */
126: public void setLocation(int x, int y) {
127: move(x, y);
128: }
129:
130: /**
131: * Moves this point to the specificed location in the
132: * (<i>x</i>, <i>y</i>) coordinate plane. This method
133: * is identical with <code>setLocation(int, int)</code>.
134: * @param x the <i>x</i> coordinate of the new location.
135: * @param y the <i>y</i> coordinate of the new location.
136: * @see java.awt.Component#setLocation(int, int)
137: * @since JDK1.0
138: */
139: public void move(int x, int y) {
140: this .x = x;
141: this .y = y;
142: }
143:
144: /**
145: * Translates this point, at location (<i>x</i>, <i>y</i>),
146: * by <code>dx</code> along the <i>x</i> axis and <code>dy</code>
147: * along the <i>y</i> axis so that it now represents the point
148: * (<code>x</code> <code>+</code> <code>dx</code>,
149: * <code>y</code> <code>+</code> <code>dy</code>).
150: * @param dx the distance to move this point
151: * along the <i>x</i> axis.
152: * @param dy the distance to move this point
153: * along the <i>y</i> axis.
154: * @since JDK1.0
155: */
156: public void translate(int x, int y) {
157: this .x += x;
158: this .y += y;
159: }
160:
161: /**
162: * Returns the hashcode for this point.
163: * @return a hash code for this point.
164: * @since JDK1.0
165: */
166: public int hashCode() {
167: return x ^ (y * 31);
168: }
169:
170: /**
171: * Determines whether two points are equal. Two instances of
172: * <code>Point</code> are equal if the values of their
173: * <code>x</code> and <code>y</code> member fields, representing
174: * their position in the coordinate space, are the same.
175: * @param obj an object to be compared with this point.
176: * @return <code>true</code> if the object to be compared is
177: * an instance of <code>Point</code> and has
178: * the same values; <code>false</code> otherwise.
179: * @since JDK1.0
180: */
181: public boolean equals(Object obj) {
182: if (obj instanceof Point) {
183: Point pt = (Point) obj;
184: return (x == pt.x) && (y == pt.y);
185: }
186: return false;
187: }
188:
189: /**
190: * Returns a representation of this point and its location
191: * in the (<i>x</i>, <i>y</i>) coordinate space as a string.
192: * @return a string representation of this point,
193: * including the values of its member fields.
194: * @since JDK1.0
195: */
196: public String toString() {
197: return getClass().getName() + "[x=" + x + ",y=" + y + "]";
198: }
199:
200: /**
201: * Creates a new object of the same class and with the
202: * same contents as this object.
203: * @return a clone of this instance.
204: * @exception OutOfMemoryError if there is not enough memory.
205: * @see java.lang.Cloneable
206: * @since 1.2
207: */
208: public Object clone() {
209: try {
210: return super .clone();
211: } catch (CloneNotSupportedException e) {
212: // this shouldn't happen, since we are Cloneable
213: throw new InternalError();
214: }
215: }
216:
217: }
|