OSDN Git Service

e03b8a8b8a30fee970fd1bd76e543c16703ca1ac
[pf3gnuchains/gcc-fork.git] / libjava / java / awt / Point.java
1 /* Copyright (C) 1999, 2002  Free Software Foundation
2
3    This file is part of libjava.
4
5 This software is copyrighted work licensed under the terms of the
6 Libjava License.  Please consult the file "LIBJAVA_LICENSE" for
7 details.  */
8
9 package java.awt;
10 import java.awt.geom.Point2D;
11
12 /* Written using "Java Class Libraries", 2nd edition, plus online
13  * API docs for JDK 1.2 beta from http://www.javasoft.com.
14  * Status:  Believed complete and correct, except that neither toString
15  * nor hashCode have been compared with JDK output.
16  */
17
18 /**
19  * This class represents a point on the screen using cartesian coordinates.
20  *
21  * @author Per Bothner <bothner@cygnus.com>
22  * @author Aaron M. Renn (arenn@urbanophile.com)
23  * @date February 8, 1999.
24  */
25 public class Point extends Point2D implements java.io.Serializable
26 {
27   /**
28    * @serial The X coordinate of the point.
29    */
30   public int x;
31
32   /**
33    * @serial The Y coordinate of the point.
34    */
35   public int y;
36
37   /**
38    * Initializes a new instance of <code>Point</code> representing the
39    * coordiates (0,0).
40    */
41   public Point () { }
42
43   /**
44    * Initializes a new instance of <code>Point</code> with coordinates
45    * identical to the coordinates of the specified points.
46    *
47    * @param point The point to copy the coordinates from.
48    */
49   public Point (Point p) { this.x = p.x;  this.y = p.y; }
50
51   /**
52    * Initializes a new instance of <code>Point</code> with the specified
53    * coordinates.
54    *
55    * @param x The X coordinate of this point.
56    * @param y The Y coordinate of this point.
57    */
58   public Point (int x, int y) { this.x = x;  this.y = y; }
59
60   /**
61    * Tests whether or not this object is equal to the specified object.
62    * This will be true if and only if the specified objectj:
63    * <p>
64    * <ul>
65    * <li>Is not <code>null</code>.
66    * <li>Is an instance of <code>Point</code>.
67    * <li>Has X and Y coordinates equal to this object's.
68    * </ul>
69    *
70    * @param obj The object to test against for equality.
71    *
72    * @return <code>true</code> if the specified object is equal to this
73    * object, <code>false</code> otherwise.
74   */
75   public boolean equals (Object obj)
76   {
77     if (! (obj instanceof Point))
78       return false;
79     Point p = (Point) obj;
80     return this.x == p.x && this.y == p.y;
81   }
82
83   /**
84    * Returns a hash value for this point.
85    *
86    * @param A hash value for this point.
87    */
88   public int hashCode () { return x ^ y; }
89
90   /**
91    * Returns the location of this object as a point.  A pretty useless
92    * method.  It is included to mimic the <code>getLocation</code> method
93    * in component.
94    *
95    * @return This point.
96    */
97   public Point getLocation () { return new Point(this); }
98
99   /**
100    * Sets this object's coordinates to the specified values.  This method
101    * is identical to the <code>setLocation(int, int)</code> method.
102    *
103    * @param x The new X coordinate.
104    * @param y The new Y coordinate.
105    */
106   public void move (int x, int y) { this.x = x;  this.y = y; }
107
108   /**
109    * Sets this object's coordinates to the specified values.  This method
110    * is identical to the <code>move()</code> method.
111    *
112    * @param x The new X coordinate.
113    * @param y The new Y coordinate.
114    */
115   public void setLocation (int x, int y) { this.x = x;  this.y = y; }
116
117   /**
118    * Sets this object's coordinates to match those of the specified point.
119    *
120    * @param point The point to copy the coordinates from.
121    */
122   public void setLocation (Point pt) { this.x = pt.x;  this.y = pt.y; }
123
124   /**
125    * Changes the coordinates of this point such that the specified 
126    * <code>dx</code> parameter is added to the existing X coordinate and
127    * <code>dy</code> is added to the existing Y coordinate.
128    *
129    * @param dx The amount to add to the X coordinate.
130    * @param dy The amount to add to the Y coordinate.
131    */
132   public void translate (int x, int y) { this.x += x;  this.y += y; }
133
134   /**
135    * Returns a string representation of this object.
136    *
137    * @return A string representation of this object.
138    */
139   public String toString ()
140   {
141     return getClass().getName() + "[x:"+x+",y:"+y+']';
142   }
143
144   public double getX() { return x; }
145   public double getY() { return y; }
146
147   public void setLocation (double x, double y)
148   { this.x = (int) x;  this.y = (int) y; }
149
150 }