1. /*
  2. * @(#)Point.java 1.37 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt;
  8. import java.awt.geom.Point2D;
  9. /**
  10. * A point representing a location in (x, y) coordinate space, specified
  11. * in integer precision.
  12. *
  13. * @version 1.37, 01/23/03
  14. * @author Sami Shaio
  15. * @since JDK1.0
  16. */
  17. public class Point extends Point2D implements java.io.Serializable {
  18. /**
  19. * The <i>x</i> coordinate.
  20. * If no <i>x</i> coordinate is set it will default to 0.
  21. *
  22. * @serial
  23. * @see #getLocation()
  24. * @see #move(int, int)
  25. */
  26. public int x;
  27. /**
  28. * The <i>y</i> coordinate.
  29. * If no <i>y</i> coordinate is set it will default to 0.
  30. *
  31. * @serial
  32. * @see #getLocation()
  33. * @see #move(int, int)
  34. */
  35. public int y;
  36. /*
  37. * JDK 1.1 serialVersionUID
  38. */
  39. private static final long serialVersionUID = -5276940640259749850L;
  40. /**
  41. * Constructs and initializes a point at the origin
  42. * (0, 0) of the coordinate space.
  43. * @since JDK1.1
  44. */
  45. public Point() {
  46. this(0, 0);
  47. }
  48. /**
  49. * Constructs and initializes a point with the same location as
  50. * the specified <code>Point</code> object.
  51. * @param p a point
  52. * @since JDK1.1
  53. */
  54. public Point(Point p) {
  55. this(p.x, p.y);
  56. }
  57. /**
  58. * Constructs and initializes a point at the specified
  59. * (<i>x</i>, <i>y</i>) location in the coordinate space.
  60. * @param x the <i>x</i> coordinate
  61. * @param y the <i>y</i> coordinate
  62. */
  63. public Point(int x, int y) {
  64. this.x = x;
  65. this.y = y;
  66. }
  67. /**
  68. * Returns the X coordinate of the point in double precision.
  69. * @return the X coordinate of the point in double precision
  70. */
  71. public double getX() {
  72. return x;
  73. }
  74. /**
  75. * Returns the Y coordinate of the point in double precision.
  76. * @return the Y coordinate of the point in double precision
  77. */
  78. public double getY() {
  79. return y;
  80. }
  81. /**
  82. * Returns the location of this point.
  83. * This method is included for completeness, to parallel the
  84. * <code>getLocation</code> method of <code>Component</code>.
  85. * @return a copy of this point, at the same location
  86. * @see java.awt.Component#getLocation
  87. * @see java.awt.Point#setLocation(java.awt.Point)
  88. * @see java.awt.Point#setLocation(int, int)
  89. * @since JDK1.1
  90. */
  91. public Point getLocation() {
  92. return new Point(x, y);
  93. }
  94. /**
  95. * Sets the location of the point to the specified location.
  96. * This method is included for completeness, to parallel the
  97. * <code>setLocation</code> method of <code>Component</code>.
  98. * @param p a point, the new location for this point
  99. * @see java.awt.Component#setLocation(java.awt.Point)
  100. * @see java.awt.Point#getLocation
  101. * @since JDK1.1
  102. */
  103. public void setLocation(Point p) {
  104. setLocation(p.x, p.y);
  105. }
  106. /**
  107. * Changes the point to have the specified location.
  108. * <p>
  109. * This method is included for completeness, to parallel the
  110. * <code>setLocation</code> method of <code>Component</code>.
  111. * Its behavior is identical with <code>move(int, int)</code>.
  112. * @param x the <i>x</i> coordinate of the new location
  113. * @param y the <i>y</i> coordinate of the new location
  114. * @see java.awt.Component#setLocation(int, int)
  115. * @see java.awt.Point#getLocation
  116. * @see java.awt.Point#move(int, int)
  117. * @since JDK1.1
  118. */
  119. public void setLocation(int x, int y) {
  120. move(x, y);
  121. }
  122. /**
  123. * Sets the location of this point to the specified double coordinates.
  124. * The double values will be rounded to integer values.
  125. * Any number smaller than <code>Integer.MIN_VALUE</code>
  126. * will be reset to <code>MIN_VALUE</code>, and any number
  127. * larger than <code>Integer.MAX_VALUE</code> will be
  128. * reset to <code>MAX_VALUE</code>.
  129. *
  130. * @param x the <i>x</i> coordinate of the new location
  131. * @param y the <i>y</i> coordinate of the new location
  132. * @see #getLocation
  133. */
  134. public void setLocation(double x, double y) {
  135. this.x = (int) Math.floor(x+0.5);
  136. this.y = (int) Math.floor(y+0.5);
  137. }
  138. /**
  139. * Moves this point to the specified location in the
  140. * (<i>x</i>, <i>y</i>) coordinate plane. This method
  141. * is identical with <code>setLocation(int, int)</code>.
  142. * @param x the <i>x</i> coordinate of the new location
  143. * @param y the <i>y</i> coordinate of the new location
  144. * @see java.awt.Component#setLocation(int, int)
  145. */
  146. public void move(int x, int y) {
  147. this.x = x;
  148. this.y = y;
  149. }
  150. /**
  151. * Translates this point, at location (<i>x</i>, <i>y</i>),
  152. * by <code>dx</code> along the <i>x</i> axis and <code>dy</code>
  153. * along the <i>y</i> axis so that it now represents the point
  154. * (<code>x</code> <code>+</code> <code>dx</code>,
  155. * <code>y</code> <code>+</code> <code>dy</code>).
  156. * @param dx the distance to move this point
  157. * along the <i>x</i> axis
  158. * @param dy the distance to move this point
  159. * along the <i>y</i> axis
  160. */
  161. public void translate(int dx, int dy) {
  162. this.x += dx;
  163. this.y += dy;
  164. }
  165. /**
  166. * Determines whether or not two points are equal. Two instances of
  167. * <code>Point2D</code> are equal if the values of their
  168. * <code>x</code> and <code>y</code> member fields, representing
  169. * their position in the coordinate space, are the same.
  170. * @param obj an object to be compared with this <code>Point2D</code>
  171. * @return <code>true</code> if the object to be compared is
  172. * an instance of <code>Point2D</code> and has
  173. * the same values; <code>false</code> otherwise.
  174. */
  175. public boolean equals(Object obj) {
  176. if (obj instanceof Point) {
  177. Point pt = (Point)obj;
  178. return (x == pt.x) && (y == pt.y);
  179. }
  180. return super.equals(obj);
  181. }
  182. /**
  183. * Returns a string representation of this point and its location
  184. * in the (<i>x</i>, <i>y</i>) coordinate space. This method is
  185. * intended to be used only for debugging purposes, and the content
  186. * and format of the returned string may vary between implementations.
  187. * The returned string may be empty but may not be <code>null</code>.
  188. *
  189. * @return a string representation of this point
  190. */
  191. public String toString() {
  192. return getClass().getName() + "[x=" + x + ",y=" + y + "]";
  193. }
  194. }