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 Point moveToScreenCenter(Component component) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension componentSize = component.getSize();
        int newComponentX = screenSize.width - componentSize.width;
        if (newComponentX >= 0)
            newComponentX = newComponentX / 2;
        else
            newComponentX = 0;
        int newComponentY = screenSize.height - componentSize.height;
        if (newComponentY >= 0)
            newComponentY = newComponentY / 2;
        else
            newComponentY = 0;
        Point location = new Point(newComponentX, newComponentY);
        component.setLocation(location);
        return location;
    }
}