summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/util/GuiOrganizer.java
blob: 55cc20307108ee8ad6fc0bef95a13cb06a09a03d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package util;

import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Rectangle;
import java.awt.Window;


import javax.swing.JOptionPane;

/**
 * An abstract class to organize the GUI.
 * Currently only provide a method for centering Window-objects.
 */
public abstract class GuiOrganizer {

	/**
	 * The rectangle representing the bounds of the primary display
	 */
	private static Rectangle rect = null;

	/**
	 * Gets the bounds of the primary display using
	 * AWT's GraphicsEnvironment class.
	 */
	private static boolean getDisplayBounds() {
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		GraphicsDevice gd = null;
		try {
			gd = ge.getDefaultScreenDevice();
		} catch (HeadlessException he) {
			he.printStackTrace();
			JOptionPane.showMessageDialog(null, "Konnte kein Display ermittelt werden.",
					"Fehler", JOptionPane.ERROR_MESSAGE);
			return false;
		}

		GraphicsConfiguration gc = gd.getDefaultConfiguration();
		rect = gc.getBounds();

		if (rect == null) {
			JOptionPane.showMessageDialog(null, "Konnte die Resolution des Bildschirms nicht ermitteln!",
					"Fehler", JOptionPane.ERROR_MESSAGE);
			return false;
		} else {
			return true;
		}
	}

	/**
	 * Centers the given Window within the bounds of the display
	 * @param gui The Window object to be centered.
	 */
	public static void centerGUI(Window gui) {

		if (rect == null) getDisplayBounds();

		double width = rect.getWidth();
		double height = rect.getHeight();
		double xPosition = (width / 2 - gui.getWidth() / 2);
		double yPosition = (height / 2 - gui.getHeight() / 2);
		gui.setLocation((int) xPosition, (int) yPosition);
	}
}