Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

public class Main {
    /**
     * Returns the size available from given point to in component to bottom right of component screen.
     */
    public static Dimension getScreenSizeAvailable(Component aComp, int anX, int aY) {
        Rectangle rect = getScreenBounds(aComp, anX, aY, true);
        Point sp = getScreenLocation(aComp, anX, aY);
        return new Dimension(rect.x + rect.width - sp.x, rect.y + rect.height - sp.y);
    }

    /**
     * Returns the screen bounds for a component location (or screen location if component null).
     */
    public static Rectangle getScreenBounds(Component aComp, int anX, int aY, boolean doInset) {
        GraphicsConfiguration gc = getGraphicsConfiguration(aComp, anX, aY);
        Rectangle rect = gc != null ? gc.getBounds() : new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        Insets ins = doInset && gc != null ? Toolkit.getDefaultToolkit().getScreenInsets(gc) : null;
        if (ins != null) {
            int lt = ins.left, rt = ins.right, tp = ins.top, bt = ins.bottom;
            rect.setBounds(rect.x + lt, rect.y + tp, rect.width - lt - rt, rect.height - tp - bt);
        }
        return rect;
    }

    /**
     * Returns the screen location for a component and X and Y.
     */
    private static Point getScreenLocation(Component aComp, int anX, int aY) {
        Point point = aComp != null && aComp.isShowing() ? aComp.getLocationOnScreen() : new Point();
        point.x += anX;
        point.y += aY;
        return point;
    }

    /**
     * Returns the GraphicsConfiguration for a point.
     */
    public static GraphicsConfiguration getGraphicsConfiguration(Component aComp, int anX, int aY) {
        // Get initial GC from component (if available) and point on screen
        GraphicsConfiguration gc = aComp != null ? aComp.getGraphicsConfiguration() : null;
        Point spoint = getScreenLocation(aComp, anX, aY);

        // Replace with alternate GraphicsConfiguration if point on another screen
        for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
            if (gd.getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
                GraphicsConfiguration dgc = gd.getDefaultConfiguration();
                if (dgc.getBounds().contains(spoint.x, spoint.y)) {
                    gc = dgc;
                    break;
                }
            }
        }

        // Return GraphicsConfiguration
        return gc;
    }
}