Java Screen Center setCenter(Component component, Component component1)

Here you can find the source of setCenter(Component component, Component component1)

Description

set Center

License

Open Source License

Declaration

public static void setCenter(Component component, Component component1) 

Method Source Code


//package com.java2s;
/*/*from w w w  . jav a  2 s. c  om*/
 * Copyright (C) 2012 Marco Ratto
 *
 * This file is part of the project MQJExplorer.
 *
 * MQJExplorer 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
 * any later version.
 *
 * MQJExplorer 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 MQJExplorer; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.awt.*;

public class Main {
    public static void setCenter(Component component, Component component1) {
        Point point;
        Dimension dimension;
        if (component != null) {
            point = component.getLocation();
            dimension = component.getSize();
        } else {
            dimension = Toolkit.getDefaultToolkit().getScreenSize();
            point = new Point(0, 0);
        }
        Dimension dimension1 = Toolkit.getDefaultToolkit().getScreenSize();
        point.setLocation((double) point.x + dimension.getWidth() / 2D,
                (double) point.y + dimension.getHeight() / 2D);
        Point point1 = new Point(point.x - component1.getWidth() / 2, point.y - component1.getHeight() / 2);
        if (point1.y < 0)
            point1.y = 0;
        if (point1.x < 0)
            point1.y = 0;
        if (point1.x + component1.getWidth() > dimension1.width)
            point1.x = dimension1.width - component1.getWidth();
        if (point1.y + component1.getHeight() > dimension1.height)
            point1.y = dimension1.height - component1.getHeight();
        component1.setLocation(point1);
    }
}

Related

  1. packAndCenter(Window window)
  2. screenCenter(Window window)
  3. setCenter(Component comp)
  4. setCenter(Component comp, Component parent)
  5. setCenter(Component component)
  6. setCenteredCoordSystem(Graphics2D g, Component comp, double unitx, double unity)
  7. setCenteredInScreen(Component frame)
  8. setCenteredRectangle(int rectangleWidth, int rectangleHeight)
  9. setCenterLocation(final Window window)