X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Futil%2Fswing%2FSwingUtils.java;h=b5300441e5fe2d2f568b63b485349e0a3a90d81b;hb=8eab1a3975c251c4820988f3fdecbf9b113d9171;hp=b1077766f27f4dba9debe1896a8f442f6a5a93bb;hpb=23aa0ec9da5f8a3e2eefaf2343bc68b88a29096a;p=jSite2.git diff --git a/src/net/pterodactylus/util/swing/SwingUtils.java b/src/net/pterodactylus/util/swing/SwingUtils.java index b107776..b530044 100644 --- a/src/net/pterodactylus/util/swing/SwingUtils.java +++ b/src/net/pterodactylus/util/swing/SwingUtils.java @@ -20,6 +20,8 @@ package net.pterodactylus.util.swing; import java.awt.Dimension; +import java.awt.Point; +import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.Window; @@ -27,10 +29,21 @@ import java.awt.Window; * Helper utilities for working with Swing. * * @author David ‘Bombe’ Roden <bombe@freenetproject.org> - * @version $Id$ */ public class SwingUtils { + /** The screen size. */ + private static Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize(); + + /** + * Returns the size of the screen. + * + * @return The size of the screen + */ + public static Dimension getScreenSize() { + return new Dimension(screenDimension); + } + /** * Centers the given window on the screen. * @@ -38,9 +51,33 @@ public class SwingUtils { * The window to center */ public static void center(Window window) { - Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameDimension = window.getSize(); window.setLocation((screenDimension.width - frameDimension.width) >> 1, (screenDimension.height - frameDimension.height) >> 1); } + /** + * {@link Window#pack() Packs} the given window and positions it so that its + * center stays the same. + * + * @param window + * The window to pack and recenter + */ + public static void repackCentered(Window window) { + Point center = getCenter(window.getBounds()); + window.pack(); + window.setLocation((center.x - window.getWidth() / 2), (center.y - window.getHeight() / 2)); + window.repaint(); + } + + /** + * Returns the center of the given rectangle. + * + * @param bounds + * The rectangle which center to get + * @return The center of the rectangle + */ + private static Point getCenter(Rectangle bounds) { + return new Point(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2); + } + }