Java Screen Center getFrameCenteredLocation(final java.awt.Component centerThis, final Component parent)

Here you can find the source of getFrameCenteredLocation(final java.awt.Component centerThis, final Component parent)

Description

Get the location for centered Dialog

License

Open Source License

Parameter

Parameter Description
centerThis a parameter

Declaration

public static java.awt.Point getFrameCenteredLocation(final java.awt.Component centerThis,
        final Component parent) 

Method Source Code

//package com.java2s;
/*//w w  w  . jav a  2s  .com
 * @(#) Helpers.java
 *
 * Created on 22.04.2006 by Daniel Becker (quippy@quippy.de)
 *
 *-----------------------------------------------------------------------
 *  This program 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.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *----------------------------------------------------------------------
 */

import java.awt.Component;

public class Main {
    /**
     * Get the location for centered Dialog
     * @since 22.06.2006
     * @param centerThis
     * @return
     */
    public static java.awt.Point getFrameCenteredLocation(final java.awt.Component centerThis,
            final Component parent) {
        java.awt.Dimension screenSize = (parent == null) ? java.awt.Toolkit.getDefaultToolkit().getScreenSize()
                : parent.getSize();

        int x = (screenSize.width - centerThis.getWidth()) >> 1;
        int y = (screenSize.height - centerThis.getHeight()) >> 1;

        if (parent != null) {
            x += parent.getX();
            y += parent.getY();
        }
        if (x < 0)
            x = 0;
        if (y < 0)
            y = 0;

        return new java.awt.Point(x, y);
    }
}

Related

  1. getCenterPoint(final int w, final int h)
  2. getCenterPointRelativeToScreen(Dimension size)
  3. getCenterPosition(Dimension d)
  4. getCenterPositionForComponent(int width, int heigth)
  5. getDimensionFromPercent(int percentWidth, int percentHeight)
  6. getFrameOnCenterLocationPoint(Window window)
  7. getLocationToCenterOnScreen(Component comp)
  8. getPercentageOnScreen(Point location, Dimension size, Rectangle screen)
  9. locateOnScreenCenter(Component component)