com.databasesandlife.util.awt.WindowCenterer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-common Show documentation
Show all versions of java-common Show documentation
Utility classes developed at Adrian Smith Software (A.S.S.)
package com.databasesandlife.util.awt;
import java.awt.*;
/**
* Takes a Window (for example Frame or Dialog) and centers it on the screen.
*
* @author This source is copyright Adrian Smith and licensed under the LGPL 3.
* @see Project on GitHub
*/
public class WindowCenterer {
/** Takes a window and makes ot 0.75x the size of the screen, then centers it */
public void centerAndResizeToNearlyMaximized(Window window) {
var ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
var center = ge.getCenterPoint();
var bounds = ge.getMaximumWindowBounds();
var w = 3*bounds.width/4;
var h = 3*bounds.height/4;
var x = center.x - w/2;
var y = center.y - h/2;
window.setBounds(x, y, w, h);
window.validate();
}
/** Nearly-maximizes a Window in the center of the screen. If the window
* size is bigger than this v.big size, will be kept at that size, unless
* bigger than the size of the screen. */
public void centerBig(Window window) {
var ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
var center = ge.getCenterPoint();
var bounds = ge.getMaximumWindowBounds();
var w = Math.max(3*bounds.width/4, Math.min(window.getWidth(), bounds.width));
var h = Math.max(3*bounds.height/4, Math.min(window.getHeight(), bounds.height));
var x = center.x - w/2;
var y = center.y - h/2;
window.setBounds(x, y, w, h);
window.validate();
}
/** Takes a window and centers it. If bigger than screen, cuts it down
* to screen-size. If fits on screen, displayed at its desired size. */
public void center(Window window) {
var ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
var center = ge.getCenterPoint();
var bounds = ge.getMaximumWindowBounds();
var w = Math.min(window.getWidth(), bounds.width);
var h = Math.min(window.getHeight(), bounds.height);
var x = center.x - w/2;
var y = center.y - h/2;
window.setBounds(x, y, w, h);
window.validate();
}
}