Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.awt.Point;

public class Main {
    /**
     * Centers the Frame on the screen
     */
    public static void center(java.awt.Window frame) {
        // Get the size of the screen
        java.awt.Dimension dim = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        Point _loc = center(dim, frame);
        // Move the window
        frame.setLocation(_loc);
    }

    /**
     * gets the center point for "what" relative to the given Dimension dim 
     * @param dim
     * @param what
     */
    private static Point center(java.awt.Dimension dim, java.awt.Window what) {
        // Determine the new location of the window
        int w = what.getSize().width;
        int h = what.getSize().height;
        int x = (dim.width - w) / 2;
        int y = (dim.height - h) / 2;
        return new Point(x, y);
    }
}