Used for getting the actual bound of the monitor that contains Point p - Java 2D Graphics

Java examples for 2D Graphics:Screen

Description

Used for getting the actual bound of the monitor that contains Point p

Demo Code


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

public class Main {
    /**//from  w  w w . j  av  a  2 s.c  o  m
     * Used for getting the actual bound of the monitor that contains Point p
     *
     * @param p - point to check monitor for
     * @return - current monitor bounds
     */
    public static Rectangle getScreenBoundsForPoint(Point p) {
        Rectangle bounds;
        GraphicsEnvironment env = GraphicsEnvironment
                .getLocalGraphicsEnvironment();

        GraphicsConfiguration graphicsConfiguration = null;
        for (GraphicsDevice gd : GraphicsEnvironment
                .getLocalGraphicsEnvironment().getScreenDevices()) {
            if (gd.getDefaultConfiguration().getBounds().contains(p)) {
                graphicsConfiguration = gd.getDefaultConfiguration();
                break;
            }
        }
        if (graphicsConfiguration != null) {
            bounds = graphicsConfiguration.getBounds();
            Insets screenInsets = Toolkit.getDefaultToolkit()
                    .getScreenInsets(graphicsConfiguration);
            bounds.x += screenInsets.left;
            bounds.y += screenInsets.top;
            bounds.height -= screenInsets.bottom;
            bounds.width -= screenInsets.right;
        } else {
            bounds = env.getMaximumWindowBounds();
        }
        return (bounds);
    }
}

Related Tutorials