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

java.awt.PointgetFrameCenteredLocation(final java.awt.Component centerThis, final Component parent)
Get the location for centered Dialog
java.awt.Dimension screenSize = (parent == null) ? java.awt.Toolkit.getDefaultToolkit().getScreenSize()
        : parent.getSize();
int x = (screenSize.width - centerThis.getWidth()) >> 1;
int y = (screenSize.height - centerThis.getHeight()) >> 1;
if (parent != null) {
    x += parent.getX();
    y += parent.getY();
if (x < 0)
    x = 0;
if (y < 0)
    y = 0;
return new java.awt.Point(x, y);
PointgetFrameOnCenterLocationPoint(Window window)
Returns the location of point of window, when it should be on center of the screen.
Dimension size = window.getSize();
int width = (int) ((Toolkit.getDefaultToolkit().getScreenSize().width / (double) 2)
        - (size.getWidth() / (double) 2));
int height = (int) ((Toolkit.getDefaultToolkit().getScreenSize().height / (double) 2)
        - (size.getHeight() / (double) 2));
return new Point(width, height);
PointgetLocationToCenterOnScreen(Component comp)
given any component, returns a location for the top left corner of the component that will center it on the screen
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));
floatgetPercentageOnScreen(Point location, Dimension size, Rectangle screen)
get Percentage On Screen
Rectangle frameBounds = new Rectangle(location, size);
Rectangle2D intersection = frameBounds.createIntersection(screen);
int frameArea = size.width * size.height;
int intersectionArea = (int) (intersection.getWidth() * intersection.getHeight());
float percentage = (float) intersectionArea / frameArea;
return percentage < 0 ? 0 : percentage;
voidlocateOnScreenCenter(Component component)
Locates the given component on the screen's center.
Dimension paneSize = component.getSize();
Dimension screenSize = component.getToolkit().getScreenSize();
component.setLocation((screenSize.width - paneSize.width) / 2, (screenSize.height - paneSize.height) / 2);
voidmoveToCenter(Component componentToMove, Component componentToCenterOver)
Centers componentToMove over componentToCenterOver.
try {
    int centerX, centerY;
    if (componentToCenterOver == null) {
        Point p = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
        centerX = p.x;
        centerY = p.y;
    } else {
        try {
...
voidmoveToScreenCenter(Window w)
move To Screen Center
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
w.setLocation((screen.width - w.getWidth()) / 2, (screen.height - w.getHeight()) / 2);
voidpackAndCenter(Window wind)
Packs and centers the windows
wind.pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = wind.getSize();
if (frameSize.height > screenSize.height) {
    frameSize.height = screenSize.height;
if (frameSize.width > screenSize.width) {
    frameSize.width = screenSize.width;
...
voidpackAndCenter(Window window)
packs a window and centers is onscreen
window.pack();
Dimension size = window.getSize();
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
window.setLocation((screen.width - size.width) / 2, (screen.height - size.height) / 2);
voidscreenCenter(Window window)
Center screen
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = window.getSize();
if (frameSize.height > screenSize.height) {
    frameSize.height = screenSize.height;
if (frameSize.width > screenSize.width) {
    frameSize.width = screenSize.width;
window.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);