home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 6.3 KB | 206 lines |
- /*
- * @(#)Point.java 1.17 98/03/18
- *
- * Copyright 1995-1997 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- package java.awt;
-
- import java.awt.geom.Point2D;
-
- /**
- * A point representing a location in (x, y) coordinate space, specified
- * in integer precision.
- *
- * @version 1.17, 03/18/98
- * @author Sami Shaio
- * @since JDK1.0
- */
- public class Point extends Point2D implements java.io.Serializable {
- /**
- * The <i>x</i> coordinate.
- */
- public int x;
-
- /**
- * The <i>y</i> coordinate.
- */
- public int y;
-
- /*
- * JDK 1.1 serialVersionUID
- */
- private static final long serialVersionUID = -5276940640259749850L;
-
- /**
- * Constructs and initializes a point at the origin
- * (0, 0) of the coordinate space.
- * @param x the <i>x</i> coordinate.
- * @param y the <i>y</i> coordinate.
- * @since JDK1.1
- */
- public Point() {
- this(0, 0);
- }
-
- /**
- * Constructs and initializes a point with the same location as
- * the specified <code>Point</code> object.
- * @param p a point.
- * @since JDK1.1
- */
- public Point(Point p) {
- this(p.x, p.y);
- }
-
- /**
- * Constructs and initializes a point at the specified
- * (<i>x</i>, <i>y</i>) location in the coordinate space.
- * @param x the <i>x</i> coordinate.
- * @param y the <i>y</i> coordinate.
- */
- public Point(int x, int y) {
- this.x = x;
- this.y = y;
- }
-
- /**
- * Returns the X coordinate of the point in double precision.
- */
- public double getX() {
- return x;
- }
-
- /**
- * Returns the Y coordinate of the point in double precision.
- */
- public double getY() {
- return y;
- }
-
- /**
- * Returns the location of this point.
- * This method is included for completeness, to parallel the
- * <code>getLocation</code> method of <code>Component</code>.
- * @return a copy of this point, at the same location.
- * @see java.awt.Component#getLocation
- * @see java.awt.Point#setLocation(java.awt.Point)
- * @see java.awt.Point#setLocation(int, int)
- * @since JDK1.1
- */
- public Point getLocation() {
- return new Point(x, y);
- }
-
- /**
- * Sets the location of the point to the specificed location.
- * This method is included for completeness, to parallel the
- * <code>setLocation</code> method of <code>Component</code>.
- * @param p a point, the new location for this point.
- * @see java.awt.Component#setLocation(java.awt.Point)
- * @see java.awt.Point#getLocation
- * @since JDK1.1
- */
- public void setLocation(Point p) {
- setLocation(p.x, p.y);
- }
-
- /**
- * Changes the point to have the specificed location.
- * <p>
- * This method is included for completeness, to parallel the
- * <code>setLocation</code> method of <code>Component</code>.
- * Its behavior is identical with <code>move(int, int)</code>.
- * @param x the <i>x</i> coordinate of the new location.
- * @param y the <i>y</i> coordinate of the new location.
- * @see java.awt.Component#setLocation(int, int)
- * @see java.awt.Point#getLocation
- * @see java.awt.Point#move(int, int)
- * @since JDK1.1
- */
- public void setLocation(int x, int y) {
- move(x, y);
- }
-
- /**
- * Sets the location of this point to the specified float coordinates.
- */
- public void setLocation(double x, double y) {
- this.x = (int) Math.round(x);
- this.y = (int) Math.round(y);
- }
-
- /**
- * Moves this point to the specificed location in the
- * (<i>x</i>, <i>y</i>) coordinate plane. This method
- * is identical with <code>setLocation(int, int)</code>.
- * @param x the <i>x</i> coordinate of the new location.
- * @param y the <i>y</i> coordinate of the new location.
- * @see java.awt.Component#setLocation(int, int)
- */
- public void move(int x, int y) {
- this.x = x;
- this.y = y;
- }
-
- /**
- * Translates this point, at location (<i>x</i>, <i>y</i>),
- * by <code>dx</code> along the <i>x</i> axis and <code>dy</code>
- * along the <i>y</i> axis so that it now represents the point
- * (<code>x</code> <code>+</code> <code>dx</code>,
- * <code>y</code> <code>+</code> <code>dy</code>).
- * @param dx the distance to move this point
- * along the <i>x</i> axis.
- * @param dy the distance to move this point
- * along the <i>y</i> axis.
- */
- public void translate(int x, int y) {
- this.x += x;
- this.y += y;
- }
-
- /**
- * Returns the hashcode for this point.
- * @return a hash code for this point.
- */
- public int hashCode() {
- return x ^ (y*31);
- }
-
- /**
- * Determines whether two points are equal. Two instances of
- * <code>Point</code> are equal if the values of their
- * <code>x</code> and <code>y</code> member fields, representing
- * their position in the coordinate space, are the same.
- * @param obj an object to be compared with this point.
- * @return <code>true</code> if the object to be compared is
- * an instance of <code>Point</code> and has
- * the same values; <code>false</code> otherwise.
- */
- public boolean equals(Object obj) {
- if (obj instanceof Point) {
- Point pt = (Point)obj;
- return (x == pt.x) && (y == pt.y);
- }
- return super.equals(obj);
- }
-
- /**
- * Returns a representation of this point and its location
- * in the (<i>x</i>, <i>y</i>) coordinate space as a string.
- * @return a string representation of this point,
- * including the values of its member fields.
- */
- public String toString() {
- return getClass().getName() + "[x=" + x + ",y=" + y + "]";
- }
- }
-