Java JComponent Size getCentralLocation(Component sourceComp, Component comp, Dimension size)

Here you can find the source of getCentralLocation(Component sourceComp, Component comp, Dimension size)

Description

get Central Location

License

Open Source License

Declaration

public static Rectangle getCentralLocation(Component sourceComp, Component comp, Dimension size) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.SwingUtilities;

public class Main {
    public static Rectangle getCentralLocation(Component comp) {
        return getCentralLocation(null, comp, comp.getPreferredSize());
    }//from  w  ww .j ava2 s. c  o m

    public static Rectangle getCentralLocation(Component sourceComp, Component comp, Dimension size) {
        final Rectangle position;

        if (sourceComp != null) {
            Point compLocation = new Point(0, 0);

            SwingUtilities.convertPointToScreen(compLocation, sourceComp);

            GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice[] devices = e.getScreenDevices();
            Rectangle displayBounds = null;

            // now get the configurations for each device
            for (GraphicsDevice device : devices) {
                GraphicsConfiguration[] configurations = device.getConfigurations();

                for (GraphicsConfiguration config : configurations) {
                    Rectangle gcBounds = config.getBounds();

                    if (gcBounds.contains(compLocation)) {
                        displayBounds = gcBounds;
                    }
                }
            }

            if (displayBounds == null) {
                position = getLocationFromMouse(size);
            } else {
                int x = (displayBounds.width - size.width) / 2;
                int y = (displayBounds.height - size.height) / 2;

                position = new Rectangle(x, y, size.width, size.height);
            }
        } else {
            position = getLocationFromMouse(size);
        }

        return position;

    }

    public static Rectangle getLocationFromMouse(Dimension panelPrefSize_) {
        GraphicsDevice device = MouseInfo.getPointerInfo().getDevice();
        Dimension screensize = new Dimension(device.getDisplayMode().getWidth(),
                device.getDisplayMode().getHeight());
        int x = (screensize.width - panelPrefSize_.width) / 2;
        int y = (screensize.height - panelPrefSize_.height) / 2;

        return new Rectangle(x, y, panelPrefSize_.width, panelPrefSize_.height);
    }
}

Related

  1. fixSize(JComponent c, Dimension d)
  2. fixSize(JComponent... items)
  3. fixSize(T comp, int width)
  4. forceSize(JComponent component, int width, int height)
  5. freezeSize(JComponent c, Dimension s)
  6. getComponentOfSameSize(final JComponent c)
  7. getLocalScreenSize(Component invoker)
  8. getMaxSize(JComponent[] components)
  9. getOptimalScreenSize(Container container, Dimension currentDim)