Java Screen Center getLocationToCenterOnScreen(Component comp)

Here you can find the source of getLocationToCenterOnScreen(Component comp)

Description

given any component, returns a location for the top left corner of the component that will center it on the screen

License

Open Source License

Parameter

Parameter Description
comp the Component to find a center for

Return

Point the point at which component would be centered

Declaration

public static Point getLocationToCenterOnScreen(Component comp) 

Method Source Code

//package com.java2s;
// This software is licensed under the GNU General Public License,

import java.awt.Component;

import java.awt.Dimension;

import java.awt.Point;

public class Main {
    /**//from w ww . j a  va 2  s . c  o m
     * given any component, returns a location for the top left corner of the component that will center it on the
     * screen
     * 
     * @param comp
     *           the Component to find a center for
     * @return Point the point at which component would be centered
     */
    public static Point getLocationToCenterOnScreen(Component comp) {
        Dimension screenDim = comp.getToolkit().getScreenSize();
        Dimension compDim = comp.getSize();
        return new Point(Math.max(0, (screenDim.width - compDim.width) / 2),
                Math.max(0, (screenDim.height - compDim.height) / 2));
    }
}

Related

  1. getCenterPosition(Dimension d)
  2. getCenterPositionForComponent(int width, int heigth)
  3. getDimensionFromPercent(int percentWidth, int percentHeight)
  4. getFrameCenteredLocation(final java.awt.Component centerThis, final Component parent)
  5. getFrameOnCenterLocationPoint(Window window)
  6. getPercentageOnScreen(Point location, Dimension size, Rectangle screen)
  7. locateOnScreenCenter(Component component)
  8. moveToCenter(Component componentToMove, Component componentToCenterOver)
  9. moveToScreenCenter(Window w)