1. /*
  2. * @(#)Point.java 1.23 01/11/29
  3. *
  4. * Copyright 2002 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.23, 11/29/01
  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()
  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()
  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. */
  70. public double getX() {
  71. return x;
  72. }
  73. /**
  74. * Returns the Y coordinate of the point in double precision.
  75. */
  76. public double getY() {
  77. return y;
  78. }
  79. /**
  80. * Returns the location of this point.
  81. * This method is included for completeness, to parallel the
  82. * <code>getLocation</code> method of <code>Component</code>.
  83. * @return a copy of this point, at the same location.
  84. * @see java.awt.Component#getLocation
  85. * @see java.awt.Point#setLocation(java.awt.Point)
  86. * @see java.awt.Point#setLocation(int, int)
  87. * @since JDK1.1
  88. */
  89. public Point getLocation() {
  90. return new Point(x, y);
  91. }
  92. /**
  93. * Sets the location of the point to the specificed location.
  94. * This method is included for completeness, to parallel the
  95. * <code>setLocation</code> method of <code>Component</code>.
  96. * @param p a point, the new location for this point.
  97. * @see java.awt.Component#setLocation(java.awt.Point)
  98. * @see java.awt.Point#getLocation
  99. * @since JDK1.1
  100. */
  101. public void setLocation(Point p) {
  102. setLocation(p.x, p.y);
  103. }
  104. /**
  105. * Changes the point to have the specificed location.
  106. * <p>
  107. * This method is included for completeness, to parallel the
  108. * <code>setLocation</code> method of <code>Component</code>.
  109. * Its behavior is identical with <code>move(int, int)</code>.
  110. * @param x the <i>x</i> coordinate of the new location.
  111. * @param y the <i>y</i> coordinate of the new location.
  112. * @see java.awt.Component#setLocation(int, int)
  113. * @see java.awt.Point#getLocation
  114. * @see java.awt.Point#move(int, int)
  115. * @since JDK1.1
  116. */
  117. public void setLocation(int x, int y) {
  118. move(x, y);
  119. }
  120. /**
  121. * Sets the location of this point to the specified float coordinates.
  122. */
  123. public void setLocation(double x, double y) {
  124. this.x = (int) Math.round(x);
  125. this.y = (int) Math.round(y);
  126. }
  127. /**
  128. * Moves this point to the specificed location in the
  129. * (<i>x</i>, <i>y</i>) coordinate plane. This method
  130. * is identical with <code>setLocation(int, int)</code>.
  131. * @param x the <i>x</i> coordinate of the new location.
  132. * @param y the <i>y</i> coordinate of the new location.
  133. * @see java.awt.Component#setLocation(int, int)
  134. */
  135. public void move(int x, int y) {
  136. this.x = x;
  137. this.y = y;
  138. }
  139. /**
  140. * Translates this point, at location (<i>x</i>, <i>y</i>),
  141. * by <code>dx</code> along the <i>x</i> axis and <code>dy</code>
  142. * along the <i>y</i> axis so that it now represents the point
  143. * (<code>x</code> <code>+</code> <code>dx</code>,
  144. * <code>y</code> <code>+</code> <code>dy</code>).
  145. * @param dx the distance to move this point
  146. * along the <i>x</i> axis.
  147. * @param dy the distance to move this point
  148. * along the <i>y</i> axis.
  149. */
  150. public void translate(int x, int y) {
  151. this.x += x;
  152. this.y += y;
  153. }
  154. /**
  155. * Determines whether two points are equal. Two instances of
  156. * <code>Point</code> are equal if the values of their
  157. * <code>x</code> and <code>y</code> member fields, representing
  158. * their position in the coordinate space, are the same.
  159. * @param obj an object to be compared with this point.
  160. * @return <code>true</code> if the object to be compared is
  161. * an instance of <code>Point</code> and has
  162. * the same values; <code>false</code> otherwise.
  163. */
  164. public boolean equals(Object obj) {
  165. if (obj instanceof Point) {
  166. Point pt = (Point)obj;
  167. return (x == pt.x) && (y == pt.y);
  168. }
  169. return super.equals(obj);
  170. }
  171. /**
  172. * Returns a string representation of this point and its location
  173. * in the (<i>x</i>, <i>y</i>) coordinate space. This method is
  174. * intended to be used only for debugging purposes, and the content
  175. * and format of the returned string may vary between implementations.
  176. * The returned string may be empty but may not be <code>null</code>.
  177. *
  178. * @return a string representation of this point.
  179. */
  180. public String toString() {
  181. return getClass().getName() + "[x=" + x + ",y=" + y + "]";
  182. }
  183. }