/* * Copyright (c) 2006-2015 DMDirc Developers * * 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. */ package com.dmdirc.ui; import java.awt.GraphicsConfiguration; import java.awt.GraphicsDevice; import java.awt.MouseInfo; import java.awt.PointerInfo; import java.awt.Rectangle; import java.awt.Window; /** * Core UI Utilities. */ public final class CoreUIUtils { /** Prevent creation. */ private CoreUIUtils() { } /** * Centre the specified window on the active monitor. * * @param window Window to centre */ public static void centreWindow(final Window window) { // Get the Location of the mouse pointer final PointerInfo myPointerInfo = MouseInfo.getPointerInfo(); if (myPointerInfo == null) { return; } // Get the Device (screen) the mouse pointer is on final GraphicsDevice myDevice = myPointerInfo.getDevice(); if (myDevice == null) { return; } // Get the configuration for the device final GraphicsConfiguration myGraphicsConfig = myDevice.getDefaultConfiguration(); if (myGraphicsConfig == null) { return; } // Get the bounds of the device final Rectangle gcBounds = myGraphicsConfig.getBounds(); // Calculate the centre of the screen // gcBounds.x and gcBounds.y give the co ordinates where the screen // starts. gcBounds.width and gcBounds.height return the size in pixels // of the screen. final int xPos = gcBounds.x + ((gcBounds.width - window.getWidth()) / 2); final int yPos = gcBounds.y + ((gcBounds.height - window.getHeight()) / 2); // Set the location of the window window.setLocation(xPos, yPos); } }