Java Screen Full Size fitToScreen(Dimension size, GraphicsConfiguration screen)

Here you can find the source of fitToScreen(Dimension size, GraphicsConfiguration screen)

Description

Returns a new size where width and height are the minimum of the given size and the available screen space.

License

Open Source License

Parameter

Parameter Description
size The original size of the component on the screen (usually a window).
screen Configuration of the screen on which the component should be shown.

Return

A new size object with a size no larger than the available screen space.

Declaration

public static Dimension fitToScreen(Dimension size, GraphicsConfiguration screen) 

Method Source Code

//package com.java2s;
/*/*  w  ww . j a  v  a  2 s.c  o  m*/
 * Copyright (C) 2001-2013 Michael Koch (tensberg@gmx.net)
 *
 * This file is part of JGloss.
 *
 * JGloss is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * JGloss is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JGloss; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 */

import java.awt.Dimension;
import java.awt.GraphicsConfiguration;

import java.awt.Insets;

import java.awt.Toolkit;

public class Main {
    /**
     * Returns a new size where width and height are the minimum of the given size and the available screen space.
     *
     * @param size The original size of the component on the screen (usually a window).
     * @param screen Configuration of the screen on which the component should be shown.
     * @return A new size object with a size no larger than the available screen space.
     */
    public static Dimension fitToScreen(Dimension size, GraphicsConfiguration screen) {
        Dimension resizedSize = new Dimension();

        Dimension displaySize = screen.getBounds().getSize();
        Insets displayInsets = Toolkit.getDefaultToolkit().getScreenInsets(screen);
        resizedSize.width = Math.min(size.width, displaySize.width - displayInsets.left - displayInsets.right);
        resizedSize.height = Math.min(size.height, displaySize.height - displayInsets.top - displayInsets.bottom);

        return resizedSize;
    }
}

Related

  1. ensureOnScreen(Container container)
  2. ensureWindowOnScreen(Window window)
  3. factorScreenDimension(Window window, double widthFactor, double heightFactor)
  4. fitInScreen(Component comp)
  5. fitInScreen(Window o)
  6. forceToScreen(Window window)
  7. fullScreen()
  8. fullScreen(Component component)
  9. fullScreen(Window window)