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.Rectangle;
import java.awt.Toolkit;
import java.awt.Window;

public class Main {
    public static void centerWithinScreen(final Window wind) {
        if (wind == null) {
            return;
        }
        final Toolkit toolKit = Toolkit.getDefaultToolkit();
        final Rectangle rcScreen = new Rectangle(toolKit.getScreenSize());
        final Dimension windSize = wind.getSize();
        final Dimension parentSize = new Dimension(rcScreen.width, rcScreen.height);
        if (windSize.height > parentSize.height) {
            windSize.height = parentSize.height;
        }
        if (windSize.width > parentSize.width) {
            windSize.width = parentSize.width;
        }
        center(wind, rcScreen);
    }

    private static void center(final Component wind, final Rectangle rect) {
        if ((wind == null) || (rect == null)) {
            return;
        }
        final Dimension windSize = wind.getSize();
        final int x = ((rect.width - windSize.width) / 2) + rect.x;
        int y = ((rect.height - windSize.height) / 2) + rect.y;
        if (y < rect.y) {
            y = rect.y;
        }
        wind.setLocation(x, y);
    }
}