/*
 * @author Ranides Atterwim {@literal <ranides@gmail.com>}
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.ui.helpers;

import lombok.experimental.UtilityClass;

import java.applet.Applet;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


/**
 * Utility method for AWT and Swing applications
 */
@UtilityClass
public final class AppletInvoker {

    /**
     * Helper method, opens new window {@link Window} and puts specified applet inside it.
     * @param applet applet
     * @param title window title
     * @param width size of window
     * @param height size of window
     * @return new window with aplet inside
     */
    public static Window run(final Applet applet, final String title, final int width, final int height) {
        final Frame frame = new Frame(title);
        // allow some extra room for the frame title bar.
        frame.setSize(width + 16, height + 36);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent event) {
                applet.stop();
                applet.destroy();
                System.exit(0);
            }
        });
        frame.add(applet);
        applet.init();
        frame.validate();
        frame.setVisible(true);
        applet.start();
        return frame;
    }
    
}
