简体   繁体   中英

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 ?

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.

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 .

它必须因为它的父类java.awt.geom.Dimension2D基于双精度。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM