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