home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / awt / geom / Point2D.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  5.2 KB  |  236 lines

  1. /*
  2.  * @(#)Point2D.java    1.6 98/03/18
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt.geom;
  16.  
  17. /**
  18.  * A point representing a location in (x, y) coordinate space.
  19.  * <p>
  20.  * This class is only the abstract superclass for all objects which
  21.  * store a 2D coordinate.
  22.  * The actual storage representation of the coordinates is left to
  23.  * the subclass.
  24.  *
  25.  * @version 10 Feb 1997
  26.  * @author    Jim Graham
  27.  */
  28. public abstract class Point2D implements Cloneable {
  29.     /**
  30.      * A point specified in float precision.
  31.      */
  32.     public static class Float extends Point2D {
  33.     /**
  34.      * The X coordinate.
  35.      * @since JDK1.2
  36.      */
  37.     public float x;
  38.  
  39.     /**
  40.      * The Y coordinate.
  41.      * @since JDK1.2
  42.      */
  43.     public float y;
  44.  
  45.     /**
  46.      * Constructs and initializes a Point with coordinates (0, 0).
  47.      * @since JDK1.2
  48.      */
  49.     public Float() {
  50.     }
  51.  
  52.     /**
  53.      * Constructs and initializes a Point with the specified coordinates.
  54.      * @since JDK1.2
  55.      */
  56.     public Float(float x, float y) {
  57.         this.x = x;
  58.         this.y = y;
  59.     }
  60.  
  61.     /**
  62.      * Returns the X coordinate of the point in double precision.
  63.      * @since JDK1.2
  64.      */
  65.     public double getX() {
  66.         return (double) x;
  67.     }
  68.  
  69.     /**
  70.      * Returns the Y coordinate of the point in double precision.
  71.      * @since JDK1.2
  72.      */
  73.     public double getY() {
  74.         return (double) y;
  75.     }
  76.  
  77.     /**
  78.      * Sets the location of this point to the specified double coordinates.
  79.      * @since JDK1.2
  80.      */
  81.     public void setLocation(double x, double y) {
  82.         this.x = (float) x;
  83.         this.y = (float) y;
  84.     }
  85.  
  86.     /**
  87.      * Sets the location of this point to the specified float coordinates.
  88.      * @since JDK1.2
  89.      */
  90.     public void setLocation(float x, float y) {
  91.         this.x = x;
  92.         this.y = y;
  93.     }
  94.  
  95.     /**
  96.      * Returns a String that represents the value of this Object.
  97.      * @since JDK1.2
  98.      */
  99.     public String toString() {
  100.         return "Point2D.Float["+x+", "+y+"]";
  101.     }
  102.     }
  103.  
  104.     /**
  105.      * A point specified in double precision.
  106.      */
  107.     public static class Double extends Point2D {
  108.     /**
  109.      * The x coordinate of the point.
  110.      * @since JDK1.2
  111.      */
  112.     public double x;
  113.  
  114.     /**
  115.      * The y coordinate of the point.
  116.      * @since JDK1.2
  117.      */
  118.     public double y;
  119.  
  120.     /**
  121.      * Constructs and initializes a Point with coordinates (0, 0).
  122.      * @since JDK1.2
  123.      */
  124.     public Double() {
  125.     }
  126.  
  127.     /**
  128.      * Constructs and initializes a Point with the specified coordinates.
  129.      * @since JDK1.2
  130.      */
  131.     public Double(double x, double y) {
  132.         this.x = x;
  133.         this.y = y;
  134.     }
  135.  
  136.     /**
  137.      * Returns the X coordinate of the point in double precision.
  138.      * @since JDK1.2
  139.      */
  140.     public double getX() {
  141.         return x;
  142.     }
  143.  
  144.     /**
  145.      * Returns the Y coordinate of the point in double precision.
  146.      * @since JDK1.2
  147.      */
  148.     public double getY() {
  149.         return y;
  150.     }
  151.  
  152.     /**
  153.      * Sets the location of this point to the specified double coordinates.
  154.      * @since JDK1.2
  155.      */
  156.     public void setLocation(double x, double y) {
  157.         this.x = x;
  158.         this.y = y;
  159.     }
  160.  
  161.     /**
  162.      * Returns a String that represents the value of this Object.
  163.      * @since JDK1.2
  164.      */
  165.     public String toString() {
  166.         return "Point2D.Double["+x+", "+y+"]";
  167.     }
  168.     }
  169.  
  170.     protected Point2D() {
  171.     }
  172.  
  173.     /**
  174.      * Returns the X coordinate of the point in double precision.
  175.      * @since JDK1.2
  176.      */
  177.     public abstract double getX();
  178.  
  179.     /**
  180.      * Returns the Y coordinate of the point in double precision.
  181.      * @since JDK1.2
  182.      */
  183.     public abstract double getY();
  184.  
  185.     /**
  186.      * Sets the location of this point to the specified float coordinates.
  187.      * @since JDK1.2
  188.      */
  189.     public abstract void setLocation(double x, double y);
  190.  
  191.     /**
  192.      * Sets the location of this point to the same coordinates as the
  193.      * specified Point object.
  194.      * @since JDK1.2
  195.      */
  196.     public void setLocation(Point2D p) {
  197.     setLocation(p.getX(), p.getY());
  198.     }
  199.  
  200.     /**
  201.      * Creates a new object of the same class as this object.
  202.      *
  203.      * @return     a clone of this instance.
  204.      * @exception  OutOfMemoryError            if there is not enough memory.
  205.      * @see        java.lang.Cloneable
  206.      * @since      JDK1.2
  207.      */
  208.     public Object clone() {
  209.     try {
  210.         return super.clone();
  211.     } catch (CloneNotSupportedException e) {
  212.         // this shouldn't happen, since we are Cloneable
  213.         throw new InternalError();
  214.     }
  215.     }
  216.  
  217.     /**
  218.      * Determines whether two points are equal. Two instances of
  219.      * <code>Point2D</code> are equal if the values of their 
  220.      * <code>x</code> and <code>y</code> member fields, representing
  221.      * their position in the coordinate space, are the same.
  222.      * @param      obj   an object to be compared with this point.
  223.      * @return     <code>true</code> if the object to be compared is
  224.      *                     an instance of <code>Point2D</code> and has
  225.      *                     the same values; <code>false</code> otherwise.
  226.      * @since JDK1.2
  227.      */
  228.     public boolean equals(Object obj) {
  229.     if (obj instanceof Point2D) {
  230.         Point2D p2d = (Point2D) obj;
  231.         return (getX() == p2d.getX()) && (getY() == p2d.getY());
  232.     }
  233.     return super.equals(obj);
  234.     }
  235. }
  236.