9

How to get screen size in Java?

Not screen resolution, screen/monitor size in inch and aspect ratio. Application developed has to display some kind of test that has to be the same size regardless to screen size and aspect ratio. Lets say we want to display a rectangle that is 2 cm wide and 2 cm high and we want it to be the same size for every possible type of display adapter.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Tomasz Kaszycki
  • 101
  • 1
  • 6
  • I think it is not possible, because you should know screen DPI to calculate real size, but it [seems not possible](http://stackoverflow.com/questions/4707756/how-to-correctly-detect-dpi-of-display-with-java) [in Java](http://stackoverflow.com/questions/6544510/how-to-get-the-screen-dpi-in-java) – bsiamionau Mar 14 '13 at 11:25
  • Have you checked this answer: http://stackoverflow.com/questions/4322253/screen-display-size – NilsH Mar 14 '13 at 11:26
  • Try `getDefaultTransform ` in `GraphicsConfiguration`. – Catalina Island Mar 14 '13 at 11:28
  • 6
    What is the screen size of a projector image? What is the relevance of the number in any case? Your specification to make something a specific size on-screen makes no sense. – Andrew Thompson Mar 14 '13 at 11:34
  • yes it is.... my client was a terrible idiot and thought that this is relevant in psychological testing.... so all kids could have the same dimensions of area of testing.... it could be relevant.... in very specific case like this – Tomasz Kaszycki Aug 04 '13 at 17:53

2 Answers2

2

I think that the answer is that it is not achievable in pure Java, and possibly not at all.

You can get hold of the displayable screen size in pixels and the nominal scaling (dpi) in Java, via the default Toolkit object. However, these are often inaccurate ... and that would lead to non-square squares.

You might be able to get more accurate values via native code, but this would definitely be non-portable. This Q/A says it is impossible - How to get screen's physical size (i.e. in inches)?

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

Here's an outline of one approach to calibrating a particular display in a test environment:

  • Contruct a calibration rectangle that assumes an arbitrary number of pixels/inch (DPI). An estimate may be obtained from GraphicsConfiguration#getNormalizingTransform().

  • Render that rectangle scaled to the presumed DPI.

  • Add spinners for horizontal and vertical DPI.

  • Ask the user to adjust the spinners to make the rectangle have the desired dimensions.

  • Record the value(s) in java.util.Preferences for subsequent use.

  • Use GraphicsDevice#getIDstring() to identify specific device calibrations.

Addendum: A similar feature is available in ImageJ in the Analyze > SetScale… dialog, illustrated in the wiki topic SpatialCalibration.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045