/** * Applies this geometry to a window. Makes sure that the window is not placed outside of the * coordinate range of all available screens. * * @param window the window */ public void applySafe(Window window) { Point p = new Point(topLeft); Rectangle virtualBounds = new Rectangle(); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice[] gs = ge.getScreenDevices(); for (GraphicsDevice gd : gs) { if (gd.getType() == GraphicsDevice.TYPE_RASTER_SCREEN) { virtualBounds = virtualBounds.union(gd.getDefaultConfiguration().getBounds()); } } if (p.x < virtualBounds.x) { p.x = virtualBounds.x; } else if (p.x > virtualBounds.x + virtualBounds.width - extent.width) { p.x = virtualBounds.x + virtualBounds.width - extent.width; } if (p.y < virtualBounds.y) { p.y = virtualBounds.y; } else if (p.y > virtualBounds.y + virtualBounds.height - extent.height) { p.y = virtualBounds.y + virtualBounds.height - extent.height; } window.setLocation(p); window.setSize(extent); }
/** * Find the size and position of the screen for given coordinates. Use first screen, when no * coordinates are stored or null is passed. * * @param g coordinates to check * @return bounds of the screen */ private static Rectangle getScreenInfo(Rectangle g) { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice[] gs = ge.getScreenDevices(); int intersect = 0; Rectangle bounds = null; for (GraphicsDevice gd : gs) { if (gd.getType() == GraphicsDevice.TYPE_RASTER_SCREEN) { Rectangle b = gd.getDefaultConfiguration().getBounds(); if (b.height > 0 && b.width / b.height >= 3) /* multiscreen with wrong definition */ { b.width /= 2; Rectangle is = b.intersection(g); int s = is.width * is.height; if (bounds == null || intersect < s) { intersect = s; bounds = b; } b = new Rectangle(b); b.x += b.width; is = b.intersection(g); s = is.width * is.height; if (bounds == null || intersect < s) { intersect = s; bounds = b; } } else { Rectangle is = b.intersection(g); int s = is.width * is.height; if (bounds == null || intersect < s) { intersect = s; bounds = b; } } } } return bounds; }