Hi I'm writing a graphics program and I've been searching for a way to get the physical size of the screen being used. I can get the size of the screen in pixels and also the logical resolution. What I can't seem to find is anywhere to get the physical dimensions that are always in the specs for any monitor (eg 19"- 376 x 301 mm). Question, is this information even stored anywhere in the OS when it loads the driver for the particular screen being used? The program I'm writing needs to work on Mac and Windows.
Thanks!
nt
Use the
getScreenResolution()
method ofToolkit
class which does returns the screen resolution in dots-per-inch. (ref javadocs)OK, following Andreas' comments I performed some search and found solution for windows. You can use WMI. WMI can be invoked from java using one of the interoperability tools (jinterop, jintegra, jawin) or by execution of external script (VBScript or JScript). The following script is an example:
save it in file screen.vbs and run it using command line
cscript screen.vbs
Then ScreenWidth/PixelsPerXLogicalInch gives you width in inches.See
[http://msdn.microsoft.com/en-us/library/aa389273%28v=VS.85%29.aspx][1]
for more information. I believe that solution for other OS types also exist.I don't think it's possible with java. You can try to write some jni code if this feature is absolutely essential in your program. Otherwise, just ask the user for their monitor size.
EDIT Looks like SWT can give you DPI, and you can calculate the monitor size with it: http://www.java2s.com/Code/JavaAPI/org.eclipse.swt.graphics/DevicegetDPI.htm
But, you'd have to use SWT :) Which is actually not that bad choice if you want to develop good-looking programs for Mac.
Try this,
You can try to use:
The "diagonal" value is in inches.
No way with pure Java. The OS doesn't care about physical screen size. Resolution is all it needs. With native code you may be able to get the name of the connected display from the driver and try to look up the technical specs for this type.
As Angus mentioned - it may even be possible that OS or driver can tell a dpi value for the connected screen but even in the best case, at least the screen has to report it's dpi/physical dimensions.