Java Screen Center centreOnScreen(Window win)

Here you can find the source of centreOnScreen(Window win)

Description

Centre the given window on the screen.

License

GNU General Public License

Parameter

Parameter Description
win The Window to position.

Declaration

public static void centreOnScreen(Window win) 

Method Source Code

//package com.java2s;
/*//from w  ww . ja  v a  2s.  c  o  m
 * Copyright (C) 2002-2014 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.awt.Dimension;

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

public class Main {
    /**
     * Centre the given window on the screen.
     * 
     * @param win The Window to position.
     */
    public static void centreOnScreen(Window win) {
        Dimension screenRes = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension windowSize = win.getSize();

        int x = Math.max(0, (screenRes.width - windowSize.width) / 2);
        int y = Math.max(0, (screenRes.height - windowSize.height) / 2);

        win.setLocation(x, y);
    }
}

Related

  1. centreOnScreen(Window frame)
  2. getCenter(Component owner, Dimension size)
  3. getCenterCoordinates(Dimension d)
  4. getCenteredLocation(Component comp)
  5. getCenteredLocation(Window w)