Java examples for Swing:Screen
get Screen Rectangle
//package com.java2s; import java.awt.*; public class Main { public static Rectangle getScreenRectangle(int x, int y) { return getScreenRectangle(new Point(x, y)); }// ww w .j a v a 2s. c o m public static Rectangle getScreenRectangle(Point p) { final GraphicsEnvironment env = GraphicsEnvironment .getLocalGraphicsEnvironment(); final GraphicsDevice[] devices = env.getScreenDevices(); double distance = -1; GraphicsConfiguration targetGC = null; GraphicsConfiguration bestConfig = null; for (GraphicsDevice device : devices) { final GraphicsConfiguration config = device .getDefaultConfiguration(); final Rectangle rect = config.getBounds(); final Insets insets = getScreenInsets(config); if (insets != null) { rect.x += insets.left; rect.width -= (insets.left + insets.right); rect.y += insets.top; rect.height -= (insets.top + insets.bottom); } if (rect.contains(p)) { targetGC = config; break; } else { final double d = findNearestPointOnBorder(rect, p) .distance(p.x, p.y); if (bestConfig == null || distance > d) { distance = d; bestConfig = config; } } } if (targetGC == null && devices.length > 0 && bestConfig != null) { targetGC = bestConfig; //targetGC = env.getDefaultScreenDevice().getDefaultConfiguration(); } if (targetGC == null) { throw new IllegalStateException( "It's impossible to determine target graphics environment for point (" + p.x + "," + p.y + ")"); } // determine real client area of target graphics configuration final Insets insets = getScreenInsets(targetGC); final Rectangle targetRect = targetGC.getBounds(); targetRect.x += insets.left; targetRect.y += insets.top; targetRect.width -= insets.left + insets.right; targetRect.height -= insets.top + insets.bottom; return targetRect; } public static Insets getScreenInsets(final GraphicsConfiguration gc) { return Toolkit.getDefaultToolkit().getScreenInsets(gc); } public static Point findNearestPointOnBorder(Rectangle rect, Point p) { final int x0 = rect.x; final int y0 = rect.y; final int x1 = x0 + rect.width; final int y1 = y0 + rect.height; double distance = -1; Point best = null; final Point[] variants = { new Point(p.x, y0), new Point(p.x, y1), new Point(x0, p.y), new Point(x1, p.y) }; for (Point variant : variants) { final double d = variant.distance(p.x, p.y); if (best == null || distance > d) { best = variant; distance = d; } } assert best != null; return best; } }