package com.srbenoit.ui;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;

/**
 * A set of utilities for user interface construction.
 */
public final class UIUtilities {

    /**
     * Private constructor to prevent instantiation.
     */
    private UIUtilities() {

        // No action
    }

    /**
     * Gets a <code>Graphics2D</code> object that is compatible with the current desktop.
     *
     * @return  the <code>Graphics2D</code>
     */
    public static Graphics2D getGraphics() {

        GraphicsEnvironment env;
        BufferedImage img;

        env = GraphicsEnvironment.getLocalGraphicsEnvironment();

        img = env.getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(1, 1);

        return env.createGraphics(img);
    }

    /**
     * Places a component (typically a frame or dialog) at a particular position within the desktop.
     *
     * @param  frame  the frame to position
     * @param  xPos   the x position (0.0 is left, 1.0 is right, 0.5 is centered)
     * @param  yPos   the y position (0.0 is top, 1.0 is bottom, 0.5 is centered)
     */
    public static void positionFrame(final Component frame, final double xPos, final double yPos) {

        Dimension screen;
        Dimension size;
        int xPixel;
        int yPixel;

        screen = Toolkit.getDefaultToolkit().getScreenSize();
        size = frame.getSize();

        xPixel = (int) ((screen.width - size.width) * xPos);
        yPixel = (int) ((screen.height - size.height) * yPos);

        frame.setLocation(xPixel, yPixel);
    }
}
