001/* Dimension.java -- represents a 2-dimensional span
002   Copyright (C) 1999, 2000, 2002 Free Software Foundation
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038
039package java.awt;
040
041import java.awt.geom.Dimension2D;
042import java.io.Serializable;
043
044/**
045 * This class holds a width and height value pair. This is used in plenty
046 * of windowing classes, but also has geometric meaning.
047 *
048 * <p>It is valid for a dimension to have negative width or height; but it
049 * is considered to have no area. Therefore, the behavior in various methods
050 * is undefined in such a case.
051 *
052 * <p>There are some public fields; if you mess with them in an inconsistent
053 * manner, it is your own fault when you get invalid results. Also, this
054 * class is not threadsafe.
055 *
056 * @author Per Bothner (bothner@cygnus.com)
057 * @author Aaron M. Renn (arenn@urbanophile.com)
058 * @author Eric Blake (ebb9@email.byu.edu)
059 * @see Component
060 * @see LayoutManager
061 * @since 1.0
062 * @status updated to 1.14
063 */
064public class Dimension extends Dimension2D implements Serializable
065{
066  /**
067   * Compatible with JDK 1.0+.
068   */
069  private static final long serialVersionUID = 4723952579491349524L;
070
071  /**
072   * The width of this object.
073   *
074   * @see #getSize()
075   * @see #setSize(double, double)
076   * @serial the width
077   */
078  public int width;
079
080  /**
081   * The height of this object.
082   *
083   * @see #getSize()
084   * @see #setSize(double, double)
085   * @serial the height
086   */
087  public int height;
088
089  /**
090   * Create a new Dimension with a width and height of zero.
091   */
092  public Dimension()
093  {
094  }
095
096  /**
097   * Create a new Dimension with width and height identical to that of the
098   * specified dimension.
099   *
100   * @param d the Dimension to copy
101   * @throws NullPointerException if d is null
102   */
103  public Dimension(Dimension d)
104  {
105    width = d.width;
106    height = d.height;
107  }
108
109  /**
110   * Create a new Dimension with the specified width and height.
111   *
112   * @param w the width of this object
113   * @param h the height of this object
114   */
115  public Dimension(int w, int h)
116  {
117    width = w;
118    height = h;
119  }
120
121  /**
122   * Gets the width of this dimension.
123   *
124   * @return the width, as a double
125   */
126  public double getWidth()
127  {
128    return width;
129  }
130
131  /**
132   * Gets the height of this dimension.
133   *
134   * @return the height, as a double
135   */
136  public double getHeight()
137  {
138    return height;
139  }
140
141  /**
142   * Sets the size of this dimension. The values are rounded to int.
143   *
144   * @param w the new width
145   * @param h the new height
146   * @since 1.2
147   */
148  public void setSize(double w, double h)
149  {
150    width = (int) w;
151    height = (int) h;
152  }
153
154  /**
155   * Returns the size of this dimension. A pretty useless method, as this is
156   * already a dimension.
157   *
158   * @return a copy of this dimension
159   * @see #setSize(Dimension)
160   * @since 1.1
161   */
162  public Dimension getSize()
163  {
164    return new Dimension(width, height);
165  }
166
167  /**
168   * Sets the width and height of this object to match that of the
169   * specified object.
170   *
171   * @param d the Dimension to get the new width and height from
172   * @throws NullPointerException if d is null
173   * @see #getSize()
174   * @since 1.1
175   */
176  public void setSize(Dimension d)
177  {
178    width = d.width;
179    height = d.height;
180  }
181
182  /**
183   * Sets the width and height of this object to the specified values.
184   *
185   * @param w the new width value
186   * @param h the new height value
187   */
188  public void setSize(int w, int h)
189  {
190    width = w;
191    height = h;
192  }
193
194  /**
195   * Tests this object for equality against the specified object.  This will
196   * be true if and only if the specified object is an instance of
197   * Dimension2D, and has the same width and height.
198   *
199   * @param obj the object to test against
200   * @return true if the object is equal to this
201   */
202  public boolean equals(Object obj)
203  {
204    if (! (obj instanceof Dimension))
205      return false;
206    Dimension dim = (Dimension) obj;
207    return height == dim.height && width == dim.width;
208  }
209
210  /**
211   * Return the hashcode for this object. It is not documented, but appears
212   * to be <code>((width + height) * (width + height + 1) / 2) + width</code>.
213   *
214   * @return the hashcode
215   */
216  public int hashCode()
217  {
218    // Reverse engineering this was fun!
219    return (width + height) * (width + height + 1) / 2 + width;
220  }
221
222  /**
223   * Returns a string representation of this object. The format is:
224   * <code>getClass().getName() + "[width=" + width + ",height=" + height
225   * + ']'</code>.
226   *
227   * @return a string representation of this object
228   */
229  public String toString()
230  {
231    return getClass().getName()
232      + "[width=" + width + ",height=" + height + ']';
233  }
234} // class Dimension