/** * Positions the specified dialog at a position relative to its parent. * * @param dialog the dialog to be positioned. * @param horizontalPercent the relative location. * @param verticalPercent the relative location. */ public static void positionDialogRelativeToParent( final Dialog dialog, final double horizontalPercent, final double verticalPercent) { final Container parent = dialog.getParent(); if (parent == null || (parent.isVisible() == false)) { positionFrameOnScreen(dialog, horizontalPercent, verticalPercent); return; } final Dimension d = dialog.getSize(); final Dimension p = parent.getSize(); final int baseX = parent.getX(); final int baseY = parent.getY(); final int parentPointX = baseX + (int) (horizontalPercent * p.width); final int parentPointY = baseY + (int) (verticalPercent * p.height); final int dialogPointX = parentPointX - (int) (horizontalPercent * d.width); final int dialogPointY = parentPointY - (int) (verticalPercent * d.height); // make sure the dialog fits completely on the screen... final Rectangle s = parent.getGraphicsConfiguration().getBounds(); final Rectangle r = new Rectangle(dialogPointX, dialogPointY, d.width, d.height); final Rectangle intersectedDialogBounds = r.intersection(s); if (intersectedDialogBounds.width < d.width) { r.x = s.width - d.width; r.width = d.width; } if (intersectedDialogBounds.height < d.height) { r.y = s.height - d.height; r.height = d.height; } final Rectangle finalIntersection = r.intersection(s); dialog.setBounds(finalIntersection); }
/** * Positions the specified frame at a random location on the screen while ensuring that the entire * frame is visible (provided that the frame is smaller than the screen). * * @param frame the frame. */ public static void positionFrameRandomly(final Window frame) { //noinspection UnsecureRandomNumberGeneration positionFrameOnScreen(frame, Math.random(), Math.random()); }
/** * Positions the specified frame in the middle of the screen. * * @param frame the frame to be centered on the screen. */ public static void centerFrameOnScreen(final Window frame) { positionFrameOnScreen(frame, 0.5, 0.5); }