Java Screen Size maximizeWindowWithMargin(final Window window, final int margin, final Dimension maxSize)

Here you can find the source of maximizeWindowWithMargin(final Window window, final int margin, final Dimension maxSize)

Description

Resizes a window by setting its bounds to maximum that fits inside the default screen having the specified margin around it, and centers the window on the screen.

License

Apache License

Parameter

Parameter Description
window window to be resized
margin margin to leave around the window
maxSize optional parameter defining a maximum size

Declaration

public static void maximizeWindowWithMargin(final Window window,
        final int margin, final Dimension maxSize) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.awt.Dimension;

import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;

import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Window;

public class Main {
    /**//from   w  w  w  . ja  v  a2  s. c  o  m
     * Resizes a window by setting its bounds to maximum that fits inside the default screen having the specified margin around it, and centers the window on
     * the screen.
     * 
     * <p>
     * The implementation takes the screen insets (for example space occupied by task bar) into account.
     * </p>
     * 
     * @param window window to be resized
     * @param margin margin to leave around the window
     * @param maxSize optional parameter defining a maximum size
     */
    public static void maximizeWindowWithMargin(final Window window,
            final int margin, final Dimension maxSize) {
        // Maybe use window.getGraphicsConfiguration() (it probably accounts multi-screens!)
        // Edit: it does, but setLocationRelativeTo() at the end will always use the default screen device!
        GraphicsConfiguration gconfig = window.getGraphicsConfiguration();
        if (gconfig == null)
            GraphicsEnvironment.getLocalGraphicsEnvironment()
                    .getDefaultScreenDevice().getDefaultConfiguration();

        final Rectangle bounds = gconfig.getBounds();
        final Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(
                gconfig);

        final int width = bounds.width - insets.left - insets.right
                - margin * 2;
        final int height = bounds.height - insets.top - insets.bottom
                - margin * 2;

        if (maxSize == null)
            window.setSize(width, height);
        else
            window.setSize(Math.min(width, maxSize.width),
                    Math.min(height, maxSize.height));

        // And finally center on screen
        // window.setLocationRelativeTo( null ) always centers on the main screen, so:
        window.setLocationRelativeTo(window.getOwner());
    }
}

Related

  1. getMinimumCacheSize(int tileSize, double overdrawFactor)
  2. getSizeWithScreen(double xd, double yd)
  3. getSystemSizeRate()
  4. getWindowActualSize(final Window window)
  5. makeEllipse(double x, double y, double size)
  6. screenSize()
  7. setLocationRelativeToAndSizeToWindow(Component parent, Component child, Dimension max)
  8. setSize(final Window window, final int minusX, final int minusY)
  9. setSize(Window window, double widthFactor, double heightFactor)