Java Screen Center moveToCenter(Component componentToMove, Component componentToCenterOver)

Here you can find the source of moveToCenter(Component componentToMove, Component componentToCenterOver)

Description

Centers componentToMove over componentToCenterOver.

License

Open Source License

Parameter

Parameter Description
componentToMove a parameter
componentToCenterOver a parameter

Declaration

public static void moveToCenter(Component componentToMove, Component componentToCenterOver) 

Method Source Code

//package com.java2s;
/*/*from   ww w  .j av a2 s  .  c  o  m*/
 * Copyright (C) 2004 by StreetFire Sound Labs
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: Util.java,v 1.7 2005/03/22 08:59:06 iain Exp $
 */

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.IllegalComponentStateException;

import java.awt.Point;
import java.awt.Toolkit;

public class Main {
    /**
     * Centers componentToMove over componentToCenterOver. If component to centerOver is not visible or
     * null, the componentToMove is center on screen.
     * @param componentToMove
     * @param componentToCenterOver
     */
    public static void moveToCenter(Component componentToMove, Component componentToCenterOver) {
        try {
            int centerX, centerY;

            // component is null
            if (componentToCenterOver == null) {
                // center on screen
                Point p = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
                centerX = p.x;
                centerY = p.y;
            } else {
                // try to get location on screen
                try {
                    Point point = componentToCenterOver.getLocationOnScreen();
                    centerX = (int) point.x + componentToCenterOver.getWidth() / 2;
                    centerY = (int) point.y + componentToCenterOver.getHeight() / 2;
                } catch (IllegalComponentStateException ex) {
                    // component not visible on screen
                    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
                    centerX = dim.width / 2;
                    centerY = dim.height / 2;
                }
            }

            // determine new local for component
            int moveX = centerX - (componentToMove.getWidth() / 2);
            int moveY = centerY - (componentToMove.getHeight() / 2);

            // move it.
            componentToMove.setLocation(moveX, moveY);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

Related

  1. getFrameCenteredLocation(final java.awt.Component centerThis, final Component parent)
  2. getFrameOnCenterLocationPoint(Window window)
  3. getLocationToCenterOnScreen(Component comp)
  4. getPercentageOnScreen(Point location, Dimension size, Rectangle screen)
  5. locateOnScreenCenter(Component component)
  6. moveToScreenCenter(Window w)
  7. packAndCenter(Window wind)
  8. packAndCenter(Window window)
  9. screenCenter(Window window)