Method return type in Dimension class of java.awt
Asked Answered
W

3

10

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!

Wring answered 28/2, 2012 at 18:32 Comment(0)
E
4

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 doubles can be represented. Dimension2D.Float is similarly restricted.

Emlynn answered 28/2, 2012 at 18:51 Comment(0)
K
3

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.

Kaif answered 28/2, 2012 at 18:35 Comment(5)
Thats it? Why do we have getters with double as return type? Any use?Wring
I don't know the exact reason why they return as double (probably because when talking about dimensions, you're talking about precision and double is more used in these cases), but for the setters, it is just some method overloading to help the user.Kaif
Thanks for your reply, I don't think doubles having int values will help in precision. I mean double 1517.00 is equal to 1517, so there must be some reason?Wring
@Wring no I know, what I meant was more that people are working with double values when working with dimensions, and they set the return type of getters to double so users don't have to cast it on their side. But I agree that in terms of precision there's no use.Kaif
@Wring I have to admit that it is interesting, good question.Kaif
I
1

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;
    }
}
Impress answered 14/2, 2018 at 5:51 Comment(1)
You can also extend Dimension2D class so you keep compatibility with other Java functions. I implemented something similar in Kotlin gist.github.com/vladostaci/db4f601611879aa657403ff277324130Alasteir

© 2022 - 2024 — McMap. All rights reserved.