Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.awt.Component;

import java.awt.Dimension;

import java.awt.Point;

import java.awt.Toolkit;

public class Main {
    public static final double HALF = 2.0;

    public static void centralizeComponent(Component component, Component otherComponent) {

        Dimension othersDimension = null;
        Point othersLocation = null;
        if (otherComponent == null || !otherComponent.isVisible()) {
            othersDimension = Toolkit.getDefaultToolkit().getScreenSize();
            othersLocation = new Point(0, 0);
        } else {
            othersDimension = otherComponent.getSize();
            othersLocation = otherComponent.getLocation();
        }
        Point centerPoint = new Point(
                (int) Math.round(othersDimension.width / HALF - component.getWidth() / HALF) + othersLocation.x,
                (int) Math.round(othersDimension.height / HALF - component.getHeight() / HALF) + othersLocation.y);
        component.setLocation(centerPoint);
    }
}