View Javadoc
1   /*
2    * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package java.awt;
27  
28  import java.awt.geom.Point2D;
29  import java.beans.Transient;
30  
31  /**
32   * A point representing a location in {@code (x,y)} coordinate space,
33   * specified in integer precision.
34   *
35   * @author      Sami Shaio
36   * @since       1.0
37   */
38  public class Point extends Point2D implements java.io.Serializable {
39      /**
40       * The X coordinate of this <code>Point</code>.
41       * If no X coordinate is set it will default to 0.
42       *
43       * @serial
44       * @see #getLocation()
45       * @see #move(int, int)
46       * @since 1.0
47       */
48      public int x;
49  
50      /**
51       * The Y coordinate of this <code>Point</code>.
52       * If no Y coordinate is set it will default to 0.
53       *
54       * @serial
55       * @see #getLocation()
56       * @see #move(int, int)
57       * @since 1.0
58       */
59      public int y;
60  
61      /*
62       * JDK 1.1 serialVersionUID
63       */
64      private static final long serialVersionUID = -5276940640259749850L;
65  
66      /**
67       * Constructs and initializes a point at the origin
68       * (0,&nbsp;0) of the coordinate space.
69       * @since       1.1
70       */
71      public Point() {
72          this(0, 0);
73      }
74  
75      /**
76       * Constructs and initializes a point with the same location as
77       * the specified <code>Point</code> object.
78       * @param       p a point
79       * @since       1.1
80       */
81      public Point(Point p) {
82          this(p.x, p.y);
83      }
84  
85      /**
86       * Constructs and initializes a point at the specified
87       * {@code (x,y)} location in the coordinate space.
88       * @param x the X coordinate of the newly constructed <code>Point</code>
89       * @param y the Y coordinate of the newly constructed <code>Point</code>
90       * @since 1.0
91       */
92      public Point(int x, int y) {
93          this.x = x;
94          this.y = y;
95      }
96  
97      /**
98       * {@inheritDoc}
99       * @since 1.2
100      */
101     public double getX() {
102         return x;
103     }
104 
105     /**
106      * {@inheritDoc}
107      * @since 1.2
108      */
109     public double getY() {
110         return y;
111     }
112 
113     /**
114      * Returns the location of this point.
115      * This method is included for completeness, to parallel the
116      * <code>getLocation</code> method of <code>Component</code>.
117      * @return      a copy of this point, at the same location
118      * @see         java.awt.Component#getLocation
119      * @see         java.awt.Point#setLocation(java.awt.Point)
120      * @see         java.awt.Point#setLocation(int, int)
121      * @since       1.1
122      */
123     @Transient
124     public Point getLocation() {
125         return new Point(x, y);
126     }
127 
128     /**
129      * Sets the location of the point to the specified location.
130      * This method is included for completeness, to parallel the
131      * <code>setLocation</code> method of <code>Component</code>.
132      * @param       p  a point, the new location for this point
133      * @see         java.awt.Component#setLocation(java.awt.Point)
134      * @see         java.awt.Point#getLocation
135      * @since       1.1
136      */
137     public void setLocation(Point p) {
138         setLocation(p.x, p.y);
139     }
140 
141     /**
142      * Changes the point to have the specified location.
143      * <p>
144      * This method is included for completeness, to parallel the
145      * <code>setLocation</code> method of <code>Component</code>.
146      * Its behavior is identical with <code>move(int,&nbsp;int)</code>.
147      * @param       x the X coordinate of the new location
148      * @param       y the Y coordinate of the new location
149      * @see         java.awt.Component#setLocation(int, int)
150      * @see         java.awt.Point#getLocation
151      * @see         java.awt.Point#move(int, int)
152      * @since       1.1
153      */
154     public void setLocation(int x, int y) {
155         move(x, y);
156     }
157 
158     /**
159      * Sets the location of this point to the specified double coordinates.
160      * The double values will be rounded to integer values.
161      * Any number smaller than <code>Integer.MIN_VALUE</code>
162      * will be reset to <code>MIN_VALUE</code>, and any number
163      * larger than <code>Integer.MAX_VALUE</code> will be
164      * reset to <code>MAX_VALUE</code>.
165      *
166      * @param x the X coordinate of the new location
167      * @param y the Y coordinate of the new location
168      * @see #getLocation
169      */
170     public void setLocation(double x, double y) {
171         this.x = (int) Math.floor(x+0.5);
172         this.y = (int) Math.floor(y+0.5);
173     }
174 
175     /**
176      * Moves this point to the specified location in the
177      * {@code (x,y)} coordinate plane. This method
178      * is identical with <code>setLocation(int,&nbsp;int)</code>.
179      * @param       x the X coordinate of the new location
180      * @param       y the Y coordinate of the new location
181      * @see         java.awt.Component#setLocation(int, int)
182      */
183     public void move(int x, int y) {
184         this.x = x;
185         this.y = y;
186     }
187 
188     /**
189      * Translates this point, at location {@code (x,y)},
190      * by {@code dx} along the {@code x} axis and {@code dy}
191      * along the {@code y} axis so that it now represents the point
192      * {@code (x+dx,y+dy)}.
193      *
194      * @param       dx   the distance to move this point
195      *                            along the X axis
196      * @param       dy    the distance to move this point
197      *                            along the Y axis
198      */
199     public void translate(int dx, int dy) {
200         this.x += dx;
201         this.y += dy;
202     }
203 
204     /**
205      * Determines whether or not two points are equal. Two instances of
206      * <code>Point2D</code> are equal if the values of their
207      * <code>x</code> and <code>y</code> member fields, representing
208      * their position in the coordinate space, are the same.
209      * @param obj an object to be compared with this <code>Point2D</code>
210      * @return <code>true</code> if the object to be compared is
211      *         an instance of <code>Point2D</code> and has
212      *         the same values; <code>false</code> otherwise.
213      */
214     public boolean equals(Object obj) {
215         if (obj instanceof Point) {
216             Point pt = (Point)obj;
217             return (x == pt.x) && (y == pt.y);
218         }
219         return super.equals(obj);
220     }
221 
222     /**
223      * Returns a string representation of this point and its location
224      * in the {@code (x,y)} coordinate space. This method is
225      * intended to be used only for debugging purposes, and the content
226      * and format of the returned string may vary between implementations.
227      * The returned string may be empty but may not be <code>null</code>.
228      *
229      * @return  a string representation of this point
230      */
231     public String toString() {
232         return getClass().getName() + "[x=" + x + ",y=" + y + "]";
233     }
234 }