Screen display size

2020-04-07 19:09发布

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

标签: java
7条回答
一纸荒年 Trace。
2楼-- · 2020-04-07 19:48

Use the getScreenResolution() method of Toolkit class which does returns the screen resolution in dots-per-inch. (ref javadocs)

查看更多
家丑人穷心不美
3楼-- · 2020-04-07 19:51

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:

 strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colSoftware = objWMIService.ExecQuery _
    ("Select * from Win32_DesktopMonitor")

For Each objSoftware in colSoftware
    Wscript.Echo "Caprion: " & objSoftware.Caption
    Wscript.Echo "Description: " & objSoftware.Description
    Wscript.Echo "PixelsPerXLogicalInch: " & objSoftware.PixelsPerXLogicalInch
    Wscript.Echo "ScreenWidth: " & objSoftware.ScreenWidth
Next

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.

查看更多
该账号已被封号
4楼-- · 2020-04-07 19:55

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.

查看更多
不美不萌又怎样
5楼-- · 2020-04-07 19:55

Try this,

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
查看更多
相关推荐>>
6楼-- · 2020-04-07 19:58

You can try to use:

Dimension screen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
int pixelPerInch= java.awt.Toolkit.getDefaultToolkit().getScreenResolution();
double height=screen.getHeight()/pixelPerInch;
double width=screen.getWidth()/pixelPerInch;
double x=Math.pow(height,2);
double y=Math.pow(width,2);
double diagonal= Math.sqrt(x+y);

The "diagonal" value is in inches.

查看更多
The star\"
7楼-- · 2020-04-07 20:01

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.

查看更多
登录 后发表回答