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

  1. /*
  2.  * @(#)Dimension.java    1.16 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.Dimension2D;
  18.  
  19. /**
  20.  * The <code>Dimension</code> class encapsulates the width and 
  21.  * height of a component (in integer precision) in a single object. 
  22.  * The class is 
  23.  * associated with certain properties of components. Several methods 
  24.  * defined by the <code>Component</code> class and the 
  25.  * <code>LayoutManager</code> interface return a 
  26.  * <code>Dimension</code> object. 
  27.  * <p>
  28.  * Normally the values of <code>width</code> 
  29.  * and <code>height</code> are non-negative integers. 
  30.  * The constructors that allow you to create a dimension do 
  31.  * not prevent you from setting a negative value for these properties. 
  32.  * If the value of <code>width</code> or <code>height</code> is 
  33.  * negative, the behavior of some methods defined by other objects is 
  34.  * undefined. 
  35.  * 
  36.  * @version     1.16, 03/18/98
  37.  * @author     Sami Shaio
  38.  * @author     Arthur van Hoff
  39.  * @see         java.awt.Component
  40.  * @see         java.awt.LayoutManager
  41.  * @since       JDK1.0
  42.  */
  43. public class Dimension extends Dimension2D implements java.io.Serializable {
  44.     
  45.     /**
  46.      * The width dimension.
  47.      */
  48.     public int width;
  49.  
  50.     /**
  51.      * The height dimension.
  52.      */
  53.     public int height;
  54.  
  55.     /*
  56.      * JDK 1.1 serialVersionUID 
  57.      */
  58.      private static final long serialVersionUID = 4723952579491349524L;
  59.  
  60.     /** 
  61.      * Creates an instance of <code>Dimension</code> with a width 
  62.      * of zero and a height of zero. 
  63.      */
  64.     public Dimension() {
  65.     this(0, 0);
  66.     }
  67.  
  68.     /** 
  69.      * Creates an instance of <code>Dimension</code> whose width  
  70.      * and height are the same as for the specified dimension. 
  71.      * @param    d   the specified dimension for the 
  72.      *               <code>width</code> and 
  73.      *               <code>height</code> values.
  74.      */
  75.     public Dimension(Dimension d) {
  76.     this(d.width, d.height);
  77.     }
  78.  
  79.     /** 
  80.      * Constructs a Dimension and initializes it to the specified width and
  81.      * specified height.
  82.      * @param width the specified width dimension
  83.      * @param height the specified height dimension
  84.      */
  85.     public Dimension(int width, int height) {
  86.     this.width = width;
  87.     this.height = height;
  88.     }
  89.  
  90.     /**
  91.      * Returns the width of this dimension in double precision.
  92.      */
  93.     public double getWidth() {
  94.     return width;
  95.     }
  96.  
  97.     /**
  98.      * Returns the height of this dimension in double precision.
  99.      */
  100.     public double getHeight() {
  101.     return height;
  102.     }
  103.  
  104.     /**
  105.      * Set the size of this Dimension object to the specified width
  106.      * and height in double precision.
  107.      * @param width  the new width for the Dimension object
  108.      * @param height  the new height for the Dimension object
  109.      */
  110.     public void setSize(double width, double height) {
  111.     width = (int) Math.ceil(width);
  112.     height = (int) Math.ceil(height);
  113.     }
  114.  
  115.     /**
  116.      * Gets the size of this <code>Dimension</code> object.
  117.      * This method is included for completeness, to parallel the
  118.      * <code>getSize</code> method defined by <code>Component</code>.
  119.      * @return   the size of this dimension, a new instance of 
  120.      *           <code>Dimension</code> with the same width and height.
  121.      * @see      java.awt.Dimension#setSize
  122.      * @see      java.awt.Component#getSize
  123.      * @since    JDK1.1
  124.      */
  125.     public Dimension getSize() {
  126.     return new Dimension(width, height);
  127.     }    
  128.  
  129.     /**
  130.      * Set the size of this <code>Dimension</code> object to the specified size.
  131.      * This method is included for completeness, to parallel the
  132.      * <code>setSize</code> method defined by <code>Component</code>.
  133.      * @param    d  the new size for this <code>Dimension</code> object.
  134.      * @see      java.awt.Dimension#getSize
  135.      * @see      java.awt.Component#setSize
  136.      * @since    JDK1.1
  137.      */
  138.     public void setSize(Dimension d) {
  139.     setSize(d.width, d.height);
  140.     }    
  141.  
  142.     /**
  143.      * Set the size of this <code>Dimension</code> object 
  144.      * to the specified width and height.
  145.      * This method is included for completeness, to parallel the
  146.      * <code>setSize</code> method defined by <code>Component</code>.
  147.      * @param    width   the new width for this <code>Dimension</code> object.
  148.      * @param    height  the new height for this <code>Dimension</code> object.
  149.      * @see      java.awt.Dimension#getSize
  150.      * @see      java.awt.Component#setSize
  151.      * @since    JDK1.1
  152.      */
  153.     public void setSize(int width, int height) {
  154.         this.width = width;
  155.         this.height = height;
  156.     }    
  157.  
  158.     /**
  159.      * Checks whether two dimension objects have equal values.
  160.      */
  161.     public boolean equals(Object obj) {
  162.     if (obj instanceof Dimension) {
  163.         Dimension d = (Dimension)obj;
  164.         return (width == d.width) && (height == d.height);
  165.     }
  166.     return false;
  167.     }
  168.  
  169.     /**
  170.      * Returns a string that represents this 
  171.      * <code>Dimension</code> object's values.
  172.      * @return     a string representation of this dimension, 
  173.      *                  including the values of <code>width</code> 
  174.      *                  and <code>height</code>.
  175.      */
  176.     public String toString() {
  177.     return getClass().getName() + "[width=" + width + ",height=" + height + "]";
  178.     }
  179. }
  180.