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