1 |
/* Copyright (C) 1999, 2000, 2002 Free Software Foundation |
/* Dimension.java -- represents a 2-dimensional span |
2 |
|
Copyright (C) 1999, 2000, 2002 Free Software Foundation |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
38 |
|
|
39 |
package java.awt; |
package java.awt; |
40 |
|
|
41 |
/* Written using "Java Class Libraries", 2nd edition, plus online |
import java.awt.geom.Dimension2D; |
42 |
* API docs for JDK 1.2 beta from http://www.javasoft.com. |
import java.io.Serializable; |
|
* Status: Believed complete and correct, except that neither toString |
|
|
* has not been compared with JDK output. |
|
|
*/ |
|
43 |
|
|
44 |
/** |
/** |
45 |
* This class holds a width and height value pair. |
* This class holds a width and height value pair. This is used in plenty |
46 |
* |
* of windowing classes, but also has geometric meaning. |
47 |
* @author Per Bothner <bothner@cygnus.com> |
* |
48 |
* @author Aaron M. Renn (arenn@urbanophile.com) |
* <p>It is valid for a dimension to have negative width or height; but it |
49 |
* @date Fenruary 8, 1999. |
* is considered to have no area. Therefore, the behavior in various methods |
50 |
*/ |
* is undefined in such a case. |
51 |
public class Dimension extends java.awt.geom.Dimension2D |
* |
52 |
implements java.io.Serializable |
* <p>There are some public fields; if you mess with them in an inconsistent |
53 |
|
* manner, it is your own fault when you get invalid results. Also, this |
54 |
|
* class is not threadsafe. |
55 |
|
* |
56 |
|
* @author Per Bothner <bothner@cygnus.com> |
57 |
|
* @author Aaron M. Renn <arenn@urbanophile.com> |
58 |
|
* @author Eric Blake <ebb9@email.byu.edu> |
59 |
|
* @see Component |
60 |
|
* @see LayoutManager |
61 |
|
* @since 1.0 |
62 |
|
* @status updated to 1.14 |
63 |
|
*/ |
64 |
|
public class Dimension extends Dimension2D implements Serializable |
65 |
{ |
{ |
66 |
/** |
/** |
67 |
* This width of this object. |
* Compatible with JDK 1.0+. |
68 |
|
*/ |
69 |
|
private static final long serialVersionUID = 4723952579491349524L; |
70 |
|
|
71 |
|
/** |
72 |
|
* The width of this object. |
73 |
|
* |
74 |
|
* @see #getSize() |
75 |
|
* @see #setSize(double, double) |
76 |
|
* @serial the width |
77 |
*/ |
*/ |
78 |
public int width; |
public int width; |
79 |
|
|
80 |
/** |
/** |
81 |
* The height of this object. |
* The height of this object. |
82 |
|
* |
83 |
|
* @see #getSize() |
84 |
|
* @see #setSize(double, double) |
85 |
|
* @serial the height |
86 |
*/ |
*/ |
87 |
public int height; |
public int height; |
88 |
|
|
89 |
/** |
/** |
90 |
* Initializes a new instance of <code>Dimension</code> with a width |
* Create a new Dimension with a width and height of zero. |
|
* and height of zero. |
|
91 |
*/ |
*/ |
92 |
public Dimension () { } |
public Dimension() |
93 |
|
{ |
94 |
|
} |
95 |
|
|
96 |
/** |
/** |
97 |
* Initializes a new instance of <code>Dimension</code> to have a width |
* Create a new Dimension with width and height identical to that of the |
98 |
* and height identical to that of the specified dimension object. |
* specified dimension. |
99 |
* |
* |
100 |
* @param dim The <code>Dimension</code> to take the width and height from. |
* @param d the Dimension to copy |
101 |
|
* @throws NullPointerException if d is null |
102 |
*/ |
*/ |
103 |
public Dimension (Dimension dim) |
public Dimension(Dimension d) |
104 |
{ |
{ |
105 |
this.width = dim.width; |
width = d.width; |
106 |
this.height = dim.height; |
height = d.height; |
107 |
} |
} |
108 |
|
|
109 |
/** |
/** |
110 |
* Initializes a new instance of <code>Dimension</code> with the |
* Create a new Dimension with the specified width and height. |
|
* specified width and height. |
|
111 |
* |
* |
112 |
* @param width The width of this object. |
* @param w the width of this object |
113 |
* @param height The height of this object. |
* @param h the height of this object |
114 |
*/ |
*/ |
115 |
public Dimension (int width, int height) |
public Dimension(int w, int h) |
116 |
{ |
{ |
117 |
this.width = width; |
width = w; |
118 |
this.height = height; |
height = h; |
119 |
} |
} |
120 |
|
|
121 |
/** |
/** |
122 |
* Tests this object for equality against the specified object. This will |
* Gets the width of this dimension. |
|
* be true if and only if the specified object: |
|
|
* <p> |
|
|
* <ul> |
|
|
* <li>Is not <code>null</code>. |
|
|
* <li>Is an instance of <code>Dimension</code>. |
|
|
* <li>Has width and height values identical to this object. |
|
|
* </ul> |
|
123 |
* |
* |
124 |
* @param obj The object to test against. |
* @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 <code>true</code> if the specified object is equal to this |
* @return the height, as a double |
|
* object, <code>false</code> otherwise. |
|
135 |
*/ |
*/ |
136 |
public boolean equals (Object obj) |
public double getHeight() |
137 |
{ |
{ |
138 |
if (! (obj instanceof Dimension)) |
return height; |
|
return false; |
|
|
Dimension dim = (Dimension) obj; |
|
|
return height == dim.height && width == dim.width; |
|
139 |
} |
} |
140 |
|
|
141 |
/** |
/** |
142 |
* Returns the size of this object. Not very useful. |
* Sets the size of this dimension. The values are rounded to int. |
143 |
* |
* |
144 |
* @return This object. |
* @param w the new width |
145 |
|
* @param h the new height |
146 |
|
* @since 1.2 |
147 |
*/ |
*/ |
148 |
public Dimension getSize () { return new Dimension(this); } |
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 |
* Sets the width and height of this object to match that of the |
169 |
* specified object. |
* specified object. |
170 |
* |
* |
171 |
* @param dim The <code>Dimension</code> object to get the new width and |
* @param d the Dimension to get the new width and height from |
172 |
* height from. |
* @throws NullPointerException if d is null |
173 |
|
* @see #getSize() |
174 |
|
* @since 1.1 |
175 |
*/ |
*/ |
176 |
public void setSize (Dimension dim) |
public void setSize(Dimension d) |
177 |
{ |
{ |
178 |
this.width = dim.width; |
width = d.width; |
179 |
this.height = dim.height; |
height = d.height; |
180 |
} |
} |
181 |
|
|
182 |
/** |
/** |
183 |
* Sets the width and height of this object to the specified values. |
* Sets the width and height of this object to the specified values. |
184 |
* |
* |
185 |
* @param width The new width value. |
* @param w the new width value |
186 |
* @param height The new height value. |
* @param h the new height value |
187 |
*/ |
*/ |
188 |
public void setSize (int width, int height) |
public void setSize(int w, int h) |
189 |
{ |
{ |
190 |
this.width = width; |
width = w; |
191 |
this.height = height; |
height = h; |
192 |
} |
} |
193 |
|
|
194 |
/** |
/** |
195 |
* Returns a string representation of this object. |
* 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 |
* @return A string representation of this object. |
* @param obj the object to test against |
200 |
|
* @return true if the object is equal to this |
201 |
*/ |
*/ |
202 |
public String toString () |
public boolean equals(Object obj) |
203 |
{ |
{ |
204 |
return "Dimension[w:"+width+",h:"+height+']'; |
if (! (obj instanceof Dimension)) |
205 |
|
return false; |
206 |
|
Dimension dim = (Dimension) obj; |
207 |
|
return height == dim.height && width == dim.width; |
208 |
} |
} |
209 |
|
|
210 |
/* Note: There is no Dimension.hashCode. */ |
/** |
211 |
|
* Return the hashcode for this object. It is not documented, but appears |
212 |
public double getWidth() { return width; } |
* to be <code>((width + height) * (width + height + 1) / 2) + width</code>. |
213 |
public double getHeight() { return height; } |
* |
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 |
public void setSize (double width, double height) |
/** |
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 |
this.width = (int) width; |
return getClass().getName() |
232 |
this.height = (int) height; |
+ "[width=" + width + ",height=" + height + ']'; |
233 |
} |
} |
234 |
} |
} // class Dimension |