ripper234
ripper234

Reputation: 230156

Why does java.awt.Dimension use doubles for height and width?

From the javadoc:

The Dimension class encapsulates the width and height of a component (in integer precision)

Why not represent these as an int or long?

Upvotes: 3

Views: 567

Answers (3)

Esko
Esko

Reputation: 29377

I believe the original explanation for this is that dimensions are usually relative, for example the far left edge of the image is 0.0 and far right of the image is 1.0 (and same for vertical dimension, of course). Pixel (or point) is just a sample from a relative point on that range, so if you have an image with width of 350 pixels, the row's 210th pixel is at point 0.6 on the dimension range.

This, of course, is just the reasoning behind the used data type. In practice this doesn't usually apply since instead of being an inclusive range from 0.0 to 1.0, range from 0.0 to [width in pixels].0 is used instead.


Addentum: Range from 0.0 to 1.0 should make perfect sense to you if you're even slightly aware of vector calculus.

Upvotes: 2

Qwerky
Qwerky

Reputation: 18445

It has to because it's parent class java.awt.geom.Dimension2D is based on doubles.

Upvotes: 0

Eric
Eric

Reputation: 367

I reckon it is because the superclass, Dimension2D, declares the method to return a double. For most solutions an integer is fine, but when you wish to create a Dimension2D type for vector graphics, returning dimensions as something else than integers would be necessary.

Upvotes: 3

Related Questions