Method return type in Dimension class of java.awt
Question
I am surprised to see that getters of height
and width
members has return
type double
, albeit they are int
. Moreover, setSize
method with double parameters has the following definition:
/**
* Sets the size of this <code>Dimension</code> object to
* the specified width and height in double precision.
* Note that if <code>width</code> or <code>height</code>
* are larger than <code>Integer.MAX_VALUE</code>, they will
* be reset to <code>Integer.MAX_VALUE</code>.
*
* @param width the new width for the <code>Dimension</code> object
* @param height the new height for the <code>Dimension</code> object
*/
public void setSize(double width, double height) {
this.width = (int) Math.ceil(width);
this.height = (int) Math.ceil(height);
}
Please have a look at Dimension class. Above comment says values cannot go beyond Integer.MAX_VALUE. Why?
Why do we have double
in between? Is there any subtle reason? Can anyone please explain this to me? Sorry for my insistence!
Solution
java.awt.Dimension
was retrofitted to fit into the java.awt.geom
package, so that it can be used wherever a Dimension2D
is required. The interface for the later deals with floating point, so Dimension
has to also. Being limited to int
fields, only a subset of double
s can be represented. Dimension2D.Float
is similarly restricted.
OTHER TIPS
The class is storing height
and width
as int
, it just provides a method that accept double too so you can call it with double values (but they are immediately cast to int). There are others setSize()
methods in this file that accept int
values or even a Dimension
object.
And as these values are stored as int
, of course their maximum value is Integer.MAX_VALUE
.
You can use the java Dimension class with ints. If you'd need a Dimension class with double width and height, youn could use the following:
public class DoubleDimension {
double width, height;
public DoubleDimension(double width, double height) {
super();
this.width = width;
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
@Override
public String toString() {
return "DoubleDimension [width=" + width + ", height=" + height + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(height);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(width);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
DoubleDimension other = (DoubleDimension) obj;
if (Double.doubleToLongBits(height) != Double.doubleToLongBits(other.height))
return false;
if (Double.doubleToLongBits(width) != Double.doubleToLongBits(other.width))
return false;
return true;
}
}