Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.awt.*;

import javax.swing.*;

public class Main {
    private static int windowXCenter = -1;
    private static int windowYCenter = -1;

    /**
     * Returns a window with a partially opaque progress Icon. Sets the opacity, location and size (to the given icon
     * size) (does not pack the image)
     *
     * @param icon the icon to set in the progress window
     * @param opacity a float value from 0-1
     * @param x the x location of the window
     * @param y the y location of the window
     * @return a jWindow of the progress wheel
     */
    public static JWindow getProgressWheelWindow(final Icon icon, final Float opacity, final int x, final int y) {
        JWindow jWindow = new JWindow() {

            {
                setOpacity(opacity);
                setLocation(x, y);
                setSize(icon.getIconWidth(), icon.getIconHeight());
                add(new JLabel(icon));
            }
        };

        return jWindow;
    }

    /**
     * Convenience method. Returns a window with a partially opaque progress Icon in the center of the screen
     *
     * @param icon the icon to set in the progress window
     * @return a jWindow of the progress wheel
     */
    public static JWindow getProgressWheelWindow(final Icon icon) {
        JWindow window = getProgressWheelWindow(icon, .85f, 0, 0);
        //It's faster and simpler just to set the location to 0, 0 and then call center and pack on it
        centerAndPack(window);
        return window;
    }

    /**
     * This method packs, sets the size, and sets the position of the window given
     *
     * @param window
     */
    public static void centerAndPack(Window window) {
        window.pack();
        int x = getWindowXCenter() - (window.getWidth() / 2);
        int y = getWindowYCenter() - (window.getHeight() / 2);
        window.setLocation(x, y);
        window.setMinimumSize(new Dimension(window.getWidth(), window.getHeight()));
    }

    /**
     * Don't use this method unless you want all the centerAndPack calls to set the center of the window somewhere other
     * than the center!
     *
     * @return the windowXCenter
     */
    public static int getWindowXCenter() {
        if (windowXCenter == -1) {
            windowXCenter = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2;
        }
        return windowXCenter;
    }

    /**
     * @return the windowYCenter
     */
    public static int getWindowYCenter() {
        if (windowYCenter == -1) {
            windowYCenter = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 2;
        }
        return windowYCenter;
    }
}