Java Screen Center getCenter(Component owner, Dimension size)

Here you can find the source of getCenter(Component owner, Dimension size)

Description

get Center

License

Open Source License

Declaration

public static Point getCenter(Component owner, Dimension size) 

Method Source Code

//package com.java2s;
/** /*from  w  w w  . ja  va  2s .c o m*/
 * Copyright (C) 2010  Jorge J. Gomez-Sanz 
 * 
 * This file is part of the INGENME tool. INGENME is an open source meta-editor
 * which produces customized editors for user-defined modeling languages
 *
 * 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 version 3 of the License
 *
 * 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, see <http://www.gnu.org/licenses/>
 **/

import java.awt.*;

public class Main {
    public static Point getCenter(Dimension size) {
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        Point result = new Point(d.width / 2 - size.width / 2, d.height / 2 - size.height / 2);
        return result;
    }

    public static Point getCenter(Frame parent, Dimension size) {
        Dimension d = parent.getSize();
        Point result = new Point(parent.getLocation().x + d.width / 2 - size.width / 2,
                parent.getLocation().y + d.height / 2 - size.height / 2);
        return result;
    }

    public static Point getCenter(Component owner, Dimension size) {
        Dimension d = owner.getSize();
        Point result = new Point((d.width / 2 - size.width / 2) + owner.getLocation().x,
                (d.height / 2 - size.height / 2) + owner.getLocation().y);
        return result;
    }
}

Related

  1. centreOnScreen(Window frame)
  2. centreOnScreen(Window win)
  3. getCenterCoordinates(Dimension d)
  4. getCenteredLocation(Component comp)
  5. getCenteredLocation(Window w)
  6. getCenteredLocation(Window window)