1 |
/* Copyright (C) 1999, 2002 Free Software Foundation |
/* Point.java -- represents a point in 2-D space |
2 |
|
Copyright (C) 1999, 2002 Free Software Foundation |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
37 |
|
|
38 |
|
|
39 |
package java.awt; |
package java.awt; |
|
import java.awt.geom.Point2D; |
|
40 |
|
|
41 |
/* Written using "Java Class Libraries", 2nd edition, plus online |
import java.awt.geom.Point2D; |
42 |
* API docs for JDK 1.2 beta from http://www.javasoft.com. |
import java.io.Serializable; |
|
* Status: Believed complete and correct, except that neither toString |
|
|
* nor hashCode have been compared with JDK output. |
|
|
*/ |
|
43 |
|
|
44 |
/** |
/** |
45 |
* This class represents a point on the screen using cartesian coordinates. |
* 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. |
48 |
|
* |
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. |
52 |
* |
* |
53 |
* @author Per Bothner <bothner@cygnus.com> |
* @author Per Bothner <bothner@cygnus.com> |
54 |
* @author Aaron M. Renn (arenn@urbanophile.com) |
* @author Aaron M. Renn <arenn@urbanophile.com> |
55 |
* @date February 8, 1999. |
* @author Eric Blake <ebb9@email.byu.edu> |
56 |
|
* @since 1.0 |
57 |
|
* @status updated to 1.4 |
58 |
*/ |
*/ |
59 |
public class Point extends Point2D implements java.io.Serializable |
public class Point extends Point2D implements Serializable |
60 |
{ |
{ |
61 |
/** |
/** |
62 |
* @serial The X coordinate of the point. |
* Compatible with JDK 1.0+. |
63 |
|
*/ |
64 |
|
private static final long serialVersionUID = -5276940640259749850L; |
65 |
|
|
66 |
|
/** |
67 |
|
* The x coordinate. |
68 |
|
* |
69 |
|
* @see #getLocation() |
70 |
|
* @see #move(int, int) |
71 |
|
* @serial the X coordinate of the point |
72 |
*/ |
*/ |
73 |
public int x; |
public int x; |
74 |
|
|
75 |
/** |
/** |
76 |
* @serial The Y coordinate of the point. |
* The y coordinate. |
77 |
|
* |
78 |
|
* @see #getLocation() |
79 |
|
* @see #move(int, int) |
80 |
|
* @serial The Y coordinate of the point |
81 |
*/ |
*/ |
82 |
public int y; |
public int y; |
83 |
|
|
84 |
/** |
/** |
85 |
* Initializes a new instance of <code>Point</code> representing the |
* Initializes a new instance of <code>Point</code> representing the |
86 |
* coordiates (0,0). |
* coordiates (0,0). |
87 |
|
* |
88 |
|
* @since 1.1 |
89 |
*/ |
*/ |
90 |
public Point () { } |
public Point() |
91 |
|
{ |
92 |
|
} |
93 |
|
|
94 |
/** |
/** |
95 |
* Initializes a new instance of <code>Point</code> with coordinates |
* Initializes a new instance of <code>Point</code> with coordinates |
96 |
* identical to the coordinates of the specified points. |
* identical to the coordinates of the specified points. |
97 |
* |
* |
98 |
* @param point The point to copy the coordinates from. |
* @param point the point to copy the coordinates from |
99 |
|
* @throws NullPointerException if p is null |
100 |
*/ |
*/ |
101 |
public Point (Point p) { this.x = p.x; this.y = p.y; } |
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 |
* Initializes a new instance of <code>Point</code> with the specified |
109 |
* coordinates. |
* coordinates. |
110 |
* |
* |
111 |
* @param x The X coordinate of this point. |
* @param x the X coordinate |
112 |
* @param y The Y coordinate of this point. |
* @param y the Y coordinate |
113 |
*/ |
*/ |
114 |
public Point (int x, int y) { this.x = x; this.y = y; } |
public Point(int x, int y) |
115 |
|
{ |
116 |
|
this.x = x; |
117 |
|
this.y = y; |
118 |
|
} |
119 |
|
|
120 |
/** |
/** |
121 |
* Tests whether or not this object is equal to the specified object. |
* Get the x coordinate. |
|
* This will be true if and only if the specified objectj: |
|
|
* <p> |
|
|
* <ul> |
|
|
* <li>Is not <code>null</code>. |
|
|
* <li>Is an instance of <code>Point</code>. |
|
|
* <li>Has X and Y coordinates equal to this object's. |
|
|
* </ul> |
|
122 |
* |
* |
123 |
* @param obj The object to test against for equality. |
* @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 <code>true</code> if the specified object is equal to this |
* @return the value of y, as a double |
134 |
* object, <code>false</code> otherwise. |
*/ |
135 |
*/ |
public double getY() |
|
public boolean equals (Object obj) |
|
136 |
{ |
{ |
137 |
if (! (obj instanceof Point)) |
return y; |
|
return false; |
|
|
Point p = (Point) obj; |
|
|
return this.x == p.x && this.y == p.y; |
|
138 |
} |
} |
139 |
|
|
140 |
/** |
/** |
141 |
* Returns a hash value for this point. |
* Returns the location of this point. A pretty useless method, as this |
142 |
|
* is already a point. |
143 |
* |
* |
144 |
* @param A hash value for this point. |
* @return a copy of this point |
145 |
|
* @see #setLocation(Point) |
146 |
|
* @since 1.1 |
147 |
*/ |
*/ |
148 |
public int hashCode () { return x ^ y; } |
public Point getLocation() |
149 |
|
{ |
150 |
|
return new Point(x, y); |
151 |
|
} |
152 |
|
|
153 |
/** |
/** |
154 |
* Returns the location of this object as a point. A pretty useless |
* Sets this object's coordinates to match those of the specified point. |
|
* method. It is included to mimic the <code>getLocation</code> method |
|
|
* in component. |
|
155 |
* |
* |
156 |
* @return This point. |
* @param p the point to copy the coordinates from |
157 |
|
* @throws NullPointerException if p is null |
158 |
|
* @since 1.1 |
159 |
*/ |
*/ |
160 |
public Point getLocation () { return new Point(this); } |
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 |
* Sets this object's coordinates to the specified values. This method |
168 |
* is identical to the <code>setLocation(int, int)</code> method. |
* is identical to the <code>move()</code> method. |
169 |
* |
* |
170 |
* @param x The new X coordinate. |
* @param x the new X coordinate |
171 |
* @param y The new Y coordinate. |
* @param y the new Y coordinate |
172 |
*/ |
*/ |
173 |
public void move (int x, int y) { this.x = x; this.y = y; } |
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 |
* Sets this object's coordinates to the specified values. This method |
181 |
* is identical to the <code>move()</code> method. |
* performs normal casting from double to int, so you may lose precision. |
182 |
* |
* |
183 |
* @param x The new X coordinate. |
* @param x the new X coordinate |
184 |
* @param y The new Y coordinate. |
* @param y the new Y coordinate |
185 |
*/ |
*/ |
186 |
public void setLocation (int x, int y) { this.x = x; this.y = y; } |
public void setLocation(double x, double y) |
187 |
|
{ |
188 |
|
this.x = (int) x; |
189 |
|
this.y = (int) y; |
190 |
|
} |
191 |
|
|
192 |
/** |
/** |
193 |
* Sets this object's coordinates to match those of the specified point. |
* Sets this object's coordinates to the specified values. This method |
194 |
|
* is identical to the <code>setLocation(int, int)</code> method. |
195 |
* |
* |
196 |
* @param point The point to copy the coordinates from. |
* @param x the new X coordinate |
197 |
|
* @param y the new Y coordinate |
198 |
*/ |
*/ |
199 |
public void setLocation (Point pt) { this.x = pt.x; this.y = pt.y; } |
public void move(int x, int y) |
200 |
|
{ |
201 |
|
this.x = x; |
202 |
|
this.y = y; |
203 |
|
} |
204 |
|
|
205 |
/** |
/** |
206 |
* Changes the coordinates of this point such that the specified |
* Changes the coordinates of this point such that the specified |
207 |
* <code>dx</code> parameter is added to the existing X coordinate and |
* <code>dx</code> parameter is added to the existing X coordinate and |
208 |
* <code>dy</code> is added to the existing Y coordinate. |
* <code>dy</code> is added to the existing Y coordinate. |
209 |
* |
* |
210 |
* @param dx The amount to add to the X coordinate. |
* @param dx the amount to add to the X coordinate |
211 |
* @param dy The amount to add to the Y coordinate. |
* @param dy the amount to add to the Y coordinate |
212 |
*/ |
*/ |
213 |
public void translate (int x, int y) { this.x += x; this.y += y; } |
public void translate(int dx, int dy) |
214 |
|
{ |
215 |
|
x += dx; |
216 |
|
y += dy; |
217 |
|
} |
218 |
|
|
219 |
/** |
/** |
220 |
* Returns a string representation of this object. |
* 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. |
223 |
* |
* |
224 |
* @return A string representation of this object. |
* @param obj the object to test against for equality |
225 |
*/ |
* @return true if the specified object is equal |
226 |
public String toString () |
*/ |
227 |
|
public boolean equals(Object obj) |
228 |
{ |
{ |
229 |
return getClass().getName() + "[x:"+x+",y:"+y+']'; |
if (! (obj instanceof Point2D)) |
230 |
|
return false; |
231 |
|
Point2D p = (Point2D) obj; |
232 |
|
return x == p.getX() && y == p.getY(); |
233 |
} |
} |
234 |
|
|
235 |
public double getX() { return x; } |
/** |
236 |
public double getY() { return y; } |
* Returns a string representation of this object. The format is: |
237 |
|
* <code>getClass().getName() + "[x=" + x + ",y=" + y + ']'</code>. |
238 |
public void setLocation (double x, double y) |
* |
239 |
{ this.x = (int) x; this.y = (int) y; } |
* @return a string representation of this object |
240 |
|
*/ |
241 |
} |
public String toString() |
242 |
|
{ |
243 |
|
return getClass().getName() + "[x=" + x + ",y=" + y + ']'; |
244 |
|
} |
245 |
|
} // class Point |