Reputation: 21
I know I can call Toolkit.getDefaultToolkit().getScreenResolution()
and get the DPI for the default screen, but I can not find a way for the life of me to get the same information for the other monitors I have connected. Can anyone tell me either how to get a Toolkit
for the other monitors, or a call on the devices to get the DPI for those monitors?
Upvotes: 0
Views: 2490
Reputation: 21
The DPI of all screens can be determined with following code: (JavaFX has to be initialized!)
public List<Integer> getScreenResolutions() {
List<Integer> resolutions = new ArrayList<>();
final List<?> screens = com.sun.javafx.tk.Toolkit.getToolkit().getScreens();
for(Object screen : screens) {
if (screen instanceof com.sun.glass.ui.Screen) {
resolutions.add(((com.sun.glass.ui.Screen) screen).getResolutionX());
}
}
return resolutions;
}
Upvotes: 2
Reputation: 21
After spending some time in debug, I found my answer. There are fields on the graphics device for the X and Y resolution (xResolution, yResolution) that give me what I needed).
Upvotes: -1
Reputation: 13408
There are several issues with this. Let's start from the theoretical point.
The DPI (actually PPI) is calculated separately for the width and height:
DPIx = screen width / resolutionx
DPIy = screen height / resolutiony
If DPIx=DPIy (which happens when the aspect ratio of you screen the same as the resolution's) then you have a single DPI value, but this is not enforced. So, you'll have to be careful about using a single DPI value.
This leads us to the second issue - the physical screen size is not normally accessible and even not always known. See this question and this question for doing it in Java.
There are 2 toolkit implementations that implement getScreenResolution()
: Unix systems use the XToolkit
(based on the X11 windowing system) and Windows systems use the WToolkit
.
native
methods to try and get info from the OS, however that info is not reliable. See this question. You usually get a default value returned, which seems to be 96.WToolkit
's javadoc specifies explicitly when getting DPIx:
Returns the number of pixels per logical inch along the screen width. In a system with multiple display monitors, this value is the same for all monitors.
So there goes the multi-monitor plan. XToolkit
does not even javadoc that method.
In conclusion, I wouldn't trust getScreenResolution()
on any display or operating system. What you can get safely is the resolution (even all possible resolutions, refresh rates, color depths...) and ask the user to input the physical size data if relevant. Remember that you can have projectors that can change their "screen size". Otherwise, you will need to interface directly with the driver in hopes that it has that valid information, which is not guaranteed.
Edit: To get the resolution in pixels of your devices, use:
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gds = ge.getScreenDevices();
for (GraphicsDevice gd : gds) {
DisplayMode dm = gd.getDisplayMode();
int h = dm.getHeight();
int w = dm.getWidth();
System.out.println(w + "x" + h);
// or
GraphicsConfiguration gc = gd.getDefaultConfiguration();
Rectangle rec = gc.getBounds();
System.out.println(rec.width + "x" + rec.height);
}
Upvotes: 0