home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 5.2 KB | 236 lines |
- /*
- * @(#)Point2D.java 1.6 98/03/18
- *
- * Copyright 1997, 1998 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.geom;
-
- /**
- * A point representing a location in (x, y) coordinate space.
- * <p>
- * This class is only the abstract superclass for all objects which
- * store a 2D coordinate.
- * The actual storage representation of the coordinates is left to
- * the subclass.
- *
- * @version 10 Feb 1997
- * @author Jim Graham
- */
- public abstract class Point2D implements Cloneable {
- /**
- * A point specified in float precision.
- */
- public static class Float extends Point2D {
- /**
- * The X coordinate.
- * @since JDK1.2
- */
- public float x;
-
- /**
- * The Y coordinate.
- * @since JDK1.2
- */
- public float y;
-
- /**
- * Constructs and initializes a Point with coordinates (0, 0).
- * @since JDK1.2
- */
- public Float() {
- }
-
- /**
- * Constructs and initializes a Point with the specified coordinates.
- * @since JDK1.2
- */
- public Float(float x, float y) {
- this.x = x;
- this.y = y;
- }
-
- /**
- * Returns the X coordinate of the point in double precision.
- * @since JDK1.2
- */
- public double getX() {
- return (double) x;
- }
-
- /**
- * Returns the Y coordinate of the point in double precision.
- * @since JDK1.2
- */
- public double getY() {
- return (double) y;
- }
-
- /**
- * Sets the location of this point to the specified double coordinates.
- * @since JDK1.2
- */
- public void setLocation(double x, double y) {
- this.x = (float) x;
- this.y = (float) y;
- }
-
- /**
- * Sets the location of this point to the specified float coordinates.
- * @since JDK1.2
- */
- public void setLocation(float x, float y) {
- this.x = x;
- this.y = y;
- }
-
- /**
- * Returns a String that represents the value of this Object.
- * @since JDK1.2
- */
- public String toString() {
- return "Point2D.Float["+x+", "+y+"]";
- }
- }
-
- /**
- * A point specified in double precision.
- */
- public static class Double extends Point2D {
- /**
- * The x coordinate of the point.
- * @since JDK1.2
- */
- public double x;
-
- /**
- * The y coordinate of the point.
- * @since JDK1.2
- */
- public double y;
-
- /**
- * Constructs and initializes a Point with coordinates (0, 0).
- * @since JDK1.2
- */
- public Double() {
- }
-
- /**
- * Constructs and initializes a Point with the specified coordinates.
- * @since JDK1.2
- */
- public Double(double x, double y) {
- this.x = x;
- this.y = y;
- }
-
- /**
- * Returns the X coordinate of the point in double precision.
- * @since JDK1.2
- */
- public double getX() {
- return x;
- }
-
- /**
- * Returns the Y coordinate of the point in double precision.
- * @since JDK1.2
- */
- public double getY() {
- return y;
- }
-
- /**
- * Sets the location of this point to the specified double coordinates.
- * @since JDK1.2
- */
- public void setLocation(double x, double y) {
- this.x = x;
- this.y = y;
- }
-
- /**
- * Returns a String that represents the value of this Object.
- * @since JDK1.2
- */
- public String toString() {
- return "Point2D.Double["+x+", "+y+"]";
- }
- }
-
- protected Point2D() {
- }
-
- /**
- * Returns the X coordinate of the point in double precision.
- * @since JDK1.2
- */
- public abstract double getX();
-
- /**
- * Returns the Y coordinate of the point in double precision.
- * @since JDK1.2
- */
- public abstract double getY();
-
- /**
- * Sets the location of this point to the specified float coordinates.
- * @since JDK1.2
- */
- public abstract void setLocation(double x, double y);
-
- /**
- * Sets the location of this point to the same coordinates as the
- * specified Point object.
- * @since JDK1.2
- */
- public void setLocation(Point2D p) {
- setLocation(p.getX(), p.getY());
- }
-
- /**
- * Creates a new object of the same class as this object.
- *
- * @return a clone of this instance.
- * @exception OutOfMemoryError if there is not enough memory.
- * @see java.lang.Cloneable
- * @since JDK1.2
- */
- public Object clone() {
- try {
- return super.clone();
- } catch (CloneNotSupportedException e) {
- // this shouldn't happen, since we are Cloneable
- throw new InternalError();
- }
- }
-
- /**
- * Determines whether two points are equal. Two instances of
- * <code>Point2D</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>Point2D</code> and has
- * the same values; <code>false</code> otherwise.
- * @since JDK1.2
- */
- public boolean equals(Object obj) {
- if (obj instanceof Point2D) {
- Point2D p2d = (Point2D) obj;
- return (getX() == p2d.getX()) && (getY() == p2d.getY());
- }
- return super.equals(obj);
- }
- }
-