001 /* Point.java -- represents a point in 2-D space 002 Copyright (C) 1999, 2002, 2006 Free Software Foundation 003 004 This file is part of GNU Classpath. 005 006 GNU Classpath is free software; you can redistribute it and/or modify 007 it under the terms of the GNU General Public License as published by 008 the Free Software Foundation; either version 2, or (at your option) 009 any later version. 010 011 GNU Classpath 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 for more details. 015 016 You should have received a copy of the GNU General Public License 017 along with GNU Classpath; see the file COPYING. If not, write to the 018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 019 02110-1301 USA. 020 021 Linking this library statically or dynamically with other modules is 022 making a combined work based on this library. Thus, the terms and 023 conditions of the GNU General Public License cover the whole 024 combination. 025 026 As a special exception, the copyright holders of this library give you 027 permission to link this library with independent modules to produce an 028 executable, regardless of the license terms of these independent 029 modules, and to copy and distribute the resulting executable under 030 terms of your choice, provided that you also meet, for each linked 031 independent module, the terms and conditions of the license of that 032 module. An independent module is a module which is not derived from 033 or based on this library. If you modify this library, you may extend 034 this exception to your version of the library, but you are not 035 obligated to do so. If you do not wish to do so, delete this 036 exception statement from your version. */ 037 038 039 package java.awt; 040 041 import java.awt.geom.Point2D; 042 import java.io.Serializable; 043 044 /** 045 * This class represents a point on the screen using cartesian coordinates. 046 * Remember that in screen coordinates, increasing x values go from left to 047 * right, and increasing y values go from top to bottom. 048 * 049 * <p>There are some public fields; if you mess with them in an inconsistent 050 * manner, it is your own fault when you get invalid results. Also, this 051 * class is not threadsafe. 052 * 053 * @author Per Bothner (bothner@cygnus.com) 054 * @author Aaron M. Renn (arenn@urbanophile.com) 055 * @author Eric Blake (ebb9@email.byu.edu) 056 * @since 1.0 057 * @status updated to 1.4 058 */ 059 public class Point extends Point2D implements Serializable 060 { 061 /** 062 * Compatible with JDK 1.0+. 063 */ 064 private static final long serialVersionUID = -5276940640259749850L; 065 066 /** 067 * The x coordinate. 068 * 069 * @see #getLocation() 070 * @see #move(int, int) 071 * @serial the X coordinate of the point 072 */ 073 public int x; 074 075 /** 076 * The y coordinate. 077 * 078 * @see #getLocation() 079 * @see #move(int, int) 080 * @serial The Y coordinate of the point 081 */ 082 public int y; 083 084 /** 085 * Initializes a new instance of <code>Point</code> representing the 086 * coordinates (0, 0). 087 * 088 * @since 1.1 089 */ 090 public Point() 091 { 092 } 093 094 /** 095 * Initializes a new instance of <code>Point</code> with coordinates 096 * identical to the coordinates of the specified point. 097 * 098 * @param p the point to copy the coordinates from 099 * @throws NullPointerException if p is null 100 */ 101 public Point(Point p) 102 { 103 x = p.x; 104 y = p.y; 105 } 106 107 /** 108 * Initializes a new instance of <code>Point</code> with the specified 109 * coordinates. 110 * 111 * @param x the X coordinate 112 * @param y the Y coordinate 113 */ 114 public Point(int x, int y) 115 { 116 this.x = x; 117 this.y = y; 118 } 119 120 /** 121 * Get the x coordinate. 122 * 123 * @return the value of x, as a double 124 */ 125 public double getX() 126 { 127 return x; 128 } 129 130 /** 131 * Get the y coordinate. 132 * 133 * @return the value of y, as a double 134 */ 135 public double getY() 136 { 137 return y; 138 } 139 140 /** 141 * Returns the location of this point. A pretty useless method, as this 142 * is already a point. 143 * 144 * @return a copy of this point 145 * @see #setLocation(Point) 146 * @since 1.1 147 */ 148 public Point getLocation() 149 { 150 return new Point(x, y); 151 } 152 153 /** 154 * Sets this object's coordinates to match those of the specified point. 155 * 156 * @param p the point to copy the coordinates from 157 * @throws NullPointerException if p is null 158 * @since 1.1 159 */ 160 public void setLocation(Point p) 161 { 162 x = p.x; 163 y = p.y; 164 } 165 166 /** 167 * Sets this object's coordinates to the specified values. This method 168 * is identical to the <code>move()</code> method. 169 * 170 * @param x the new X coordinate 171 * @param y the new Y coordinate 172 */ 173 public void setLocation(int x, int y) 174 { 175 this.x = x; 176 this.y = y; 177 } 178 179 /** 180 * Sets this object's coordinates to the specified values. This method 181 * rounds to the nearest integer coordinates by adding 0.5 and calling 182 * {@link Math#floor(double)}. 183 * 184 * @param x the new X coordinate 185 * @param y the new Y coordinate 186 */ 187 public void setLocation(double x, double y) 188 { 189 this.x = (int) Math.floor(x + 0.5); 190 this.y = (int) Math.floor(y + 0.5); 191 } 192 193 /** 194 * Sets this object's coordinates to the specified values. This method 195 * is identical to the <code>setLocation(int, int)</code> method. 196 * 197 * @param x the new X coordinate 198 * @param y the new Y coordinate 199 */ 200 public void move(int x, int y) 201 { 202 this.x = x; 203 this.y = y; 204 } 205 206 /** 207 * Changes the coordinates of this point such that the specified 208 * <code>dx</code> parameter is added to the existing X coordinate and 209 * <code>dy</code> is added to the existing Y coordinate. 210 * 211 * @param dx the amount to add to the X coordinate 212 * @param dy the amount to add to the Y coordinate 213 */ 214 public void translate(int dx, int dy) 215 { 216 x += dx; 217 y += dy; 218 } 219 220 /** 221 * Tests whether or not this object is equal to the specified object. 222 * This will be true if and only if the specified object is an instance 223 * of Point2D and has the same X and Y coordinates. 224 * 225 * @param obj the object to test against for equality 226 * @return true if the specified object is equal 227 */ 228 public boolean equals(Object obj) 229 { 230 // NOTE: No special hashCode() method is required for this class, 231 // as this equals() implementation is functionally equivalent to 232 // super.equals(), which does define a proper hashCode(). 233 234 if (! (obj instanceof Point2D)) 235 return false; 236 Point2D p = (Point2D) obj; 237 return x == p.getX() && y == p.getY(); 238 } 239 240 /** 241 * Returns a string representation of this object. The format is: 242 * <code>getClass().getName() + "[x=" + x + ",y=" + y + ']'</code>. 243 * 244 * @return a string representation of this object 245 */ 246 public String toString() 247 { 248 return getClass().getName() + "[x=" + x + ",y=" + y + ']'; 249 } 250 } // class Point