1 /*
   2  * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.awt;
  27 
  28 import java.awt.geom.Dimension2D;
  29 import java.beans.Transient;
  30 
  31 /**
  32  * The <code>Dimension</code> class encapsulates the width and
  33  * height of a component (in integer precision) in a single object.
  34  * The class is
  35  * associated with certain properties of components. Several methods
  36  * defined by the <code>Component</code> class and the
  37  * <code>LayoutManager</code> interface return a
  38  * <code>Dimension</code> object.
  39  * <p>
  40  * Normally the values of <code>width</code>
  41  * and <code>height</code> are non-negative integers.
  42  * The constructors that allow you to create a dimension do
  43  * not prevent you from setting a negative value for these properties.
  44  * If the value of <code>width</code> or <code>height</code> is
  45  * negative, the behavior of some methods defined by other objects is
  46  * undefined.
  47  *
  48  * @author      Sami Shaio
  49  * @author      Arthur van Hoff
  50  * @see         java.awt.Component
  51  * @see         java.awt.LayoutManager
  52  * @since       1.0
  53  */
  54 public class Dimension extends Dimension2D implements java.io.Serializable {
  55 
  56     /**
  57      * The width dimension; negative values can be used.
  58      *
  59      * @serial
  60      * @see #getSize
  61      * @see #setSize
  62      * @since 1.0
  63      */
  64     public int width;
  65 
  66     /**
  67      * The height dimension; negative values can be used.
  68      *
  69      * @serial
  70      * @see #getSize
  71      * @see #setSize
  72      * @since 1.0
  73      */
  74     public int height;
  75 
  76     /*
  77      * JDK 1.1 serialVersionUID
  78      */
  79      private static final long serialVersionUID = 4723952579491349524L;
  80 
  81     /**
  82      * Initialize JNI field and method IDs
  83      */
  84     private static native void initIDs();
  85 
  86     static {
  87         /* ensure that the necessary native libraries are loaded */
  88         Toolkit.loadLibraries();
  89         if (!GraphicsEnvironment.isHeadless()) {
  90             initIDs();
  91         }
  92     }
  93 
  94     /**
  95      * Creates an instance of <code>Dimension</code> with a width
  96      * of zero and a height of zero.
  97      */
  98     public Dimension() {
  99         this(0, 0);
 100     }
 101 
 102     /**
 103      * Creates an instance of <code>Dimension</code> whose width
 104      * and height are the same as for the specified dimension.
 105      *
 106      * @param    d   the specified dimension for the
 107      *               <code>width</code> and
 108      *               <code>height</code> values
 109      */
 110     public Dimension(Dimension d) {
 111         this(d.width, d.height);
 112     }
 113 
 114     /**
 115      * Constructs a <code>Dimension</code> and initializes
 116      * it to the specified width and specified height.
 117      *
 118      * @param width the specified width
 119      * @param height the specified height
 120      */
 121     public Dimension(int width, int height) {
 122         this.width = width;
 123         this.height = height;
 124     }
 125 
 126     /**
 127      * {@inheritDoc}
 128      * @since 1.2
 129      */
 130     public double getWidth() {
 131         return width;
 132     }
 133 
 134     /**
 135      * {@inheritDoc}
 136      * @since 1.2
 137      */
 138     public double getHeight() {
 139         return height;
 140     }
 141 
 142     /**
 143      * Sets the size of this <code>Dimension</code> object to
 144      * the specified width and height in double precision.
 145      * Note that if <code>width</code> or <code>height</code>
 146      * are larger than <code>Integer.MAX_VALUE</code>, they will
 147      * be reset to <code>Integer.MAX_VALUE</code>.
 148      *
 149      * @param width  the new width for the <code>Dimension</code> object
 150      * @param height the new height for the <code>Dimension</code> object
 151      * @since 1.2
 152      */
 153     public void setSize(double width, double height) {
 154         this.width = (int) Math.ceil(width);
 155         this.height = (int) Math.ceil(height);
 156     }
 157 
 158     /**
 159      * Gets the size of this <code>Dimension</code> object.
 160      * This method is included for completeness, to parallel the
 161      * <code>getSize</code> method defined by <code>Component</code>.
 162      *
 163      * @return   the size of this dimension, a new instance of
 164      *           <code>Dimension</code> with the same width and height
 165      * @see      java.awt.Dimension#setSize
 166      * @see      java.awt.Component#getSize
 167      * @since    1.1
 168      */
 169     @Transient
 170     public Dimension getSize() {
 171         return new Dimension(width, height);
 172     }
 173 
 174     /**
 175      * Sets the size of this <code>Dimension</code> object to the specified size.
 176      * This method is included for completeness, to parallel the
 177      * <code>setSize</code> method defined by <code>Component</code>.
 178      * @param    d  the new size for this <code>Dimension</code> object
 179      * @see      java.awt.Dimension#getSize
 180      * @see      java.awt.Component#setSize
 181      * @since    1.1
 182      */
 183     public void setSize(Dimension d) {
 184         setSize(d.width, d.height);
 185     }
 186 
 187     /**
 188      * Sets the size of this <code>Dimension</code> object
 189      * to the specified width and height.
 190      * This method is included for completeness, to parallel the
 191      * <code>setSize</code> method defined by <code>Component</code>.
 192      *
 193      * @param    width   the new width for this <code>Dimension</code> object
 194      * @param    height  the new height for this <code>Dimension</code> object
 195      * @see      java.awt.Dimension#getSize
 196      * @see      java.awt.Component#setSize
 197      * @since    1.1
 198      */
 199     public void setSize(int width, int height) {
 200         this.width = width;
 201         this.height = height;
 202     }
 203 
 204     /**
 205      * Checks whether two dimension objects have equal values.
 206      */
 207     public boolean equals(Object obj) {
 208         if (obj instanceof Dimension) {
 209             Dimension d = (Dimension)obj;
 210             return (width == d.width) && (height == d.height);
 211         }
 212         return false;
 213     }
 214 
 215     /**
 216      * Returns the hash code for this <code>Dimension</code>.
 217      *
 218      * @return    a hash code for this <code>Dimension</code>
 219      */
 220     public int hashCode() {
 221         int sum = width + height;
 222         return sum * (sum + 1)/2 + width;
 223     }
 224 
 225     /**
 226      * Returns a string representation of the values of this
 227      * <code>Dimension</code> object's <code>height</code> and
 228      * <code>width</code> fields. This method is intended to be used only
 229      * for debugging purposes, and the content and format of the returned
 230      * string may vary between implementations. The returned string may be
 231      * empty but may not be <code>null</code>.
 232      *
 233      * @return  a string representation of this <code>Dimension</code>
 234      *          object
 235      */
 236     public String toString() {
 237         return getClass().getName() + "[width=" + width + ",height=" + height + "]";
 238     }
 239 }
--- EOF ---