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
Try this,
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
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.
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.
Use the getScreenResolution()
method of Toolkit
class which does returns the screen resolution in dots-per-inch. (ref javadocs)
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.
In SWT you can use getShell().getDisplay().getPrimaryMonitor().getClientArea();
final Display display = getShell().getDisplay();
final Monitor monitor = display.getPrimaryMonitor();
final Rectangle rect;
if (monitor != null) {
rect = monitor.getClientArea();
} else {
// In case we cannot find the primary monitor get the entire display rectangle
// Note that it may include the dimensions of multiple monitors.
rect = display.getBounds();
}
System.out.println("Monitor width=" + String.valueOf(rect.width));
System.out.println("Monitor height=" + String.valueOf(rect.height));