Java get MAXIMIZED state window size

2019-08-08 20:28发布

问题:

I am developing a multi platform game and I wanted to make a full-screen non bordered window and this is where I got into a problem for LINUX operating system.

For WINDOWS system I was using Toolkit.getDefaultToolkit().getScreenSize() method to get complete SCREEN SIZE and it worked fine, but for LINUX my screen was a bit wider and higher due to LINUX BARS.

*SOLUTION in my answer bellow

回答1:

You could have a look at Full-Screen Exclusive Mode API. This will give your program full access to the whole screen, removing any need for you to be concerned about task/dock bars and the like

Or you could do something like

GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
Rectangle bounds = gd.getDefaultConfiguration().getBounds();
Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gd.getDefaultConfiguration());

Rectangle safeBounds = new Rectangle(bounds);
safeBounds.x += insets.left;
safeBounds.y += insets.top;
safeBounds.width -= (insets.left + insets.right);
safeBounds.height -= (insets.top + insets.bottom);

Essentially all this does is gets the screen bounds (x/y and width/height), the screens insets for the device and updates the screen bounds to take into consideration things like the task bar and other system resources.

You could also simply try using something like Frame#setExtendedState which will allow you to put the window into Frame.MAXIMIZED_BOTH and let it do the work for you



回答2:

*SOLUTION
After many hours of browsing the internet I already found the solution to get size of the window when its in MAXIMIZED state (Like you press maximize square on frame) and gives you MAXIMUM allowed size of the window paying attention on all OPERATING SYSTEM bars. Here is the method: GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds()