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

  1. /*
  2.  * @(#)Point.java    1.17 98/03/18
  3.  *
  4.  * Copyright 1995-1997 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;
  16.  
  17. import java.awt.geom.Point2D;
  18.  
  19. /**
  20.  * A point representing a location in (x, y) coordinate space, specified
  21.  * in integer precision.
  22.  *
  23.  * @version     1.17, 03/18/98
  24.  * @author     Sami Shaio
  25.  * @since       JDK1.0
  26.  */
  27. public class Point extends Point2D implements java.io.Serializable {
  28.     /**
  29.      * The <i>x</i> coordinate. 
  30.      */
  31.     public int x;
  32.  
  33.     /**
  34.      * The <i>y</i> coordinate. 
  35.      */
  36.     public int y;
  37.  
  38.     /*
  39.      * JDK 1.1 serialVersionUID 
  40.      */
  41.     private static final long serialVersionUID = -5276940640259749850L;
  42.  
  43.     /**
  44.      * Constructs and initializes a point at the origin 
  45.      * (0, 0) of the coordinate space. 
  46.      * @param       x   the <i>x</i> coordinate.
  47.      * @param       y   the <i>y</i> coordinate.
  48.      * @since       JDK1.1
  49.      */
  50.     public Point() {
  51.     this(0, 0);
  52.     }
  53.  
  54.     /**
  55.      * Constructs and initializes a point with the same location as
  56.      * the specified <code>Point</code> object.
  57.      * @param       p a point.
  58.      * @since       JDK1.1
  59.      */
  60.     public Point(Point p) {
  61.     this(p.x, p.y);
  62.     }
  63.  
  64.     /**
  65.      * Constructs and initializes a point at the specified 
  66.      * (<i>x</i>, <i>y</i>) location in the coordinate space. 
  67.      * @param       x   the <i>x</i> coordinate.
  68.      * @param       y   the <i>y</i> coordinate.
  69.      */
  70.     public Point(int x, int y) {
  71.     this.x = x;
  72.     this.y = y;
  73.     }
  74.  
  75.     /**
  76.      * Returns the X coordinate of the point in double precision.
  77.      */
  78.     public double getX() {
  79.     return x;
  80.     }
  81.  
  82.     /**
  83.      * Returns the Y coordinate of the point in double precision.
  84.      */
  85.     public double getY() {
  86.     return y;
  87.     }
  88.  
  89.     /**
  90.      * Returns the location of this point.
  91.      * This method is included for completeness, to parallel the
  92.      * <code>getLocation</code> method of <code>Component</code>.
  93.      * @return      a copy of this point, at the same location.
  94.      * @see         java.awt.Component#getLocation
  95.      * @see         java.awt.Point#setLocation(java.awt.Point)
  96.      * @see         java.awt.Point#setLocation(int, int)
  97.      * @since       JDK1.1
  98.      */
  99.     public Point getLocation() {
  100.     return new Point(x, y);
  101.     }    
  102.  
  103.     /**
  104.      * Sets the location of the point to the specificed location.
  105.      * This method is included for completeness, to parallel the
  106.      * <code>setLocation</code> method of <code>Component</code>.
  107.      * @param       p  a point, the new location for this point.
  108.      * @see         java.awt.Component#setLocation(java.awt.Point)
  109.      * @see         java.awt.Point#getLocation
  110.      * @since       JDK1.1
  111.      */
  112.     public void setLocation(Point p) {
  113.     setLocation(p.x, p.y);
  114.     }    
  115.  
  116.     /**
  117.      * Changes the point to have the specificed location.
  118.      * <p>
  119.      * This method is included for completeness, to parallel the
  120.      * <code>setLocation</code> method of <code>Component</code>.
  121.      * Its behavior is identical with <code>move(int, int)</code>.
  122.      * @param       x  the <i>x</i> coordinate of the new location.
  123.      * @param       y  the <i>y</i> coordinate of the new location.
  124.      * @see         java.awt.Component#setLocation(int, int)
  125.      * @see         java.awt.Point#getLocation
  126.      * @see         java.awt.Point#move(int, int)
  127.      * @since       JDK1.1
  128.      */
  129.     public void setLocation(int x, int y) {
  130.     move(x, y);
  131.     }    
  132.  
  133.     /**
  134.      * Sets the location of this point to the specified float coordinates.
  135.      */
  136.     public void setLocation(double x, double y) {
  137.     this.x = (int) Math.round(x);
  138.     this.y = (int) Math.round(y);
  139.     }
  140.  
  141.     /**
  142.      * Moves this point to the specificed location in the 
  143.      * (<i>x</i>, <i>y</i>) coordinate plane. This method
  144.      * is identical with <code>setLocation(int, int)</code>.
  145.      * @param       x  the <i>x</i> coordinate of the new location.
  146.      * @param       y  the <i>y</i> coordinate of the new location.
  147.      * @see         java.awt.Component#setLocation(int, int)
  148.      */
  149.     public void move(int x, int y) {
  150.     this.x = x;
  151.     this.y = y;
  152.     }    
  153.  
  154.     /**
  155.      * Translates this point, at location (<i>x</i>, <i>y</i>), 
  156.      * by <code>dx</code> along the <i>x</i> axis and <code>dy</code> 
  157.      * along the <i>y</i> axis so that it now represents the point 
  158.      * (<code>x</code> <code>+</code> <code>dx</code>, 
  159.      * <code>y</code> <code>+</code> <code>dy</code>). 
  160.      * @param       dx   the distance to move this point 
  161.      *                            along the <i>x</i> axis.
  162.      * @param       dy    the distance to move this point 
  163.      *                            along the <i>y</i> axis.
  164.      */
  165.     public void translate(int x, int y) {
  166.     this.x += x;
  167.     this.y += y;
  168.     }    
  169.  
  170.     /**
  171.      * Returns the hashcode for this point.
  172.      * @return      a hash code for this point.
  173.      */
  174.     public int hashCode() {
  175.     return x ^ (y*31);
  176.     }
  177.  
  178.     /**
  179.      * Determines whether two points are equal. Two instances of
  180.      * <code>Point</code> are equal if the values of their 
  181.      * <code>x</code> and <code>y</code> member fields, representing
  182.      * their position in the coordinate space, are the same.
  183.      * @param      obj   an object to be compared with this point.
  184.      * @return     <code>true</code> if the object to be compared is
  185.      *                     an instance of <code>Point</code> and has
  186.      *                     the same values; <code>false</code> otherwise.
  187.      */
  188.     public boolean equals(Object obj) {
  189.     if (obj instanceof Point) {
  190.         Point pt = (Point)obj;
  191.         return (x == pt.x) && (y == pt.y);
  192.     }
  193.     return super.equals(obj);
  194.     }
  195.  
  196.     /**
  197.      * Returns a representation of this point and its location
  198.      * in the (<i>x</i>, <i>y</i>) coordinate space as a string.
  199.      * @return    a string representation of this point, 
  200.      *                 including the values of its member fields.
  201.      */
  202.     public String toString() {
  203.     return getClass().getName() + "[x=" + x + ",y=" + y + "]";
  204.     }
  205. }
  206.