2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / awt / Point.java
blob9d5126434aec0d8c7e85449baf92014001cad1ec
1 /* Point.java -- represents a point in 2-D space
2 Copyright (C) 1999, 2002 Free Software Foundation
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.awt;
41 import java.awt.geom.Point2D;
42 import java.io.Serializable;
44 /**
45 * This class represents a point on the screen using cartesian coordinates.
46 * Remember that in screen coordinates, increasing x values go from left to
47 * right, and increasing y values go from top to bottom.
49 * <p>There are some public fields; if you mess with them in an inconsistent
50 * manner, it is your own fault when you get invalid results. Also, this
51 * class is not threadsafe.
53 * @author Per Bothner <bothner@cygnus.com>
54 * @author Aaron M. Renn <arenn@urbanophile.com>
55 * @author Eric Blake <ebb9@email.byu.edu>
56 * @since 1.0
57 * @status updated to 1.4
59 public class Point extends Point2D implements Serializable
61 /**
62 * Compatible with JDK 1.0+.
64 private static final long serialVersionUID = -5276940640259749850L;
66 /**
67 * The x coordinate.
69 * @see #getLocation()
70 * @see #move(int, int)
71 * @serial the X coordinate of the point
73 public int x;
75 /**
76 * The y coordinate.
78 * @see #getLocation()
79 * @see #move(int, int)
80 * @serial The Y coordinate of the point
82 public int y;
84 /**
85 * Initializes a new instance of <code>Point</code> representing the
86 * coordiates (0,0).
88 * @since 1.1
90 public Point()
94 /**
95 * Initializes a new instance of <code>Point</code> with coordinates
96 * identical to the coordinates of the specified points.
98 * @param point the point to copy the coordinates from
99 * @throws NullPointerException if p is null
101 public Point(Point p)
103 x = p.x;
104 y = p.y;
108 * Initializes a new instance of <code>Point</code> with the specified
109 * coordinates.
111 * @param x the X coordinate
112 * @param y the Y coordinate
114 public Point(int x, int y)
116 this.x = x;
117 this.y = y;
121 * Get the x coordinate.
123 * @return the value of x, as a double
125 public double getX()
127 return x;
131 * Get the y coordinate.
133 * @return the value of y, as a double
135 public double getY()
137 return y;
141 * Returns the location of this point. A pretty useless method, as this
142 * is already a point.
144 * @return a copy of this point
145 * @see #setLocation(Point)
146 * @since 1.1
148 public Point getLocation()
150 return new Point(x, y);
154 * Sets this object's coordinates to match those of the specified point.
156 * @param p the point to copy the coordinates from
157 * @throws NullPointerException if p is null
158 * @since 1.1
160 public void setLocation(Point p)
162 x = p.x;
163 y = p.y;
167 * Sets this object's coordinates to the specified values. This method
168 * is identical to the <code>move()</code> method.
170 * @param x the new X coordinate
171 * @param y the new Y coordinate
173 public void setLocation(int x, int y)
175 this.x = x;
176 this.y = y;
180 * Sets this object's coordinates to the specified values. This method
181 * performs normal casting from double to int, so you may lose precision.
183 * @param x the new X coordinate
184 * @param y the new Y coordinate
186 public void setLocation(double x, double y)
188 this.x = (int) x;
189 this.y = (int) y;
193 * Sets this object's coordinates to the specified values. This method
194 * is identical to the <code>setLocation(int, int)</code> method.
196 * @param x the new X coordinate
197 * @param y the new Y coordinate
199 public void move(int x, int y)
201 this.x = x;
202 this.y = y;
206 * Changes the coordinates of this point such that the specified
207 * <code>dx</code> parameter is added to the existing X coordinate and
208 * <code>dy</code> is added to the existing Y coordinate.
210 * @param dx the amount to add to the X coordinate
211 * @param dy the amount to add to the Y coordinate
213 public void translate(int dx, int dy)
215 x += dx;
216 y += dy;
220 * Tests whether or not this object is equal to the specified object.
221 * This will be true if and only if the specified object is an instance
222 * of Point2D and has the same X and Y coordinates.
224 * @param obj the object to test against for equality
225 * @return true if the specified object is equal
227 public boolean equals(Object obj)
229 if (! (obj instanceof Point2D))
230 return false;
231 Point2D p = (Point2D) obj;
232 return x == p.getX() && y == p.getY();
236 * Returns a string representation of this object. The format is:
237 * <code>getClass().getName() + "[x=" + x + ",y=" + y + ']'</code>.
239 * @return a string representation of this object
241 public String toString()
243 return getClass().getName() + "[x=" + x + ",y=" + y + ']';
245 } // class Point