home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / Point.java < prev    next >
Text File  |  1997-10-01  |  7KB  |  197 lines

  1. /*
  2.  * @(#)Point.java    1.13 97/06/17
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22. package java.awt;
  23.  
  24. /**
  25.  * The <code>Point</code> class represents a location in a
  26.  * two-dimensional (<i>x</i>, <i>y</i>) coordinate space.
  27.  *
  28.  * @version     1.13, 06/17/97
  29.  * @author     Sami Shaio
  30.  * @since       JDK1.0
  31.  */
  32. public class Point implements java.io.Serializable {
  33.     /**
  34.      * The <i>x</i> coordinate. 
  35.      * @since   JDK1.0
  36.      */
  37.     public int x;
  38.  
  39.     /**
  40.      * The <i>y</i> coordinate. 
  41.      * @since   JDK1.0
  42.      */
  43.     public int y;
  44.  
  45.     /*
  46.      * JDK 1.1 serialVersionUID 
  47.      */
  48.     private static final long serialVersionUID = -5276940640259749850L;
  49.  
  50.     /**
  51.      * Constructs and initializes a point at the origin 
  52.      * (0, 0) of the coordinate space. 
  53.      * @param       x   the <i>x</i> coordinate.
  54.      * @param       y   the <i>y</i> coordinate.
  55.      * @since       JDK1.1
  56.      */
  57.     public Point() {
  58.     this(0, 0);
  59.     }
  60.  
  61.     /**
  62.      * Constructs and initializes a point with the same location as
  63.      * the specified <code>Point</code> object.
  64.      * @param       p a point.
  65.      * @since       JDK1.1
  66.      */
  67.     public Point(Point p) {
  68.     this(p.x, p.y);
  69.     }
  70.  
  71.     /**
  72.      * Constructs and initializes a point at the specified 
  73.      * (<i>x</i>, <i>y</i>) location in the coordinate space. 
  74.      * @param       x   the <i>x</i> coordinate.
  75.      * @param       y   the <i>y</i> coordinate.
  76.      * @since       JDK1.0
  77.      */
  78.     public Point(int x, int y) {
  79.     this.x = x;
  80.     this.y = y;
  81.     }
  82.  
  83.     /**
  84.      * Returns the location of this point.
  85.      * This method is included for completeness, to parallel the
  86.      * <code>getLocation</code> method of <code>Component</code>.
  87.      * @return      a copy of this point, at the same location.
  88.      * @see         java.awt.Component#getLocation
  89.      * @see         java.awt.Point#setLocation(java.awt.Point)
  90.      * @see         java.awt.Point#setLocation(int, int)
  91.      * @since       JDK1.1
  92.      */
  93.     public Point getLocation() {
  94.     return new Point(x, y);
  95.     }    
  96.  
  97.     /**
  98.      * Sets the location of the point to the specificed location.
  99.      * This method is included for completeness, to parallel the
  100.      * <code>setLocation</code> method of <code>Component</code>.
  101.      * @param       p  a point, the new location for this point.
  102.      * @see         java.awt.Component#setLocation(java.awt.Point)
  103.      * @see         java.awt.Point#getLocation
  104.      * @since       JDK1.1
  105.      */
  106.     public void setLocation(Point p) {
  107.     setLocation(p.x, p.y);
  108.     }    
  109.  
  110.     /**
  111.      * Changes the point to have the specificed location.
  112.      * <p>
  113.      * This method is included for completeness, to parallel the
  114.      * <code>setLocation</code> method of <code>Component</code>.
  115.      * Its behavior is identical with <code>move(int, int)</code>.
  116.      * @param       x  the <i>x</i> coordinate of the new location.
  117.      * @param       y  the <i>y</i> coordinate of the new location.
  118.      * @see         java.awt.Component#setLocation(int, int)
  119.      * @see         java.awt.Point#getLocation
  120.      * @see         java.awt.Point#move(int, int)
  121.      * @since       JDK1.1
  122.      */
  123.     public void setLocation(int x, int y) {
  124.     move(x, y);
  125.     }    
  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.      * @since       JDK1.0
  135.      */
  136.     public void move(int x, int y) {
  137.     this.x = x;
  138.     this.y = y;
  139.     }    
  140.  
  141.     /**
  142.      * Translates this point, at location (<i>x</i>, <i>y</i>), 
  143.      * by <code>dx</code> along the <i>x</i> axis and <code>dy</code> 
  144.      * along the <i>y</i> axis so that it now represents the point 
  145.      * (<code>x</code> <code>+</code> <code>dx</code>, 
  146.      * <code>y</code> <code>+</code> <code>dy</code>). 
  147.      * @param       dx   the distance to move this point 
  148.      *                            along the <i>x</i> axis.
  149.      * @param       dy    the distance to move this point 
  150.      *                            along the <i>y</i> axis.
  151.      * @since       JDK1.0
  152.      */
  153.     public void translate(int x, int y) {
  154.     this.x += x;
  155.     this.y += y;
  156.     }    
  157.  
  158.     /**
  159.      * Returns the hashcode for this point.
  160.      * @return      a hash code for this point.
  161.      * @since       JDK1.0
  162.      */
  163.     public int hashCode() {
  164.     return x ^ (y*31);
  165.     }
  166.  
  167.     /**
  168.      * Determines whether two points are equal. Two instances of
  169.      * <code>Point</code> are equal if the values of their 
  170.      * <code>x</code> and <code>y</code> member fields, representing
  171.      * their position in the coordinate space, are the same.
  172.      * @param      obj   an object to be compared with this point.
  173.      * @return     <code>true</code> if the object to be compared is
  174.      *                     an instance of <code>Point</code> and has
  175.      *                     the same values; <code>false</code> otherwise.
  176.      * @since      JDK1.0
  177.      */
  178.     public boolean equals(Object obj) {
  179.     if (obj instanceof Point) {
  180.         Point pt = (Point)obj;
  181.         return (x == pt.x) && (y == pt.y);
  182.     }
  183.     return false;
  184.     }
  185.  
  186.     /**
  187.      * Returns a representation of this point and its location
  188.      * in the (<i>x</i>, <i>y</i>) coordinate space as a string.
  189.      * @return    a string representation of this point, 
  190.      *                 including the values of its member fields.
  191.      * @since     JDK1.0
  192.      */
  193.     public String toString() {
  194.     return getClass().getName() + "[x=" + x + ",y=" + y + "]";
  195.     }
  196. }
  197.