Reputation: 429
I am having an issue using this method:
Toolkit.getDefaultToolkit().getScreenSize()
I am coding an application in Java using Swing that must be compatible with any screen resolution, because it will be an application for desktop and mobile devices. I've been reading about how to retrieve the screen resolution and everybody refers to this method. I've used it and for my recommendable screen resolution (1920x1080) it works fine, but I realized that, when I change the screen resolution of my monitor (I am working on a laptop) sometimes the ethod does not retrieve the correct screen resolution specified (and yes, I confirm the changes when I change the screen resolution from the Windows configuration).
I've read about the method but I realized that it is not useful for what I am trying to do (I think):
Toolkit.getDefaultToolkit().getScreenResolution()
My curiosity has led me to check the retrieved values of the method Toolkit.getDefaultToolkit().getScreenSize()
and I've made this table:
My question here is, how can be this possible? Is this a bug or there is something more to do to retrieve the correct screen size always? I don't understand why I am not getting the correct values always
Upvotes: 4
Views: 3224
Reputation: 759
getScreenResolution() returns the DPI of the screen, it is better to use GraphicsDevice.getDisplayModes();
You can then get the screen height and width from the returned DisplayMode objects.
Also be careful if you have multiple monitors as some methods return the values for the primary display only.
This code is a bit messy but it might help.
for (DisplayMode mode : GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayModes())
{
System.out.println(mode.getWidth() + " x " + mode.getHeight() + "(" + mode.getBitDepth() + ") : refresh rate " + mode.getRefreshRate());
}
You can also get the current display mode on the default screen device with
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode()
Upvotes: 4