java.awt

Class Dimension

public class Dimension extends Dimension2D implements Serializable

This class holds a width and height value pair. This is used in plenty of windowing classes, but also has geometric meaning.

It is valid for a dimension to have negative width or height; but it is considered to have no area. Therefore, the behavior in various methods is undefined in such a case.

There are some public fields; if you mess with them in an inconsistent manner, it is your own fault when you get invalid results. Also, this class is not threadsafe.

Since: 1.0

See Also: Component LayoutManager

UNKNOWN: updated to 1.14

Field Summary
intheight
The height of this object.
intwidth
The width of this object.
Constructor Summary
Dimension()
Create a new Dimension with a width and height of zero.
Dimension(Dimension d)
Create a new Dimension with width and height identical to that of the specified dimension.
Dimension(int w, int h)
Create a new Dimension with the specified width and height.
Method Summary
booleanequals(Object obj)
Tests this object for equality against the specified object.
doublegetHeight()
Gets the height of this dimension.
DimensiongetSize()
Returns the size of this dimension.
doublegetWidth()
Gets the width of this dimension.
inthashCode()
Return the hashcode for this object.
voidsetSize(double w, double h)
Sets the size of this dimension.
voidsetSize(Dimension d)
Sets the width and height of this object to match that of the specified object.
voidsetSize(int w, int h)
Sets the width and height of this object to the specified values.
StringtoString()
Returns a string representation of this object.

Field Detail

height

public int height
The height of this object.

Serial: the height

See Also: getSize Dimension

width

public int width
The width of this object.

Serial: the width

See Also: getSize Dimension

Constructor Detail

Dimension

public Dimension()
Create a new Dimension with a width and height of zero.

Dimension

public Dimension(Dimension d)
Create a new Dimension with width and height identical to that of the specified dimension.

Parameters: d the Dimension to copy

Throws: NullPointerException if d is null

Dimension

public Dimension(int w, int h)
Create a new Dimension with the specified width and height.

Parameters: w the width of this object h the height of this object

Method Detail

equals

public boolean equals(Object obj)
Tests this object for equality against the specified object. This will be true if and only if the specified object is an instance of Dimension2D, and has the same width and height.

Parameters: obj the object to test against

Returns: true if the object is equal to this

getHeight

public double getHeight()
Gets the height of this dimension.

Returns: the height, as a double

getSize

public Dimension getSize()
Returns the size of this dimension. A pretty useless method, as this is already a dimension.

Returns: a copy of this dimension

Since: 1.1

See Also: setSize

getWidth

public double getWidth()
Gets the width of this dimension.

Returns: the width, as a double

hashCode

public int hashCode()
Return the hashcode for this object. It is not documented, but appears to be ((width + height) * (width + height + 1) / 2) + width.

Returns: the hashcode

setSize

public void setSize(double w, double h)
Sets the size of this dimension. The values are rounded to int.

Parameters: w the new width h the new height

Since: 1.2

setSize

public void setSize(Dimension d)
Sets the width and height of this object to match that of the specified object.

Parameters: d the Dimension to get the new width and height from

Throws: NullPointerException if d is null

Since: 1.1

See Also: getSize

setSize

public void setSize(int w, int h)
Sets the width and height of this object to the specified values.

Parameters: w the new width value h the new height value

toString

public String toString()
Returns a string representation of this object. The format is: getClass().getName() + "[width=" + width + ",height=" + height + ']'.

Returns: a string representation of this object