Java examples for Swing:Screen
move Rectangle To Fit The Screen
//package com.java2s; import java.awt.*; public class Main { public static void moveRectangleToFitTheScreen(Rectangle aRectangle) { int screenX = aRectangle.x + aRectangle.width / 2; int screenY = aRectangle.y + aRectangle.height / 2; Rectangle screen = getScreenRectangle(screenX, screenY); moveToFit(aRectangle, screen, null); }/*from w w w . ja v a2 s . c o m*/ public static Rectangle getScreenRectangle(int x, int y) { return getScreenRectangle(new Point(x, y)); } 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 void moveToFit(final Rectangle rectangle, final Rectangle container, Insets padding) { Insets insets = padding != null ? padding : new Insets(0, 0, 0, 0); Rectangle move = new Rectangle(rectangle.x - insets.left, rectangle.y - insets.top, rectangle.width + insets.left + insets.right, rectangle.height + insets.top + insets.bottom); if (move.getMaxX() > container.getMaxX()) { move.x = (int) container.getMaxX() - move.width; } if (move.getMinX() < container.getMinX()) { move.x = (int) container.getMinX(); } if (move.getMaxY() > container.getMaxY()) { move.y = (int) container.getMaxY() - move.height; } if (move.getMinY() < container.getMinY()) { move.y = (int) container.getMinY(); } rectangle.x = move.x + insets.left; rectangle.y = move.y + insets.right; rectangle.width = move.width - insets.left - insets.right; rectangle.height = move.height - insets.top - insets.bottom; } 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; } }