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;
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);
+ }
+
}