J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1995-2006 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package java.awt; |
| 27 | |
| 28 | import java.awt.geom.Point2D; |
| 29 | |
| 30 | /** |
| 31 | * A point representing a location in {@code (x,y)} coordinate space, |
| 32 | * specified in integer precision. |
| 33 | * |
| 34 | * @author Sami Shaio |
| 35 | * @since 1.0 |
| 36 | */ |
| 37 | public class Point extends Point2D implements java.io.Serializable { |
| 38 | /** |
| 39 | * The X coordinate of this <code>Point</code>. |
| 40 | * If no X coordinate is set it will default to 0. |
| 41 | * |
| 42 | * @serial |
| 43 | * @see #getLocation() |
| 44 | * @see #move(int, int) |
| 45 | * @since 1.0 |
| 46 | */ |
| 47 | public int x; |
| 48 | |
| 49 | /** |
| 50 | * The Y coordinate of this <code>Point</code>. |
| 51 | * If no Y coordinate is set it will default to 0. |
| 52 | * |
| 53 | * @serial |
| 54 | * @see #getLocation() |
| 55 | * @see #move(int, int) |
| 56 | * @since 1.0 |
| 57 | */ |
| 58 | public int y; |
| 59 | |
| 60 | /* |
| 61 | * JDK 1.1 serialVersionUID |
| 62 | */ |
| 63 | private static final long serialVersionUID = -5276940640259749850L; |
| 64 | |
| 65 | /** |
| 66 | * Constructs and initializes a point at the origin |
| 67 | * (0, 0) of the coordinate space. |
| 68 | * @since 1.1 |
| 69 | */ |
| 70 | public Point() { |
| 71 | this(0, 0); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Constructs and initializes a point with the same location as |
| 76 | * the specified <code>Point</code> object. |
| 77 | * @param p a point |
| 78 | * @since 1.1 |
| 79 | */ |
| 80 | public Point(Point p) { |
| 81 | this(p.x, p.y); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Constructs and initializes a point at the specified |
| 86 | * {@code (x,y)} location in the coordinate space. |
| 87 | * @param x the X coordinate of the newly constructed <code>Point</code> |
| 88 | * @param y the Y coordinate of the newly constructed <code>Point</code> |
| 89 | * @since 1.0 |
| 90 | */ |
| 91 | public Point(int x, int y) { |
| 92 | this.x = x; |
| 93 | this.y = y; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * {@inheritDoc} |
| 98 | * @since 1.2 |
| 99 | */ |
| 100 | public double getX() { |
| 101 | return x; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * {@inheritDoc} |
| 106 | * @since 1.2 |
| 107 | */ |
| 108 | public double getY() { |
| 109 | return y; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Returns the location of this point. |
| 114 | * This method is included for completeness, to parallel the |
| 115 | * <code>getLocation</code> method of <code>Component</code>. |
| 116 | * @return a copy of this point, at the same location |
| 117 | * @see java.awt.Component#getLocation |
| 118 | * @see java.awt.Point#setLocation(java.awt.Point) |
| 119 | * @see java.awt.Point#setLocation(int, int) |
| 120 | * @since 1.1 |
| 121 | */ |
| 122 | public Point getLocation() { |
| 123 | return new Point(x, y); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Sets the location of the point to the specified location. |
| 128 | * This method is included for completeness, to parallel the |
| 129 | * <code>setLocation</code> method of <code>Component</code>. |
| 130 | * @param p a point, the new location for this point |
| 131 | * @see java.awt.Component#setLocation(java.awt.Point) |
| 132 | * @see java.awt.Point#getLocation |
| 133 | * @since 1.1 |
| 134 | */ |
| 135 | public void setLocation(Point p) { |
| 136 | setLocation(p.x, p.y); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Changes the point to have the specified location. |
| 141 | * <p> |
| 142 | * This method is included for completeness, to parallel the |
| 143 | * <code>setLocation</code> method of <code>Component</code>. |
| 144 | * Its behavior is identical with <code>move(int, int)</code>. |
| 145 | * @param x the X coordinate of the new location |
| 146 | * @param y the Y coordinate of the new location |
| 147 | * @see java.awt.Component#setLocation(int, int) |
| 148 | * @see java.awt.Point#getLocation |
| 149 | * @see java.awt.Point#move(int, int) |
| 150 | * @since 1.1 |
| 151 | */ |
| 152 | public void setLocation(int x, int y) { |
| 153 | move(x, y); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Sets the location of this point to the specified double coordinates. |
| 158 | * The double values will be rounded to integer values. |
| 159 | * Any number smaller than <code>Integer.MIN_VALUE</code> |
| 160 | * will be reset to <code>MIN_VALUE</code>, and any number |
| 161 | * larger than <code>Integer.MAX_VALUE</code> will be |
| 162 | * reset to <code>MAX_VALUE</code>. |
| 163 | * |
| 164 | * @param x the X coordinate of the new location |
| 165 | * @param y the Y coordinate of the new location |
| 166 | * @see #getLocation |
| 167 | */ |
| 168 | public void setLocation(double x, double y) { |
| 169 | this.x = (int) Math.floor(x+0.5); |
| 170 | this.y = (int) Math.floor(y+0.5); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Moves this point to the specified location in the |
| 175 | * {@code (x,y)} coordinate plane. This method |
| 176 | * is identical with <code>setLocation(int, int)</code>. |
| 177 | * @param x the X coordinate of the new location |
| 178 | * @param y the Y coordinate of the new location |
| 179 | * @see java.awt.Component#setLocation(int, int) |
| 180 | */ |
| 181 | public void move(int x, int y) { |
| 182 | this.x = x; |
| 183 | this.y = y; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Translates this point, at location {@code (x,y)}, |
| 188 | * by {@code dx} along the {@code x} axis and {@code dy} |
| 189 | * along the {@code y} axis so that it now represents the point |
| 190 | * {@code (x+dx,y+dy)}. |
| 191 | * |
| 192 | * @param dx the distance to move this point |
| 193 | * along the X axis |
| 194 | * @param dy the distance to move this point |
| 195 | * along the Y axis |
| 196 | */ |
| 197 | public void translate(int dx, int dy) { |
| 198 | this.x += dx; |
| 199 | this.y += dy; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Determines whether or not two points are equal. Two instances of |
| 204 | * <code>Point2D</code> are equal if the values of their |
| 205 | * <code>x</code> and <code>y</code> member fields, representing |
| 206 | * their position in the coordinate space, are the same. |
| 207 | * @param obj an object to be compared with this <code>Point2D</code> |
| 208 | * @return <code>true</code> if the object to be compared is |
| 209 | * an instance of <code>Point2D</code> and has |
| 210 | * the same values; <code>false</code> otherwise. |
| 211 | */ |
| 212 | public boolean equals(Object obj) { |
| 213 | if (obj instanceof Point) { |
| 214 | Point pt = (Point)obj; |
| 215 | return (x == pt.x) && (y == pt.y); |
| 216 | } |
| 217 | return super.equals(obj); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Returns a string representation of this point and its location |
| 222 | * in the {@code (x,y)} coordinate space. This method is |
| 223 | * intended to be used only for debugging purposes, and the content |
| 224 | * and format of the returned string may vary between implementations. |
| 225 | * The returned string may be empty but may not be <code>null</code>. |
| 226 | * |
| 227 | * @return a string representation of this point |
| 228 | */ |
| 229 | public String toString() { |
| 230 | return getClass().getName() + "[x=" + x + ",y=" + y + "]"; |
| 231 | } |
| 232 | } |