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.Component;
import java.awt.Dimension;

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Window;
import java.awt.geom.Rectangle2D;

public class Main {
    /**
     * A version of setLocationRelativeTo() that takes the maximum window bounds into account
     * (taskbar).
     */
    public static void setLocationRelativeTo(Window window, Component component) {
        window.setLocationRelativeTo(component);
        Point screenLocation = getCurrentScreenLocation(window);
        if (window.getX() < screenLocation.x)
            window.setLocation(screenLocation.x, window.getY());
        if (window.getY() < screenLocation.y)
            window.setLocation(window.getX(), screenLocation.y);
    }

    public static Point getCurrentScreenLocation(Window window) {
        GraphicsDevice currentScreen = getCurrentScreen(window);
        if (currentScreen == GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()) {
            return GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().getLocation();
        }
        return currentScreen.getDefaultConfiguration().getBounds().getLocation();
    }

    public static GraphicsDevice getCurrentScreen(Window window) {
        return getCurrentScreen(window.getLocation(), window.getSize());
    }

    public static GraphicsDevice getCurrentScreen(Point location, Dimension size) {
        GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();

        GraphicsDevice bestMatch = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        float bestPercentage = 0;
        for (GraphicsDevice device : devices) {
            Rectangle bounds = device.getDefaultConfiguration().getBounds();
            float percentage = getPercentageOnScreen(location, size, bounds);

            if (percentage > bestPercentage) {
                bestMatch = device;
                bestPercentage = percentage;
            }
        }
        return bestMatch;
    }

    private static float getPercentageOnScreen(Point location, Dimension size, Rectangle screen) {
        Rectangle frameBounds = new Rectangle(location, size);
        Rectangle2D intersection = frameBounds.createIntersection(screen);
        int frameArea = size.width * size.height;
        int intersectionArea = (int) (intersection.getWidth() * intersection.getHeight());
        float percentage = (float) intersectionArea / frameArea;
        return percentage < 0 ? 0 : percentage;
    }
}