Rectangle (int x, int y) { if (GraphicsEnvironment.getLocalGraphicsEnvironment().isHeadlessInstance()) { return new Rectangle(x, y, 0, 0); } GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices(); if (devices.length == 0) { return new Rectangle(x, y, 0, 0); } if (devices.length == 1) { return getScreenRectangle(devices[0]); } Rectangle[] rectangles = new Rectangle[devices.length]; for (int i = 0; i < devices.length; i++) { GraphicsConfiguration configuration = devices[i].getDefaultConfiguration(); Rectangle bounds = configuration.getBounds(); rectangles[i] = applyInsets(bounds, getScreenInsets(configuration)); if (bounds.contains(x, y)) { return rectangles[i]; } } Rectangle bounds = rectangles[0]; int minimum = distance(bounds, x, y); if (bounds.width == 0 || bounds.height == 0) { //Screen is invalid, give maximum score minimum = Integer.MAX_VALUE; } for (int i = 1; i < rectangles.length; i++) { if (rectangles[i].width == 0 || rectangles[i].height == 0) { //Screen is invalid continue; } int distance = distance(rectangles[i], x, y); if (minimum > distance) { minimum = distance; bounds = rectangles[i]; } } if (bounds.width == 0 || bounds.height == 0) { //All screens were invalid, return sensible default return new Rectangle(x, y, 0, 0); } return bounds; }