Java Utililty Methods Screen Center

List of utility methods to do Screen Center

Description

The list of methods to do Screen Center are organized into topic(s).

Method

PointgetCenterLocation(final Window frame)
get Center Location
int x = (int) (Math.rint(Toolkit.getDefaultToolkit().getScreenSize().getWidth() - frame.getWidth()) / 2);
int y = (int) (Math.rint(Toolkit.getDefaultToolkit().getScreenSize().getHeight() - frame.getHeight()) / 2);
return new Point(x, y);
PointgetCenterLocation(Window window)
get Center Location
Dimension componentSize = window.getSize();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Point pos = new Point();
pos.x = ((screenSize.width / 2) - (componentSize.width / 2));
pos.y = ((screenSize.height / 2) - (componentSize.height / 2)) - 30;
return pos;
int[]getCenterLocationAWT(final java.awt.Component comp, final boolean onTitleBarIfWindow)
get Center Location AWT
int x0, y0;
final java.awt.Point p0 = comp.getLocationOnScreen();
final java.awt.Rectangle r0 = comp.getBounds();
if (onTitleBarIfWindow && comp instanceof java.awt.Window) {
    final java.awt.Window window = (java.awt.Window) comp;
    final java.awt.Insets insets = window.getInsets();
    y0 = (int) (p0.getY() + insets.top / 2.0 + .5);
} else {
...
PointgetCenterPoint()
get Center Point
Toolkit toolkit = Toolkit.getDefaultToolkit();
double desktopHeight = toolkit.getScreenSize().getHeight();
double desktopWidth = toolkit.getScreenSize().getWidth();
return new Point((int) desktopWidth / 2, (int) desktopHeight / 2);
PointgetCenterPoint(Component parent, Component comp)
get Center Point
Dimension cSize = comp.getSize();
Dimension pSize = parent.getSize();
Point loc = parent.getLocationOnScreen();
loc.x = ((pSize.width - cSize.width) / 2) + loc.x;
loc.y = ((pSize.height - cSize.height) / 2) + loc.y;
if (loc.x < 0) {
    loc.x = 0;
if (loc.y < 0) {
    loc.y = 0;
return loc;
PointgetCenterPoint(final int w, final int h)
get Center Point
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
final int x = (int) (d.getWidth() / 2) - (w / 2);
final int y = (int) (d.getHeight() / 2) - (h / 2);
return new Point(x, y);
PointgetCenterPointRelativeToScreen(Dimension size)
Gives the center point relative to the user's screen, given the size of some component.
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
return new Point((screenSize.width - size.width) / 2, (screenSize.height - size.height) / 2);
PointgetCenterPosition(Dimension d)
Calculates x,y to center window of given dimension on the desktop
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);
...
PointgetCenterPositionForComponent(int width, int heigth)
get Center Position For Component
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int positionX = screenSize.width - (screenSize.width / 2) - (width / 2);
int positionY = screenSize.height - (screenSize.height / 2) - (heigth / 2);
return new Point(positionX, positionY);
DimensiongetDimensionFromPercent(int percentWidth, int percentHeight)
Return a Dimension whose size is defined not in terms of pixels, but in terms of a given percent of the screen's width and height.
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
return calcDimensionFromPercent(screenSize, percentWidth, percentHeight);