home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 October / Chip_1997-10_cd.bin / tema / sybase / powerj / java.z / Rectangle.java < prev    next >
Text File  |  1996-05-03  |  6KB  |  262 lines

  1. /*
  2.  * @(#)Rectangle.java    1.19 96/03/19 Sami Shaio
  3.  *
  4.  * Copyright (c) 1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.awt;
  21.  
  22. /**
  23.  * A rectangle defined by x, y, width and height.
  24.  *
  25.  * @version     1.19, 19 Mar 1996
  26.  * @author     Sami Shaio
  27.  */
  28. public class Rectangle {
  29.  
  30.     /**
  31.      * The x coordinate of the rectangle.
  32.      */
  33.     public int x;
  34.  
  35.     /**
  36.      * The y coordinate of the rectangle.
  37.      */
  38.     public int y;
  39.  
  40.     /**
  41.      * The width of the rectangle.
  42.      */
  43.     public int width;
  44.  
  45.     /**
  46.      * The height of the rectangle.
  47.      */
  48.     public int height;
  49.  
  50.     /**
  51.      * Constructs a new rectangle.
  52.      */
  53.     public Rectangle() {
  54.     }
  55.  
  56.     /**
  57.      * Constructs and initializes a rectangle with the specified parameters.
  58.      * @param x the x coordinate
  59.      * @param y the y coordinate
  60.      * @param width the width of the rectangle
  61.      * @param height the height of the rectangle
  62.      */
  63.     public Rectangle(int x, int y, int width, int height) {
  64.     this.x = x;
  65.     this.y = y;
  66.     this.width = width;
  67.     this.height = height;
  68.     }
  69.  
  70.     /**
  71.      * Constructs a rectangle and initializes it with the specified width and 
  72.      * height parameters.
  73.      * @param width the width of the rectangle
  74.      * @param height the height of the rectangle
  75.      */
  76.     public Rectangle(int width, int height) {
  77.     this(0, 0, width, height);
  78.     }
  79.  
  80.     /**
  81.      * Constructs a rectangle and initializes it to a specified point and  
  82.      * dimension.
  83.      * @param p the point
  84.      * @param d dimension 
  85.      */
  86.     public Rectangle(Point p, Dimension d) {
  87.     this(p.x, p.y, d.width, d.height);
  88.     }
  89.     
  90.     /**
  91.      * Constructs a rectangle and initializes it to the specified point.
  92.      * @param p the value of the x and y coordinate
  93.      */
  94.     public Rectangle(Point p) {
  95.     this(p.x, p.y, 0, 0);
  96.     }
  97.     
  98.     /**
  99.      * Constructs a rectangle and initializes it to the specified width and 
  100.      * height.
  101.      * @param d the value of the width and height
  102.      */
  103.     public Rectangle(Dimension d) {
  104.     this(0, 0, d.width, d.height);
  105.     }
  106.  
  107.     /**
  108.      * Reshapes the rectangle.
  109.      */
  110.     public void reshape(int x, int y, int width, int height) {
  111.     this.x = x;
  112.     this.y = y;
  113.     this.width = width;
  114.     this.height = height;
  115.     }    
  116.  
  117.     /**
  118.      * Moves the rectangle.
  119.      */
  120.     public void move(int x, int y) {
  121.     this.x = x;
  122.     this.y = y;
  123.     }    
  124.  
  125.     /**
  126.      * Translates the rectangle.
  127.      */
  128.     public void translate(int x, int y) {
  129.     this.x += x;
  130.     this.y += y;
  131.     }    
  132.  
  133.     /**
  134.      * Resizes the rectangle.
  135.      */
  136.     public void resize(int width, int height) {
  137.     this.width = width;
  138.     this.height = height;
  139.     }    
  140.  
  141.     /**
  142.      * Checks if the specified point lies inside a rectangle.
  143.      * @param x the x coordinate
  144.      * @param y the y coordinate
  145.      */
  146.     public boolean inside(int x, int y) {
  147.     return (x >= this.x) && ((x - this.x) < this.width) && (y >= this.y) && ((y-this.y) < this.height);
  148.     }
  149.  
  150.     /**
  151.      * Checks if two rectangles intersect.
  152.      */
  153.     public boolean intersects(Rectangle r) {
  154.     return !((r.x + r.width <= x) ||
  155.          (r.y + r.height <= y) ||
  156.          (r.x >= x + width) ||
  157.          (r.y >= y + height));
  158.     }
  159.  
  160.     /**
  161.      * Computes the intersection of two rectangles.
  162.      */
  163.     public Rectangle intersection(Rectangle r) {
  164.     int x1 = Math.max(x, r.x);
  165.     int x2 = Math.min(x + width, r.x + r.width);
  166.     int y1 = Math.max(y, r.y);
  167.     int y2 = Math.min(y + height, r.y + r.height);
  168.     return new Rectangle(x1, y1, x2 - x1, y2 - y1);
  169.     }
  170.  
  171.     /**
  172.      * Computes the union of two rectangles.
  173.      */
  174.     public Rectangle union(Rectangle r) {
  175.     int x1 = Math.min(x, r.x);
  176.     int x2 = Math.max(x + width, r.x + r.width);
  177.     int y1 = Math.min(y, r.y);
  178.     int y2 = Math.max(y + height, r.y + r.height);
  179.     return new Rectangle(x1, y1, x2 - x1, y2 - y1);
  180.     }
  181.  
  182.     /**
  183.      * Adds a point to a rectangle. This results in the smallest
  184.      * rectangle that contains both the rectangle and the point.
  185.      */
  186.     public void add(int newx, int newy) {
  187.     int x1 = Math.min(x, newx);
  188.     int x2 = Math.max(x + width, newx);
  189.     int y1 = Math.min(y, newy);
  190.     int y2 = Math.max(y + height, newy);
  191.     x = x1;
  192.     y = y1;
  193.     width = x2 - x1;
  194.     height = y2 - y1;
  195.     }
  196.  
  197.     /**
  198.      * Adds a point to a rectangle. This results in the smallest
  199.      * rectangle that contains both the rectangle and the point.
  200.      */
  201.     public void add(Point pt) {
  202.     add(pt.x, pt.y);
  203.     }
  204.  
  205.     /**
  206.      * Adds a rectangle to a rectangle. This results in the union
  207.      * of the two rectangles.
  208.      */
  209.     public void add(Rectangle r) {
  210.     int x1 = Math.min(x, r.x);
  211.     int x2 = Math.max(x + width, r.x + r.width);
  212.     int y1 = Math.min(y, r.y);
  213.     int y2 = Math.max(y + height, r.y + r.height);
  214.     x = x1;
  215.     y = y1;
  216.     width = x2 - x1;
  217.     height = y2 - y1;
  218.     }
  219.  
  220.     /**
  221.      * Grows the rectangle horizontally and vertically.
  222.      */
  223.     public void grow(int h, int v) {
  224.     x -= h;
  225.     y -= v;
  226.     width += h * 2;
  227.     height += v * 2;
  228.     }
  229.  
  230.     /**
  231.      * Determines whether the rectangle is empty.
  232.      */
  233.     public boolean isEmpty() {
  234.     return (width <= 0) || (height <= 0);
  235.     }
  236.  
  237.     /**
  238.      * Returns the hashcode for this Rectangle.
  239.      */
  240.     public int hashCode() {
  241.     return x ^ (y*37) ^ (width*43) ^ (height*47);
  242.     }
  243.  
  244.     /**
  245.      * Checks whether two rectangles are equal.
  246.      */
  247.     public boolean equals(Object obj) {
  248.     if (obj instanceof Rectangle) {
  249.         Rectangle r = (Rectangle)obj;
  250.         return (x == r.x) && (y == r.y) && (width == r.width) && (height == r.height);
  251.     }
  252.     return false;
  253.     }
  254.  
  255.     /**
  256.      * Returns the String representation of this Rectangle's values.
  257.      */
  258.     public String toString() {
  259.     return getClass().getName() + "[x=" + x + ",y=" + y + ",width=" + width + ",height=" + height + "]";
  260.     }
  261. }
  262.