1 |
/* Dimension.java -- Class for width and height |
/* Copyright (C) 1999, 2000, 2002 Free Software Foundation |
|
Copyright (C) 1999 Free Software Foundation, Inc. |
|
2 |
|
|
3 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
4 |
|
|
26 |
|
|
27 |
package java.awt; |
package java.awt; |
28 |
|
|
29 |
/** |
/* Written using "Java Class Libraries", 2nd edition, plus online |
30 |
* This class holds a width and height value pair. |
* API docs for JDK 1.2 beta from http://www.javasoft.com. |
31 |
* |
* Status: Believed complete and correct, except that neither toString |
32 |
* @author Aaron M. Renn (arenn@urbanophile.com) |
* has not been compared with JDK output. |
|
*/ |
|
|
public class Dimension implements java.io.Serializable |
|
|
{ |
|
|
|
|
|
/* |
|
|
* Instance Variables |
|
|
*/ |
|
|
|
|
|
/** |
|
|
* This width of this object. |
|
|
*/ |
|
|
public int width; |
|
|
|
|
|
/** |
|
|
* The height of this object. |
|
|
*/ |
|
|
public int height; |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/* |
|
|
* Constructors |
|
|
*/ |
|
|
|
|
|
/** |
|
|
* Initializes a new instance of <code>Dimension</code> with a width |
|
|
* and height of zero. |
|
|
*/ |
|
|
public |
|
|
Dimension() |
|
|
{ |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Initializes a new instance of <code>Dimension</code> to have a width |
|
|
* and height identical to that of the specified dimension object. |
|
|
* |
|
|
* @param dim The <code>Dimension</code> to take the width and height from. |
|
|
*/ |
|
|
public |
|
|
Dimension(Dimension dim) |
|
|
{ |
|
|
width = dim.width; |
|
|
height = dim.height; |
|
|
|
|
|
// Your a dim.width, Beavis! |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Initializes a new instance of <code>Dimension</code> with the |
|
|
* specified width and height. |
|
|
* |
|
|
* @param width The width of this object. |
|
|
* @param height The height of this object. |
|
|
*/ |
|
|
public |
|
|
Dimension(int width, int height) |
|
|
{ |
|
|
this.width = width; |
|
|
this.height = height; |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/* |
|
|
* Instance Methods |
|
33 |
*/ |
*/ |
34 |
|
|
35 |
/** |
/** |
36 |
* Returns the size of this object. Not very useful. |
* This class holds a width and height value pair. |
|
* |
|
|
* @return This object. |
|
|
*/ |
|
|
public Dimension |
|
|
getSize() |
|
|
{ |
|
|
return(this); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Sets the width and height of this object to match that of the |
|
|
* specified object. |
|
|
* |
|
|
* @param dim The <code>Dimension</code> object to get the new width and |
|
|
* height from. |
|
|
*/ |
|
|
public void |
|
|
setSize(Dimension dim) |
|
|
{ |
|
|
synchronized(this) |
|
|
{ |
|
|
this.width = dim.width; |
|
|
this.height = dim.height; |
|
|
} |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Sets the width and height of this object to the specified values. |
|
|
* |
|
|
* @param width The new width value. |
|
|
* @param height The new height value. |
|
|
*/ |
|
|
public void |
|
|
setSize(int width, int height) |
|
|
{ |
|
|
synchronized(this) |
|
|
{ |
|
|
this.width = width; |
|
|
this.height = height; |
|
|
} |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Tests this object for equality against the specified object. This will |
|
|
* 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> |
|
|
* |
|
|
* @param obj The object to test against. |
|
|
* |
|
|
* @return <code>true</code> if the specified object is equal to this |
|
|
* object, <code>false</code> otherwise. |
|
|
*/ |
|
|
public boolean |
|
|
equals(Object obj) |
|
|
{ |
|
|
if (obj == null) |
|
|
return(false); |
|
|
|
|
|
if (!(obj instanceof Dimension)) |
|
|
return(false); |
|
|
|
|
|
Dimension dim = (Dimension)obj; |
|
|
|
|
|
if ((dim.width != width) || (dim.height != height)) |
|
|
return(false); |
|
|
|
|
|
return(true); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Returns a string representation of this object. |
|
37 |
* |
* |
38 |
* @return A string representation of this object. |
* @author Per Bothner <bothner@cygnus.com> |
39 |
|
* @author Aaron M. Renn (arenn@urbanophile.com) |
40 |
|
* @date Fenruary 8, 1999. |
41 |
*/ |
*/ |
42 |
public String |
public class Dimension extends java.awt.geom.Dimension2D |
43 |
toString() |
implements java.io.Serializable |
44 |
{ |
{ |
45 |
return(getClass().getName() + "(width=" + width + ", height=" + height + |
/** |
46 |
")"); |
* This width of this object. |
47 |
|
*/ |
48 |
|
public int width; |
49 |
|
|
50 |
|
/** |
51 |
|
* The height of this object. |
52 |
|
*/ |
53 |
|
public int height; |
54 |
|
|
55 |
|
/** |
56 |
|
* Initializes a new instance of <code>Dimension</code> with a width |
57 |
|
* and height of zero. |
58 |
|
*/ |
59 |
|
public Dimension () { } |
60 |
|
|
61 |
|
/** |
62 |
|
* Initializes a new instance of <code>Dimension</code> to have a width |
63 |
|
* and height identical to that of the specified dimension object. |
64 |
|
* |
65 |
|
* @param dim The <code>Dimension</code> to take the width and height from. |
66 |
|
*/ |
67 |
|
public Dimension (Dimension dim) |
68 |
|
{ |
69 |
|
this.width = dim.width; |
70 |
|
this.height = dim.height; |
71 |
|
} |
72 |
|
|
73 |
|
/** |
74 |
|
* Initializes a new instance of <code>Dimension</code> with the |
75 |
|
* specified width and height. |
76 |
|
* |
77 |
|
* @param width The width of this object. |
78 |
|
* @param height The height of this object. |
79 |
|
*/ |
80 |
|
public Dimension (int width, int height) |
81 |
|
{ |
82 |
|
this.width = width; |
83 |
|
this.height = height; |
84 |
|
} |
85 |
|
|
86 |
|
/** |
87 |
|
* Tests this object for equality against the specified object. This will |
88 |
|
* be true if and only if the specified object: |
89 |
|
* <p> |
90 |
|
* <ul> |
91 |
|
* <li>Is not <code>null</code>. |
92 |
|
* <li>Is an instance of <code>Dimension</code>. |
93 |
|
* <li>Has width and height values identical to this object. |
94 |
|
* </ul> |
95 |
|
* |
96 |
|
* @param obj The object to test against. |
97 |
|
* |
98 |
|
* @return <code>true</code> if the specified object is equal to this |
99 |
|
* object, <code>false</code> otherwise. |
100 |
|
*/ |
101 |
|
public boolean equals (Object obj) |
102 |
|
{ |
103 |
|
if (! (obj instanceof Dimension)) |
104 |
|
return false; |
105 |
|
Dimension dim = (Dimension) obj; |
106 |
|
return height == dim.height && width == dim.width; |
107 |
|
} |
108 |
|
|
109 |
|
/** |
110 |
|
* Returns the size of this object. Not very useful. |
111 |
|
* |
112 |
|
* @return This object. |
113 |
|
*/ |
114 |
|
public Dimension getSize () { return new Dimension(this); } |
115 |
|
|
116 |
|
/** |
117 |
|
* Sets the width and height of this object to match that of the |
118 |
|
* specified object. |
119 |
|
* |
120 |
|
* @param dim The <code>Dimension</code> object to get the new width and |
121 |
|
* height from. |
122 |
|
*/ |
123 |
|
public void setSize (Dimension dim) |
124 |
|
{ |
125 |
|
this.width = dim.width; |
126 |
|
this.height = dim.height; |
127 |
|
} |
128 |
|
|
129 |
|
/** |
130 |
|
* Sets the width and height of this object to the specified values. |
131 |
|
* |
132 |
|
* @param width The new width value. |
133 |
|
* @param height The new height value. |
134 |
|
*/ |
135 |
|
public void setSize (int width, int height) |
136 |
|
{ |
137 |
|
this.width = width; |
138 |
|
this.height = height; |
139 |
|
} |
140 |
|
|
141 |
|
/** |
142 |
|
* Returns a string representation of this object. |
143 |
|
* |
144 |
|
* @return A string representation of this object. |
145 |
|
*/ |
146 |
|
public String toString () |
147 |
|
{ |
148 |
|
return "Dimension[w:"+width+",h:"+height+']'; |
149 |
|
} |
150 |
|
|
151 |
|
/* Note: There is no Dimension.hashCode. */ |
152 |
|
|
153 |
|
public double getWidth() { return width; } |
154 |
|
public double getHeight() { return height; } |
155 |
|
|
156 |
|
public void setSize (double width, double height) |
157 |
|
{ |
158 |
|
this.width = (int) width; |
159 |
|
this.height = (int) height; |
160 |
|
} |
161 |
} |
} |
|
|
|
|
} // class Dimension |
|
|
|
|