home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / awt / Dimension.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  6.2 KB  |  203 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)Dimension.java    1.21 98/09/21
  3.  *
  4.  * Copyright 1995-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;
  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.21, 09/21/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. Negative values can be used. 
  47.      *
  48.      * @serial
  49.      * @see getSize()
  50.      * @see setSize()
  51.      */
  52.     public int width;
  53.  
  54.     /**
  55.      * The height dimension. Negative values can be used. 
  56.      *
  57.      * @serial
  58.      * @see getSize()
  59.      * @see setSize()
  60.      */
  61.     public int height;
  62.  
  63.     /*
  64.      * JDK 1.1 serialVersionUID 
  65.      */
  66.      private static final long serialVersionUID = 4723952579491349524L;
  67.  
  68.     /**
  69.      * Initialize JNI field and method IDs
  70.      */
  71.     private static native void initIDs();
  72.  
  73.     static {
  74.         /* ensure that the necessary native libraries are loaded */
  75.     Toolkit.loadLibraries();
  76.         initIDs();
  77.     }
  78.  
  79.     /** 
  80.      * Creates an instance of <code>Dimension</code> with a width 
  81.      * of zero and a height of zero. 
  82.      */
  83.     public Dimension() {
  84.     this(0, 0);
  85.     }
  86.  
  87.     /** 
  88.      * Creates an instance of <code>Dimension</code> whose width  
  89.      * and height are the same as for the specified dimension. 
  90.      * @param    d   the specified dimension for the 
  91.      *               <code>width</code> and 
  92.      *               <code>height</code> values.
  93.      */
  94.     public Dimension(Dimension d) {
  95.     this(d.width, d.height);
  96.     }
  97.  
  98.     /** 
  99.      * Constructs a Dimension and initializes it to the specified width and
  100.      * specified height.
  101.      * @param width the specified width dimension
  102.      * @param height the specified height dimension
  103.      */
  104.     public Dimension(int width, int height) {
  105.     this.width = width;
  106.     this.height = height;
  107.     }
  108.  
  109.     /**
  110.      * Returns the width of this dimension in double precision.
  111.      */
  112.     public double getWidth() {
  113.     return width;
  114.     }
  115.  
  116.     /**
  117.      * Returns the height of this dimension in double precision.
  118.      */
  119.     public double getHeight() {
  120.     return height;
  121.     }
  122.  
  123.     /**
  124.      * Set the size of this Dimension object to the specified width
  125.      * and height in double precision.
  126.      * @param width  the new width for the Dimension object
  127.      * @param height  the new height for the Dimension object
  128.      */
  129.     public void setSize(double width, double height) {
  130.     width = (int) Math.ceil(width);
  131.     height = (int) Math.ceil(height);
  132.     }
  133.  
  134.     /**
  135.      * Gets the size of this <code>Dimension</code> object.
  136.      * This method is included for completeness, to parallel the
  137.      * <code>getSize</code> method defined by <code>Component</code>.
  138.      * @return   the size of this dimension, a new instance of 
  139.      *           <code>Dimension</code> with the same width and height.
  140.      * @see      java.awt.Dimension#setSize
  141.      * @see      java.awt.Component#getSize
  142.      * @since    JDK1.1
  143.      */
  144.     public Dimension getSize() {
  145.     return new Dimension(width, height);
  146.     }    
  147.  
  148.     /**
  149.      * Set the size of this <code>Dimension</code> object to the specified size.
  150.      * This method is included for completeness, to parallel the
  151.      * <code>setSize</code> method defined by <code>Component</code>.
  152.      * @param    d  the new size for this <code>Dimension</code> object.
  153.      * @see      java.awt.Dimension#getSize
  154.      * @see      java.awt.Component#setSize
  155.      * @since    JDK1.1
  156.      */
  157.     public void setSize(Dimension d) {
  158.     setSize(d.width, d.height);
  159.     }    
  160.  
  161.     /**
  162.      * Set the size of this <code>Dimension</code> object 
  163.      * to the specified width and height.
  164.      * This method is included for completeness, to parallel the
  165.      * <code>setSize</code> method defined by <code>Component</code>.
  166.      * @param    width   the new width for this <code>Dimension</code> object.
  167.      * @param    height  the new height for this <code>Dimension</code> object.
  168.      * @see      java.awt.Dimension#getSize
  169.      * @see      java.awt.Component#setSize
  170.      * @since    JDK1.1
  171.      */
  172.     public void setSize(int width, int height) {
  173.         this.width = width;
  174.         this.height = height;
  175.     }    
  176.  
  177.     /**
  178.      * Checks whether two dimension objects have equal values.
  179.      */
  180.     public boolean equals(Object obj) {
  181.     if (obj instanceof Dimension) {
  182.         Dimension d = (Dimension)obj;
  183.         return (width == d.width) && (height == d.height);
  184.     }
  185.     return false;
  186.     }
  187.  
  188.     /**
  189.      * Returns a string representation of the values of this 
  190.      * <code>Dimension</code> object's <code>height</code> and 
  191.      * <code>width</code> fields. This method is intended to be used only 
  192.      * for debugging purposes, and the content and format of the returned 
  193.      * string may vary between implementations. The returned string may be 
  194.      * empty but may not be <code>null</code>.
  195.      * 
  196.      * @return  a string representation of this <code>Dimension</code> 
  197.      *          object.
  198.      */
  199.     public String toString() {
  200.     return getClass().getName() + "[width=" + width + ",height=" + height + "]";
  201.     }
  202. }
  203.