javacavaj
javacavaj

Reputation: 2971

Shape at "Actual Size"

What's the easy way to render a shape in Java to its "actual size". For example, I have a tube with a diameter of 1" and I want to depict it on screen as the outline of a 1" circle. The Graphics2D method drawOval(int x, int y, int width, int height) takes a height and width in pixels. What are the steps to translate a pixel size into the size rendered on screen?

Thanks in advance.

Upvotes: 3

Views: 1063

Answers (8)

Kevin Herron
Kevin Herron

Reputation: 6985

I was under the impression that simply getting the screen resolution from Toolkit was not enough. I think you need to do something more along the lines of

float scalingFactor = Toolkit.getDefaultToolkit().getScreenResolution() / 72f;

Making a 1" square

int width = 1.0 * scalingFactor;

and a 2.5" square

int width = 2.5 * scalingFactor;

All of this being that Java2D assumes a 72 dpi screen resolution, and if the system is set differently you need to scale up to correct for this.

Upvotes: 0

DJClayworth
DJClayworth

Reputation: 26856

In theory you can do it this way. The java.awt.Toolkit will tell you the size of a pixel, and the pixel dimensions of the screen. So, to draw a 1" circle, you'd use a diameter of 1.0 * tk.getScreenResolution(), a 2.5" circle is 2.5 * tk.getScreenResolution(), etc. Or you can use the GraphicsConfiguration.getNormalizingTransform() method which adjusts the resolution to a 'fixed' size.

Unfortunately both of these methods rely on the underlying system knowing (and telling you) the actual resolution of your screen. In practice this very rarely occurs. All sorts of things can affect the actual size of a pixel. The actual size and make of monitor is one, and some monitors even allow you to adjust the size of the image on the screen.

This article http://www.developer.com/java/other/print.php/626071 discusses this.

Printers are generally better at telling you their real resolution. If you absolutely must have a picture which is the correct size, send it there.

Acknowledgements to the various answers from which I synthesized this one.

Upvotes: 1

erickson
erickson

Reputation: 269667

The java.awt.Toolkit will tell you the size of a pixel, and the pixel dimensions of the screen. This is based on information from the underlying system, however, which may sometimes be misconfigured.

So, to draw a 1" circle, you'd use a diameter of 1.0 * tk.getScreenResolution(), a 2.5" circle is 2.5 * tk.getScreenResolution(), etc.

Upvotes: 2

Adrian Grigore
Adrian Grigore

Reputation: 33318

You should be aware that, even though you might be able to find out about the screen size and resolution, you still can't be sure of the actual size of the displayed picture. If the user has a CRT screen, the screen is likely to be a bit smaller than the actual screen size.

Therefore, if you really need accurate results, the only way is to let the user adjust a ruler displayed on the screen interactively and compare it with an actual ruler.

Upvotes: 1

CookieOfFortune
CookieOfFortune

Reputation: 13974

Well, you would need the size of the monitor and resolution. Let's say the monitor is 17" with a 1280:1024 aspect ratio.

The screen size is the hypotenuse, so you would need to find the number of pixels on the hypotenuse. Simple geometry later, you can get the pixels/inch calculation.

1280px^2 + 1024px^2 = c^2, c ~= 1639.2px. So it's 1639.2px/17inch = 96.4px/inch for a 17 inch monitor with 1280x1024 resolution. This would have to be entered by the user.

Upvotes: 0

Greg Noe
Greg Noe

Reputation: 1156

This is a curious question, one I haven't thought of. Off the top of my head, you would probably need to know a combination of screen size (17", 21", etc.), screen resolution ("800x600, 1280x1024, etc.) and DPI of the screen (72, 96, 120, etc.).

Through various api's, you can determine the screen resolution, and maybe the dpi... but good luck with the screen size. And even with all that, you're still not guaranteed to produce the correct size on screen.

Upvotes: -1

Oli
Oli

Reputation: 228

The getNormalizingTransform() method of the class GraphicsConfiguration looks like it has some potential

http://java.sun.com/javase/6/docs/api/java/awt/GraphicsConfiguration.html#getNormalizingTransform()

Upvotes: 5

David Webb
David Webb

Reputation: 193696

The problem you're going to have is Pixels are not always the same size. For example a 100 x 100 pixel square is going to be different sizes on a 17" 1280 x 1024 monitor and a 19" 1280 x 1024 monitor.

I don't believe there is an API which tells you the physical size of a display.

Asking the user the size of their monitor might not help as a lot of people simply won't know.

You could display a number of lines on screen and get the user to click which one is closest to 1 inch and scale all your rendering to that, but it's a bit clumsy.

Upvotes: 0

Related Questions