| 1 | /* Copyright (C) 1999, 2002 Free Software Foundation
|
|---|
| 2 |
|
|---|
| 3 | This file is part of GNU Classpath.
|
|---|
| 4 |
|
|---|
| 5 | GNU Classpath is free software; you can redistribute it and/or modify
|
|---|
| 6 | it under the terms of the GNU General Public License as published by
|
|---|
| 7 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 8 | any later version.
|
|---|
| 9 |
|
|---|
| 10 | GNU Classpath is distributed in the hope that it will be useful, but
|
|---|
| 11 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 13 | General Public License for more details.
|
|---|
| 14 |
|
|---|
| 15 | You should have received a copy of the GNU General Public License
|
|---|
| 16 | along with GNU Classpath; see the file COPYING. If not, write to the
|
|---|
| 17 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 18 | 02111-1307 USA.
|
|---|
| 19 |
|
|---|
| 20 | Linking this library statically or dynamically with other modules is
|
|---|
| 21 | making a combined work based on this library. Thus, the terms and
|
|---|
| 22 | conditions of the GNU General Public License cover the whole
|
|---|
| 23 | combination.
|
|---|
| 24 |
|
|---|
| 25 | As a special exception, the copyright holders of this library give you
|
|---|
| 26 | permission to link this library with independent modules to produce an
|
|---|
| 27 | executable, regardless of the license terms of these independent
|
|---|
| 28 | modules, and to copy and distribute the resulting executable under
|
|---|
| 29 | terms of your choice, provided that you also meet, for each linked
|
|---|
| 30 | independent module, the terms and conditions of the license of that
|
|---|
| 31 | module. An independent module is a module which is not derived from
|
|---|
| 32 | or based on this library. If you modify this library, you may extend
|
|---|
| 33 | this exception to your version of the library, but you are not
|
|---|
| 34 | obligated to do so. If you do not wish to do so, delete this
|
|---|
| 35 | exception statement from your version. */
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 | package java.awt;
|
|---|
| 39 | import java.awt.geom.Point2D;
|
|---|
| 40 |
|
|---|
| 41 | /* Written using "Java Class Libraries", 2nd edition, plus online
|
|---|
| 42 | * API docs for JDK 1.2 beta from http://www.javasoft.com.
|
|---|
| 43 | * Status: Believed complete and correct, except that neither toString
|
|---|
| 44 | * nor hashCode have been compared with JDK output.
|
|---|
| 45 | */
|
|---|
| 46 |
|
|---|
| 47 | /**
|
|---|
| 48 | * This class represents a point on the screen using cartesian coordinates.
|
|---|
| 49 | *
|
|---|
| 50 | * @author Per Bothner <[email protected]>
|
|---|
| 51 | * @author Aaron M. Renn ([email protected])
|
|---|
| 52 | * @date February 8, 1999.
|
|---|
| 53 | */
|
|---|
| 54 | public class Point extends Point2D implements java.io.Serializable
|
|---|
| 55 | {
|
|---|
| 56 | /**
|
|---|
| 57 | * @serial The X coordinate of the point.
|
|---|
| 58 | */
|
|---|
| 59 | public int x;
|
|---|
| 60 |
|
|---|
| 61 | /**
|
|---|
| 62 | * @serial The Y coordinate of the point.
|
|---|
| 63 | */
|
|---|
| 64 | public int y;
|
|---|
| 65 |
|
|---|
| 66 | /**
|
|---|
| 67 | * Initializes a new instance of <code>Point</code> representing the
|
|---|
| 68 | * coordiates (0,0).
|
|---|
| 69 | */
|
|---|
| 70 | public Point () { }
|
|---|
| 71 |
|
|---|
| 72 | /**
|
|---|
| 73 | * Initializes a new instance of <code>Point</code> with coordinates
|
|---|
| 74 | * identical to the coordinates of the specified points.
|
|---|
| 75 | *
|
|---|
| 76 | * @param point The point to copy the coordinates from.
|
|---|
| 77 | */
|
|---|
| 78 | public Point (Point p) { this.x = p.x; this.y = p.y; }
|
|---|
| 79 |
|
|---|
| 80 | /**
|
|---|
| 81 | * Initializes a new instance of <code>Point</code> with the specified
|
|---|
| 82 | * coordinates.
|
|---|
| 83 | *
|
|---|
| 84 | * @param x The X coordinate of this point.
|
|---|
| 85 | * @param y The Y coordinate of this point.
|
|---|
| 86 | */
|
|---|
| 87 | public Point (int x, int y) { this.x = x; this.y = y; }
|
|---|
| 88 |
|
|---|
| 89 | /**
|
|---|
| 90 | * Tests whether or not this object is equal to the specified object.
|
|---|
| 91 | * This will be true if and only if the specified objectj:
|
|---|
| 92 | * <p>
|
|---|
| 93 | * <ul>
|
|---|
| 94 | * <li>Is not <code>null</code>.
|
|---|
| 95 | * <li>Is an instance of <code>Point</code>.
|
|---|
| 96 | * <li>Has X and Y coordinates equal to this object's.
|
|---|
| 97 | * </ul>
|
|---|
| 98 | *
|
|---|
| 99 | * @param obj The object to test against for equality.
|
|---|
| 100 | *
|
|---|
| 101 | * @return <code>true</code> if the specified object is equal to this
|
|---|
| 102 | * object, <code>false</code> otherwise.
|
|---|
| 103 | */
|
|---|
| 104 | public boolean equals (Object obj)
|
|---|
| 105 | {
|
|---|
| 106 | if (! (obj instanceof Point))
|
|---|
| 107 | return false;
|
|---|
| 108 | Point p = (Point) obj;
|
|---|
| 109 | return this.x == p.x && this.y == p.y;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | /**
|
|---|
| 113 | * Returns a hash value for this point.
|
|---|
| 114 | *
|
|---|
| 115 | * @param A hash value for this point.
|
|---|
| 116 | */
|
|---|
| 117 | public int hashCode () { return x ^ y; }
|
|---|
| 118 |
|
|---|
| 119 | /**
|
|---|
| 120 | * Returns the location of this object as a point. A pretty useless
|
|---|
| 121 | * method. It is included to mimic the <code>getLocation</code> method
|
|---|
| 122 | * in component.
|
|---|
| 123 | *
|
|---|
| 124 | * @return This point.
|
|---|
| 125 | */
|
|---|
| 126 | public Point getLocation () { return new Point(this); }
|
|---|
| 127 |
|
|---|
| 128 | /**
|
|---|
| 129 | * Sets this object's coordinates to the specified values. This method
|
|---|
| 130 | * is identical to the <code>setLocation(int, int)</code> method.
|
|---|
| 131 | *
|
|---|
| 132 | * @param x The new X coordinate.
|
|---|
| 133 | * @param y The new Y coordinate.
|
|---|
| 134 | */
|
|---|
| 135 | public void move (int x, int y) { this.x = x; this.y = y; }
|
|---|
| 136 |
|
|---|
| 137 | /**
|
|---|
| 138 | * Sets this object's coordinates to the specified values. This method
|
|---|
| 139 | * is identical to the <code>move()</code> method.
|
|---|
| 140 | *
|
|---|
| 141 | * @param x The new X coordinate.
|
|---|
| 142 | * @param y The new Y coordinate.
|
|---|
| 143 | */
|
|---|
| 144 | public void setLocation (int x, int y) { this.x = x; this.y = y; }
|
|---|
| 145 |
|
|---|
| 146 | /**
|
|---|
| 147 | * Sets this object's coordinates to match those of the specified point.
|
|---|
| 148 | *
|
|---|
| 149 | * @param point The point to copy the coordinates from.
|
|---|
| 150 | */
|
|---|
| 151 | public void setLocation (Point pt) { this.x = pt.x; this.y = pt.y; }
|
|---|
| 152 |
|
|---|
| 153 | /**
|
|---|
| 154 | * Changes the coordinates of this point such that the specified
|
|---|
| 155 | * <code>dx</code> parameter is added to the existing X coordinate and
|
|---|
| 156 | * <code>dy</code> is added to the existing Y coordinate.
|
|---|
| 157 | *
|
|---|
| 158 | * @param dx The amount to add to the X coordinate.
|
|---|
| 159 | * @param dy The amount to add to the Y coordinate.
|
|---|
| 160 | */
|
|---|
| 161 | public void translate (int x, int y) { this.x += x; this.y += y; }
|
|---|
| 162 |
|
|---|
| 163 | /**
|
|---|
| 164 | * Returns a string representation of this object.
|
|---|
| 165 | *
|
|---|
| 166 | * @return A string representation of this object.
|
|---|
| 167 | */
|
|---|
| 168 | public String toString ()
|
|---|
| 169 | {
|
|---|
| 170 | return getClass().getName() + "[x:"+x+",y:"+y+']';
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | public double getX() { return x; }
|
|---|
| 174 | public double getY() { return y; }
|
|---|
| 175 |
|
|---|
| 176 | public void setLocation (double x, double y)
|
|---|
| 177 | { this.x = (int) x; this.y = (int) y; }
|
|---|
| 178 |
|
|---|
| 179 | }
|
|---|