001 /*
002 * Copyright 1995-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.awt;
027
028 import java.awt.geom.Dimension2D;
029
030 /**
031 * The <code>Dimension</code> class encapsulates the width and
032 * height of a component (in integer precision) in a single object.
033 * The class is
034 * associated with certain properties of components. Several methods
035 * defined by the <code>Component</code> class and the
036 * <code>LayoutManager</code> interface return a
037 * <code>Dimension</code> object.
038 * <p>
039 * Normally the values of <code>width</code>
040 * and <code>height</code> are non-negative integers.
041 * The constructors that allow you to create a dimension do
042 * not prevent you from setting a negative value for these properties.
043 * If the value of <code>width</code> or <code>height</code> is
044 * negative, the behavior of some methods defined by other objects is
045 * undefined.
046 *
047 * @version 1.40, 05/05/07
048 * @author Sami Shaio
049 * @author Arthur van Hoff
050 * @see java.awt.Component
051 * @see java.awt.LayoutManager
052 * @since 1.0
053 */
054 public class Dimension extends Dimension2D implements
055 java.io.Serializable {
056
057 /**
058 * The width dimension; negative values can be used.
059 *
060 * @serial
061 * @see #getSize
062 * @see #setSize
063 * @since 1.0
064 */
065 public int width;
066
067 /**
068 * The height dimension; negative values can be used.
069 *
070 * @serial
071 * @see #getSize
072 * @see #setSize
073 * @since 1.0
074 */
075 public int height;
076
077 /*
078 * JDK 1.1 serialVersionUID
079 */
080 private static final long serialVersionUID = 4723952579491349524L;
081
082 /**
083 * Initialize JNI field and method IDs
084 */
085 private static native void initIDs();
086
087 static {
088 /* ensure that the necessary native libraries are loaded */
089 Toolkit.loadLibraries();
090 if (!GraphicsEnvironment.isHeadless()) {
091 initIDs();
092 }
093 }
094
095 /**
096 * Creates an instance of <code>Dimension</code> with a width
097 * of zero and a height of zero.
098 */
099 public Dimension() {
100 this (0, 0);
101 }
102
103 /**
104 * Creates an instance of <code>Dimension</code> whose width
105 * and height are the same as for the specified dimension.
106 *
107 * @param d the specified dimension for the
108 * <code>width</code> and
109 * <code>height</code> values
110 */
111 public Dimension(Dimension d) {
112 this (d.width, d.height);
113 }
114
115 /**
116 * Constructs a <code>Dimension</code> and initializes
117 * it to the specified width and specified height.
118 *
119 * @param width the specified width
120 * @param height the specified height
121 */
122 public Dimension(int width, int height) {
123 this .width = width;
124 this .height = height;
125 }
126
127 /**
128 * {@inheritDoc}
129 * @since 1.2
130 */
131 public double getWidth() {
132 return width;
133 }
134
135 /**
136 * {@inheritDoc}
137 * @since 1.2
138 */
139 public double getHeight() {
140 return height;
141 }
142
143 /**
144 * Sets the size of this <code>Dimension</code> object to
145 * the specified width and height in double precision.
146 * Note that if <code>width</code> or <code>height</code>
147 * are larger than <code>Integer.MAX_VALUE</code>, they will
148 * be reset to <code>Integer.MAX_VALUE</code>.
149 *
150 * @param width the new width for the <code>Dimension</code> object
151 * @param height the new height for the <code>Dimension</code> object
152 * @since 1.2
153 */
154 public void setSize(double width, double height) {
155 this .width = (int) Math.ceil(width);
156 this .height = (int) Math.ceil(height);
157 }
158
159 /**
160 * Gets the size of this <code>Dimension</code> object.
161 * This method is included for completeness, to parallel the
162 * <code>getSize</code> method defined by <code>Component</code>.
163 *
164 * @return the size of this dimension, a new instance of
165 * <code>Dimension</code> with the same width and height
166 * @see java.awt.Dimension#setSize
167 * @see java.awt.Component#getSize
168 * @since 1.1
169 */
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="
238 + height + "]";
239 }
240 }
|