Example usage for java.awt GraphicsEnvironment getCenterPoint

List of usage examples for java.awt GraphicsEnvironment getCenterPoint

Introduction

In this page you can find the example usage for java.awt GraphicsEnvironment getCenterPoint.

Prototype

public Point getCenterPoint() throws HeadlessException 

Source Link

Document

Returns the Point where Windows should be centered.

Usage

From source file:Main.java

/**
 * Centers the frame on the screen and sets its bounds. THis method should be
 * used after you call frame.pack() or frame.setSize().
 * @param frame/*from ww w .  j  av a 2s .  co  m*/
 */
public static void centerFrame(JFrame frame) {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    Point center = ge.getCenterPoint();
    Rectangle bounds = ge.getMaximumWindowBounds();
    int w = Math.max(bounds.width / 2, Math.min(frame.getWidth(), bounds.width));
    int h = Math.max(bounds.height / 2, Math.min(frame.getHeight(), bounds.height));
    int x = center.x - w / 2, y = center.y - h / 2;

    frame.setBounds(x, y, w, h);

    if ((w == bounds.width) && (h == bounds.height)) {
        frame.setExtendedState(Frame.MAXIMIZED_BOTH);
    }

    frame.validate();
}

From source file:Main.java

private void centerFrame() {
    Dimension windowSize = getSize();
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    Point centerPoint = ge.getCenterPoint();

    int dx = centerPoint.x - windowSize.width / 2;
    int dy = centerPoint.y - windowSize.height / 2;
    setLocation(dx, dy);// w w  w  .  j  ava2  s .  c  om
}

From source file:musiccrawler.App.java

private void configPositionApp() {
    Dimension windowSize = getSize();
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    Point centerPoint = ge.getCenterPoint();
    int dx = centerPoint.x - windowSize.width / 2;
    int dy = centerPoint.y - windowSize.height / 2;
    setLocation(dx, dy);/*from   ww w .  j av  a 2s  . co m*/
}