0

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?

user1803551
  • 12,965
  • 5
  • 47
  • 74
Joe Bryant
  • 21
  • 4

3 Answers3

2

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;
}
0

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.

  1. Both calculate only DPIx, which is the number you get.
  2. Both use 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.
  3. 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);
}
Community
  • 1
  • 1
user1803551
  • 12,965
  • 5
  • 47
  • 74
  • But, there IS a getScreenResolution for the defaultToolkit. Not matter which of my monitors I plug in to my primary HDMI, I get it's actual (I know, the x, and the y, are not the same, so actual is not accurate) resolution. So surely there is either a non-default toolkit for the other monitors, or a getResolution on some other object? – Joe Bryant Jul 07 '16 at 01:36
  • @JoeBryant Toolkit is not a per-monitor (graphics device, actually) object, it's tied to the windowing system. Of course there is a `getScreenResolution()` for *all* toolkits, because they inherit the method from the `awt,Toolkit`. Not all of them implement it and even if they do nothing guarantees a correct result. I added the normal way to get the resolution of a monitor in pixels. A result in DPI is insignificant to programs since they don't care about physical sizes. What do you need it for? – user1803551 Jul 07 '16 at 06:56
  • Again, PIXELS/INCH – Joe Bryant Jul 07 '16 at 12:10
  • @JoeBryant Again, you didn't answer the question. I said that you can get the resolution in pixels. Pixels/inch is not a measurement of resolution for software. What do you need a result that is tied to the physical size of the screen and that is not accurate for? – user1803551 Jul 07 '16 at 12:50
-1

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).

Joe Bryant
  • 21
  • 4