001: /*
002: * Copyright 1995-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package java.awt;
027:
028: import java.awt.geom.Point2D;
029:
030: /**
031: * A point representing a location in {@code (x,y)} coordinate space,
032: * specified in integer precision.
033: *
034: * @version 1.46, 05/05/07
035: * @author Sami Shaio
036: * @since 1.0
037: */
038: public class Point extends Point2D implements java.io.Serializable {
039: /**
040: * The X coordinate of this <code>Point</code>.
041: * If no X coordinate is set it will default to 0.
042: *
043: * @serial
044: * @see #getLocation()
045: * @see #move(int, int)
046: * @since 1.0
047: */
048: public int x;
049:
050: /**
051: * The Y coordinate of this <code>Point</code>.
052: * If no Y coordinate is set it will default to 0.
053: *
054: * @serial
055: * @see #getLocation()
056: * @see #move(int, int)
057: * @since 1.0
058: */
059: public int y;
060:
061: /*
062: * JDK 1.1 serialVersionUID
063: */
064: private static final long serialVersionUID = -5276940640259749850L;
065:
066: /**
067: * Constructs and initializes a point at the origin
068: * (0, 0) of the coordinate space.
069: * @since 1.1
070: */
071: public Point() {
072: this (0, 0);
073: }
074:
075: /**
076: * Constructs and initializes a point with the same location as
077: * the specified <code>Point</code> object.
078: * @param p a point
079: * @since 1.1
080: */
081: public Point(Point p) {
082: this (p.x, p.y);
083: }
084:
085: /**
086: * Constructs and initializes a point at the specified
087: * {@code (x,y)} location in the coordinate space.
088: * @param x the X coordinate of the newly constructed <code>Point</code>
089: * @param y the Y coordinate of the newly constructed <code>Point</code>
090: * @since 1.0
091: */
092: public Point(int x, int y) {
093: this .x = x;
094: this .y = y;
095: }
096:
097: /**
098: * {@inheritDoc}
099: * @since 1.2
100: */
101: public double getX() {
102: return x;
103: }
104:
105: /**
106: * {@inheritDoc}
107: * @since 1.2
108: */
109: public double getY() {
110: return y;
111: }
112:
113: /**
114: * Returns the location of this point.
115: * This method is included for completeness, to parallel the
116: * <code>getLocation</code> method of <code>Component</code>.
117: * @return a copy of this point, at the same location
118: * @see java.awt.Component#getLocation
119: * @see java.awt.Point#setLocation(java.awt.Point)
120: * @see java.awt.Point#setLocation(int, int)
121: * @since 1.1
122: */
123: public Point getLocation() {
124: return new Point(x, y);
125: }
126:
127: /**
128: * Sets the location of the point to the specified location.
129: * This method is included for completeness, to parallel the
130: * <code>setLocation</code> method of <code>Component</code>.
131: * @param p a point, the new location for this point
132: * @see java.awt.Component#setLocation(java.awt.Point)
133: * @see java.awt.Point#getLocation
134: * @since 1.1
135: */
136: public void setLocation(Point p) {
137: setLocation(p.x, p.y);
138: }
139:
140: /**
141: * Changes the point to have the specified location.
142: * <p>
143: * This method is included for completeness, to parallel the
144: * <code>setLocation</code> method of <code>Component</code>.
145: * Its behavior is identical with <code>move(int, int)</code>.
146: * @param x the X coordinate of the new location
147: * @param y the Y coordinate of the new location
148: * @see java.awt.Component#setLocation(int, int)
149: * @see java.awt.Point#getLocation
150: * @see java.awt.Point#move(int, int)
151: * @since 1.1
152: */
153: public void setLocation(int x, int y) {
154: move(x, y);
155: }
156:
157: /**
158: * Sets the location of this point to the specified double coordinates.
159: * The double values will be rounded to integer values.
160: * Any number smaller than <code>Integer.MIN_VALUE</code>
161: * will be reset to <code>MIN_VALUE</code>, and any number
162: * larger than <code>Integer.MAX_VALUE</code> will be
163: * reset to <code>MAX_VALUE</code>.
164: *
165: * @param x the X coordinate of the new location
166: * @param y the Y coordinate of the new location
167: * @see #getLocation
168: */
169: public void setLocation(double x, double y) {
170: this .x = (int) Math.floor(x + 0.5);
171: this .y = (int) Math.floor(y + 0.5);
172: }
173:
174: /**
175: * Moves this point to the specified location in the
176: * {@code (x,y)} coordinate plane. This method
177: * is identical with <code>setLocation(int, int)</code>.
178: * @param x the X coordinate of the new location
179: * @param y the Y coordinate of the new location
180: * @see java.awt.Component#setLocation(int, int)
181: */
182: public void move(int x, int y) {
183: this .x = x;
184: this .y = y;
185: }
186:
187: /**
188: * Translates this point, at location {@code (x,y)},
189: * by {@code dx} along the {@code x} axis and {@code dy}
190: * along the {@code y} axis so that it now represents the point
191: * {@code (x+dx,y+dy)}.
192: *
193: * @param dx the distance to move this point
194: * along the X axis
195: * @param dy the distance to move this point
196: * along the Y axis
197: */
198: public void translate(int dx, int dy) {
199: this .x += dx;
200: this .y += dy;
201: }
202:
203: /**
204: * Determines whether or not two points are equal. Two instances of
205: * <code>Point2D</code> are equal if the values of their
206: * <code>x</code> and <code>y</code> member fields, representing
207: * their position in the coordinate space, are the same.
208: * @param obj an object to be compared with this <code>Point2D</code>
209: * @return <code>true</code> if the object to be compared is
210: * an instance of <code>Point2D</code> and has
211: * the same values; <code>false</code> otherwise.
212: */
213: public boolean equals(Object obj) {
214: if (obj instanceof Point) {
215: Point pt = (Point) obj;
216: return (x == pt.x) && (y == pt.y);
217: }
218: return super .equals(obj);
219: }
220:
221: /**
222: * Returns a string representation of this point and its location
223: * in the {@code (x,y)} coordinate space. This method is
224: * intended to be used only for debugging purposes, and the content
225: * and format of the returned string may vary between implementations.
226: * The returned string may be empty but may not be <code>null</code>.
227: *
228: * @return a string representation of this point
229: */
230: public String toString() {
231: return getClass().getName() + "[x=" + x + ",y=" + y + "]";
232: }
233: }
|