KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > Dimension


1 /*
2  * @(#)Dimension.java 1.32 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.awt;
9
10 import java.awt.geom.Dimension2D JavaDoc;
11
12 /**
13  * The <code>Dimension</code> class encapsulates the width and
14  * height of a component (in integer precision) in a single object.
15  * The class is
16  * associated with certain properties of components. Several methods
17  * defined by the <code>Component</code> class and the
18  * <code>LayoutManager</code> interface return a
19  * <code>Dimension</code> object.
20  * <p>
21  * Normally the values of <code>width</code>
22  * and <code>height</code> are non-negative integers.
23  * The constructors that allow you to create a dimension do
24  * not prevent you from setting a negative value for these properties.
25  * If the value of <code>width</code> or <code>height</code> is
26  * negative, the behavior of some methods defined by other objects is
27  * undefined.
28  *
29  * @version 1.32, 12/19/03
30  * @author Sami Shaio
31  * @author Arthur van Hoff
32  * @see java.awt.Component
33  * @see java.awt.LayoutManager
34  * @since JDK1.0
35  */

36 public class Dimension extends Dimension2D JavaDoc implements java.io.Serializable JavaDoc {
37     
38     /**
39      * The width dimension; negative values can be used.
40      *
41      * @serial
42      * @see #getSize
43      * @see #setSize
44      */

45     public int width;
46
47     /**
48      * The height dimension; negative values can be used.
49      *
50      * @serial
51      * @see #getSize
52      * @see #setSize
53      */

54     public int height;
55
56     /*
57      * JDK 1.1 serialVersionUID
58      */

59      private static final long serialVersionUID = 4723952579491349524L;
60
61     /**
62      * Initialize JNI field and method IDs
63      */

64     private static native void initIDs();
65
66     static {
67         /* ensure that the necessary native libraries are loaded */
68     Toolkit.loadLibraries();
69         if (!GraphicsEnvironment.isHeadless()) {
70             initIDs();
71         }
72     }
73
74     /**
75      * Creates an instance of <code>Dimension</code> with a width
76      * of zero and a height of zero.
77      */

78     public Dimension() {
79     this(0, 0);
80     }
81
82     /**
83      * Creates an instance of <code>Dimension</code> whose width
84      * and height are the same as for the specified dimension.
85      *
86      * @param d the specified dimension for the
87      * <code>width</code> and
88      * <code>height</code> values
89      */

90     public Dimension(Dimension JavaDoc d) {
91     this(d.width, d.height);
92     }
93
94     /**
95      * Constructs a <code>Dimension</code> and initializes
96      * it to the specified width and specified height.
97      *
98      * @param width the specified width
99      * @param height the specified height
100      */

101     public Dimension(int width, int height) {
102     this.width = width;
103     this.height = height;
104     }
105
106     /**
107      * Returns the width of this dimension in double precision.
108      * @return the width of this dimension in double precision
109      */

110     public double getWidth() {
111     return width;
112     }
113
114     /**
115      * Returns the height of this dimension in double precision.
116      * @return the height of this dimension in double precision
117      */

118     public double getHeight() {
119     return height;
120     }
121
122     /**
123      * Sets the size of this <code>Dimension</code> object to
124      * the specified width and height in double precision.
125      * Note that if <code>width</code> or <code>height</code>
126      * are larger than <code>Integer.MAX_VALUE</code>, they will
127      * be reset to <code>Integer.MAX_VALUE</code>.
128      *
129      * @param width the new width for the <code>Dimension</code> object
130      * @param height the new height for the <code>Dimension</code> object
131      */

132     public void setSize(double width, double height) {
133     this.width = (int) Math.ceil(width);
134     this.height = (int) Math.ceil(height);
135     }
136
137     /**
138      * Gets the size of this <code>Dimension</code> object.
139      * This method is included for completeness, to parallel the
140      * <code>getSize</code> method defined by <code>Component</code>.
141      *
142      * @return the size of this dimension, a new instance of
143      * <code>Dimension</code> with the same width and height
144      * @see java.awt.Dimension#setSize
145      * @see java.awt.Component#getSize
146      * @since JDK1.1
147      */

148     public Dimension JavaDoc getSize() {
149     return new Dimension JavaDoc(width, height);
150     }
151
152     /**
153      * Sets the size of this <code>Dimension</code> object to the specified size.
154      * This method is included for completeness, to parallel the
155      * <code>setSize</code> method defined by <code>Component</code>.
156      * @param d the new size for this <code>Dimension</code> object
157      * @see java.awt.Dimension#getSize
158      * @see java.awt.Component#setSize
159      * @since JDK1.1
160      */

161     public void setSize(Dimension JavaDoc d) {
162     setSize(d.width, d.height);
163     }
164
165     /**
166      * Sets the size of this <code>Dimension</code> object
167      * to the specified width and height.
168      * This method is included for completeness, to parallel the
169      * <code>setSize</code> method defined by <code>Component</code>.
170      *
171      * @param width the new width for this <code>Dimension</code> object
172      * @param height the new height for this <code>Dimension</code> object
173      * @see java.awt.Dimension#getSize
174      * @see java.awt.Component#setSize
175      * @since JDK1.1
176      */

177     public void setSize(int width, int height) {
178         this.width = width;
179         this.height = height;
180     }
181
182     /**
183      * Checks whether two dimension objects have equal values.
184      */

185     public boolean equals(Object JavaDoc obj) {
186     if (obj instanceof Dimension JavaDoc) {
187         Dimension JavaDoc d = (Dimension JavaDoc)obj;
188         return (width == d.width) && (height == d.height);
189     }
190     return false;
191     }
192
193     /**
194      * Returns the hash code for this <code>Dimension</code>.
195      *
196      * @return a hash code for this <code>Dimension</code>
197      */

198     public int hashCode() {
199         int sum = width + height;
200         return sum * (sum + 1)/2 + width;
201     }
202
203     /**
204      * Returns a string representation of the values of this
205      * <code>Dimension</code> object's <code>height</code> and
206      * <code>width</code> fields. This method is intended to be used only
207      * for debugging purposes, and the content and format of the returned
208      * string may vary between implementations. The returned string may be
209      * empty but may not be <code>null</code>.
210      *
211      * @return a string representation of this <code>Dimension</code>
212      * object
213      */

214     public String JavaDoc toString() {
215     return getClass().getName() + "[width=" + width + ",height=" + height + "]";
216     }
217 }
218
Popular Tags