KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > Point


1 /*
2  * @(#)Point.java 1.38 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.awt;
9
10 import java.awt.geom.Point2D JavaDoc;
11
12 /**
13  * A point representing a location in (x, y) coordinate space, specified
14  * in integer precision.
15  *
16  * @version 1.38, 12/19/03
17  * @author Sami Shaio
18  * @since JDK1.0
19  */

20 public class Point extends Point2D JavaDoc implements java.io.Serializable JavaDoc {
21     /**
22      * The <i>x</i> coordinate.
23      * If no <i>x</i> coordinate is set it will default to 0.
24      *
25      * @serial
26      * @see #getLocation()
27      * @see #move(int, int)
28      */

29     public int x;
30
31     /**
32      * The <i>y</i> coordinate.
33      * If no <i>y</i> coordinate is set it will default to 0.
34      *
35      * @serial
36      * @see #getLocation()
37      * @see #move(int, int)
38      */

39     public int y;
40
41     /*
42      * JDK 1.1 serialVersionUID
43      */

44     private static final long serialVersionUID = -5276940640259749850L;
45
46     /**
47      * Constructs and initializes a point at the origin
48      * (0,&nbsp;0) of the coordinate space.
49      * @since JDK1.1
50      */

51     public Point() {
52     this(0, 0);
53     }
54
55     /**
56      * Constructs and initializes a point with the same location as
57      * the specified <code>Point</code> object.
58      * @param p a point
59      * @since JDK1.1
60      */

61     public Point(Point JavaDoc p) {
62     this(p.x, p.y);
63     }
64
65     /**
66      * Constructs and initializes a point at the specified
67      * (<i>x</i>,&nbsp;<i>y</i>) location in the coordinate space.
68      * @param x the <i>x</i> coordinate
69      * @param y the <i>y</i> coordinate
70      */

71     public Point(int x, int y) {
72     this.x = x;
73     this.y = y;
74     }
75
76     /**
77      * Returns the X coordinate of the point in double precision.
78      * @return the X coordinate of the point in double precision
79      */

80     public double getX() {
81     return x;
82     }
83
84     /**
85      * Returns the Y coordinate of the point in double precision.
86      * @return the Y coordinate of the point in double precision
87      */

88     public double getY() {
89     return y;
90     }
91
92     /**
93      * Returns the location of this point.
94      * This method is included for completeness, to parallel the
95      * <code>getLocation</code> method of <code>Component</code>.
96      * @return a copy of this point, at the same location
97      * @see java.awt.Component#getLocation
98      * @see java.awt.Point#setLocation(java.awt.Point)
99      * @see java.awt.Point#setLocation(int, int)
100      * @since JDK1.1
101      */

102     public Point JavaDoc getLocation() {
103     return new Point JavaDoc(x, y);
104     }
105
106     /**
107      * Sets the location of the point to the specified location.
108      * This method is included for completeness, to parallel the
109      * <code>setLocation</code> method of <code>Component</code>.
110      * @param p a point, the new location for this point
111      * @see java.awt.Component#setLocation(java.awt.Point)
112      * @see java.awt.Point#getLocation
113      * @since JDK1.1
114      */

115     public void setLocation(Point JavaDoc p) {
116     setLocation(p.x, p.y);
117     }
118
119     /**
120      * Changes the point to have the specified location.
121      * <p>
122      * This method is included for completeness, to parallel the
123      * <code>setLocation</code> method of <code>Component</code>.
124      * Its behavior is identical with <code>move(int,&nbsp;int)</code>.
125      * @param x the <i>x</i> coordinate of the new location
126      * @param y the <i>y</i> coordinate of the new location
127      * @see java.awt.Component#setLocation(int, int)
128      * @see java.awt.Point#getLocation
129      * @see java.awt.Point#move(int, int)
130      * @since JDK1.1
131      */

132     public void setLocation(int x, int y) {
133     move(x, y);
134     }
135
136     /**
137      * Sets the location of this point to the specified double coordinates.
138      * The double values will be rounded to integer values.
139      * Any number smaller than <code>Integer.MIN_VALUE</code>
140      * will be reset to <code>MIN_VALUE</code>, and any number
141      * larger than <code>Integer.MAX_VALUE</code> will be
142      * reset to <code>MAX_VALUE</code>.
143      *
144      * @param x the <i>x</i> coordinate of the new location
145      * @param y the <i>y</i> coordinate of the new location
146      * @see #getLocation
147      */

148     public void setLocation(double x, double y) {
149     this.x = (int) Math.floor(x+0.5);
150     this.y = (int) Math.floor(y+0.5);
151     }
152
153     /**
154      * Moves this point to the specified location in the
155      * (<i>x</i>,&nbsp;<i>y</i>) coordinate plane. This method
156      * is identical with <code>setLocation(int,&nbsp;int)</code>.
157      * @param x the <i>x</i> coordinate of the new location
158      * @param y the <i>y</i> coordinate of the new location
159      * @see java.awt.Component#setLocation(int, int)
160      */

161     public void move(int x, int y) {
162     this.x = x;
163     this.y = y;
164     }
165
166     /**
167      * Translates this point, at location (<i>x</i>,&nbsp;<i>y</i>),
168      * by <code>dx</code> along the <i>x</i> axis and <code>dy</code>
169      * along the <i>y</i> axis so that it now represents the point
170      * (<code>x</code>&nbsp;<code>+</code>&nbsp;<code>dx</code>,
171      * <code>y</code>&nbsp;<code>+</code>&nbsp;<code>dy</code>).
172      * @param dx the distance to move this point
173      * along the <i>x</i> axis
174      * @param dy the distance to move this point
175      * along the <i>y</i> axis
176      */

177     public void translate(int dx, int dy) {
178     this.x += dx;
179     this.y += dy;
180     }
181
182     /**
183      * Determines whether or not two points are equal. Two instances of
184      * <code>Point2D</code> are equal if the values of their
185      * <code>x</code> and <code>y</code> member fields, representing
186      * their position in the coordinate space, are the same.
187      * @param obj an object to be compared with this <code>Point2D</code>
188      * @return <code>true</code> if the object to be compared is
189      * an instance of <code>Point2D</code> and has
190      * the same values; <code>false</code> otherwise.
191      */

192     public boolean equals(Object JavaDoc obj) {
193     if (obj instanceof Point JavaDoc) {
194         Point JavaDoc pt = (Point JavaDoc)obj;
195         return (x == pt.x) && (y == pt.y);
196     }
197     return super.equals(obj);
198     }
199
200     /**
201      * Returns a string representation of this point and its location
202      * in the (<i>x</i>,&nbsp;<i>y</i>) coordinate space. This method is
203      * intended to be used only for debugging purposes, and the content
204      * and format of the returned string may vary between implementations.
205      * The returned string may be empty but may not be <code>null</code>.
206      *
207      * @return a string representation of this point
208      */

209     public String JavaDoc toString() {
210     return getClass().getName() + "[x=" + x + ",y=" + y + "]";
211     }
212 }
213
Popular Tags