Java Screen Full Size centreInScreen(Container containee, int xOffSet, int yOffSet)

Here you can find the source of centreInScreen(Container containee, int xOffSet, int yOffSet)

Description

centre In Screen

License

Open Source License

Declaration

public static void centreInScreen(Container containee, int xOffSet, int yOffSet) 

Method Source Code

//package com.java2s;
/* Copyright (C) 2000-2003 The Software Conservancy as Trustee.
 * All rights reserved.//w ww.  j  av  a 2  s  .  c o  m
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Nothing in this notice shall be deemed to grant any rights to trademarks,
 * copyrights, patents, trade secrets or any other intellectual property of the
 * licensor or any contributor except as expressly stated herein. No patent
 * license is granted separate from the Software, for code that you delete from
 * the Software, or for combinations of the Software with other software or
 * hardware.
 */

import java.awt.*;

public class Main {
    private static Rectangle dirtyScreenSizeWithOutAdjustment;
    private static Dimension DEFAULT_SIZE = new Dimension(1000, 800);

    public static void centreInScreen(Container containee, int xOffSet, int yOffSet) {
        Dimension screenSize = getDefaultScreenSize();
        Dimension thisSize = containee.getSize();

        int x = (int) (((screenSize.getWidth() - thisSize.width) / 2) + xOffSet);
        if (x < 0)
            x = 0;

        int y = (int) (((screenSize.getHeight() - thisSize.height) / 2) + yOffSet);
        if (y < 0)
            y = 0;

        containee.setLocation(x, y);
    }

    public static Dimension getDefaultScreenSize() {
        Rectangle dimension = getDefaultScreenSizeWithoutAdjustment();

        // many drivers will return a single graphics device even where there is more than one device (monitor)
        // because the graphics card is making them appear as one, therefore use a hack
        // This assumes the aspect ratio of a monitor is somewhere between 1 and 1.5.
        // The calculation starts to fail as the number of monitors increase.
        // It also assumes that monitors are laids out from left-right, and not from top-bottom
        // TODO: one day this may not be neccessary
        int noOfScreens = (int) Math.floor((double) dimension.width / (double) dimension.height); // this is a good estimate in most cases

        if (noOfScreens < 1) {
            noOfScreens = 1;
        }
        if (dimension.width < 100 || dimension.height < 100) {
            dimension.width = DEFAULT_SIZE.width;
            dimension.height = DEFAULT_SIZE.height;
        }
        return new Dimension(dimension.width / noOfScreens, dimension.height);
    }

    public static Rectangle getDefaultScreenSizeWithoutAdjustment() {
        return getDefaultScreenSizeWithoutAdjustment(false);
    }

    public static Rectangle getDefaultScreenSizeWithoutAdjustment(boolean useDirty) {
        if (useDirty && dirtyScreenSizeWithOutAdjustment != null)
            return dirtyScreenSizeWithOutAdjustment;

        Rectangle totalBounds = new Rectangle(0, 0);

        GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment(); // to work with multiple screens
        GraphicsDevice[] screenDevices = graphicsEnvironment.getScreenDevices();
        for (GraphicsDevice screenDevice : screenDevices) {
            GraphicsConfiguration graphicsConfiguration = screenDevice.getDefaultConfiguration();
            Rectangle singleScreenBounds = graphicsConfiguration.getBounds();
            totalBounds.width += singleScreenBounds.width;
            totalBounds.height = Math.max(totalBounds.height, singleScreenBounds.height);
        }
        dirtyScreenSizeWithOutAdjustment = totalBounds;
        return totalBounds;
    }
}

Related

  1. bringWindowToScreen(Window window)
  2. centrePositionOnScreen(Window window)
  3. ensureOnScreen(Container container)
  4. ensureWindowOnScreen(Window window)
  5. factorScreenDimension(Window window, double widthFactor, double heightFactor)