Java Screen Center getCenterPosition(Dimension d)

Here you can find the source of getCenterPosition(Dimension d)

Description

Calculates x,y to center window of given dimension on the desktop

License

Open Source License

Parameter

Parameter Description
d Dimension object used (along with the size of the screen) to determine the center point location.

Return

Point

Declaration

public static Point getCenterPosition(Dimension d) 

Method Source Code

//package com.java2s;

import java.awt.*;

public class Main {
    /**/*w ww . j a  v a2 s . c  om*/
     * Calculates x,y to center window of given dimension on the desktop
     * 
     * @param d Dimension object used (along with the size of the screen) to determine the center point location.
     *
     * @return Point 
     */
    public static Point getCenterPosition(Dimension d) {
        Point retval = new Point(0, 0);

        try {
            Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
            int x = (screen.width / 2) - (d.width / 2);
            int y = (screen.height / 2) - (d.height / 2);
            retval = new Point(x, y);
        } catch (Exception e) {
            retval = new Point(0, 0);
        }

        return retval;
    }
}

Related

  1. getCenterLocationAWT( final java.awt.Component comp, final boolean onTitleBarIfWindow)
  2. getCenterPoint()
  3. getCenterPoint(Component parent, Component comp)
  4. getCenterPoint(final int w, final int h)
  5. getCenterPointRelativeToScreen(Dimension size)
  6. getCenterPositionForComponent(int width, int heigth)
  7. getDimensionFromPercent(int percentWidth, int percentHeight)
  8. getFrameCenteredLocation(final java.awt.Component centerThis, final Component parent)
  9. getFrameOnCenterLocationPoint(Window window)