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

voidsetCenter(Component comp)
Center a component on the screen.
Dimension screenSize = comp.getToolkit().getScreenSize();
Dimension frameSize = comp.getSize();
if (frameSize.height > screenSize.height)
    frameSize.height = screenSize.height;
if (frameSize.width > screenSize.width)
    frameSize.width = screenSize.width;
comp.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
voidsetCenter(Component comp, Component parent)
Center a component within a parental component.
if (parent == null) {
    setCenter(comp);
    return;
Dimension dlgSize = comp.getPreferredSize();
Dimension frmSize = parent.getSize();
Point loc = parent.getLocation();
if (dlgSize.width < frmSize.width && dlgSize.height < frmSize.height)
...
voidsetCenter(Component component)
set Center
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
component.setLocation(screenSize.width / 2 - component.getWidth() / 2,
        screenSize.height / 2 - component.getHeight() / 2);
voidsetCenter(Component component, Component component1)
set Center
Point point;
Dimension dimension;
if (component != null) {
    point = component.getLocation();
    dimension = component.getSize();
} else {
    dimension = Toolkit.getDefaultToolkit().getScreenSize();
    point = new Point(0, 0);
...
voidsetCenteredCoordSystem(Graphics2D g, Component comp, double unitx, double unity)
sets the coordinate system with given units and origin at the centre of the component in question.
int dx = comp.getWidth();
int dy = comp.getHeight();
g.translate(dx / 2, dy / 2);
g.scale(unitx, -unity);
voidsetCenteredInScreen(Component frame)
set Centered In Screen
setCenteredInScreen(frame, -1);
RectanglesetCenteredRectangle(int rectangleWidth, int rectangleHeight)
set Centered Rectangle
Dimension screenDimensions = Toolkit.getDefaultToolkit().getScreenSize();
int xCenter = (screenDimensions.width - rectangleWidth) / 2;
int yCenter = (screenDimensions.height - rectangleHeight) / 2;
return new Rectangle(xCenter, yCenter, rectangleWidth, rectangleHeight);
voidsetCenterLocation(final Window window)
set Center Location
final Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
window.setLocation(dim.width / 2 - window.getSize().width / 2,
        dim.height / 2 - window.getSize().height / 2);
voidsetCenterPosition(final Frame frame)
Set the position of the frame to the center of the screen
final Dimension paneSize = frame.getSize();
final Dimension screenSize = frame.getToolkit().getScreenSize();
frame.setLocation((screenSize.width - paneSize.width) / 2, (screenSize.height - paneSize.height) / 2);
voidsetComponentLocationOnCenter(Component component)
Centers the specified component based on the user main screen dimensions and resolution.
int screenID = getScreenID(component);
Dimension dim = getScreenDimension(screenID);
final int x = (dim.width - component.getWidth()) / 2;
final int y = (dim.height - component.getHeight()) / 2;
component.setLocation(x, y);