Example usage for java.awt Toolkit getScreenSize

List of usage examples for java.awt Toolkit getScreenSize

Introduction

In this page you can find the example usage for java.awt Toolkit getScreenSize.

Prototype

public abstract Dimension getScreenSize() throws HeadlessException;

Source Link

Document

Gets the size of the screen.

Usage

From source file:Main.java

/** Repositions a frame to be centered on the screen */
public static void center(Dialog frame) {
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension screen = tk.getScreenSize();
    Dimension win = frame.getSize();
    //frame.setSize(screenWidth / 2, screenHeight / 2);
    frame.setLocation((screen.width - win.width) / 2, (screen.height - win.height) / 2);
}

From source file:Main.java

/** Repositions a frame to be centered on the screen */
public static void center(Frame frame) {
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension screen = tk.getScreenSize();
    Dimension win = frame.getSize();
    //frame.setSize(screenWidth / 2, screenHeight / 2);
    frame.setLocation((screen.width - win.width) / 2, (screen.height - win.height) / 2);
}

From source file:Main.java

public static void centerComponent(Component relativeTo, Component toCenter) {
    if (relativeTo == null) {
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension screenSize = tk.getScreenSize();
        int screenHeight = screenSize.height;
        int screenWidth = screenSize.width;
        toCenter.setLocation((screenWidth / 2) - (toCenter.getSize().width / 2),
                (screenHeight / 2) - (toCenter.getSize().height / 2));
    } else {//www. j a  v a  2 s  .com
        Point loc = relativeTo.getLocationOnScreen();
        Rectangle bounds = relativeTo.getBounds();
        toCenter.setLocation((int) (loc.x + bounds.getWidth() / 2) - (toCenter.getWidth() / 2),
                (int) (loc.y + bounds.getHeight() / 2) - (toCenter.getHeight() / 2));

    }
}

From source file:Main.java

public static void moveToCenter(JFrame frame) {
    int windowWidth = frame.getWidth();
    int windowHeight = frame.getHeight();
    Toolkit kit = Toolkit.getDefaultToolkit();
    Dimension screenSize = kit.getScreenSize();
    int screenWidth = screenSize.width;
    int screenHeight = screenSize.height;
    frame.setLocation(screenWidth / 2 - windowWidth / 2, screenHeight / 2 - windowHeight / 2);
}

From source file:Main.java

public static void centerFrameOnScreen(JFrame frame) {

    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Dimension screenSize = toolkit.getScreenSize();
    Dimension frameSize = frame.getSize();
    if (frameSize.height > screenSize.height) {
        frameSize.height = screenSize.height;
    }/*from  w w  w  .  j  a va  2  s  .  c o m*/
    if (frameSize.width > screenSize.width) {
        frameSize.width = screenSize.width;
    }
    Point p = new Point((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    frame.setLocation(p);
}

From source file:Main.java

public static void centerWindowOnScreen(Window window) {
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension screenSize = tk.getScreenSize();
    window.setLocation((int) (screenSize.getWidth() / 2 - window.getWidth() / 2),
            (int) (screenSize.getHeight() / 2 - window.getHeight() / 2));
}

From source file:Main.java

public static void centerWithinScreen(final Window wind) {
    if (wind == null) {
        return;//from   w w  w  .  j  ava 2  s  . c om
    }
    final Toolkit toolKit = Toolkit.getDefaultToolkit();
    final Rectangle rcScreen = new Rectangle(toolKit.getScreenSize());
    final Dimension windSize = wind.getSize();
    final Dimension parentSize = new Dimension(rcScreen.width, rcScreen.height);
    if (windSize.height > parentSize.height) {
        windSize.height = parentSize.height;
    }
    if (windSize.width > parentSize.width) {
        windSize.width = parentSize.width;
    }
    center(wind, rcScreen);
}

From source file:Main.java

public static void centerWindow(Window window, int width, int height) {
    // Get screen size
    final Toolkit tk = Toolkit.getDefaultToolkit();
    final Dimension screensize = tk.getScreenSize();

    // Set window minimum size
    window.setMinimumSize(new Dimension((int) Math.floor(screensize.getWidth() * 0.3),
            (int) Math.floor(screensize.getHeight() * 0.3)));

    // Set window size
    if (width == 0f)
        width = (int) Math.floor(screensize.getWidth() * 0.8);

    if (height == 0f)
        height = (int) Math.floor(screensize.getHeight() * 0.8);

    window.setPreferredSize(new Dimension(width, height));

    int left = (int) (screensize.getWidth() - width) / 2;
    int right = (int) (screensize.getHeight() - height) / 2;
    ;/*from  w ww  .ja va2s  . c  o m*/

    // Center window
    window.setLocation(left, right);
}

From source file:Main.java

public static void setPreferredGeometry(JFrame frame) {

    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension frameSize = frame.getPreferredSize();
    Dimension screenSize = tk.getScreenSize();

    if (frameSize.height > screenSize.height) {
        frameSize.height = screenSize.height;
    }/*  ww  w . ja va  2s .  c o m*/

    if (frameSize.width > screenSize.width) {
        frameSize.width = screenSize.width;
    }

    Point p = new Point((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    Rectangle rect = new Rectangle(p, frameSize);
    frame.setBounds(rect);
}

From source file:GUIUtils.java

/**
 * Centers <CODE>wind</CODE> within the screen. If centering would cause the
 * title bar to go off the top of the screen then move the window down.
 * /*from  www . ja  v  a  2s .  com*/
 * @param wind
 *          The Window to be centered.
 * 
 * @throws IllegalArgumentException
 *           If <TT>wind</TT> is <TT>null</TT>.
 */
public static void centerWithinScreen(Window wind) {
    if (wind == null) {
        throw new IllegalArgumentException("null Window passed");
    }
    final Toolkit toolKit = Toolkit.getDefaultToolkit();
    final Rectangle rcScreen = new Rectangle(toolKit.getScreenSize());
    final Dimension windSize = wind.getSize();
    final Dimension parentSize = new Dimension(rcScreen.width, rcScreen.height);
    if (windSize.height > parentSize.height) {
        windSize.height = parentSize.height;
    }
    if (windSize.width > parentSize.width) {
        windSize.width = parentSize.width;
    }
    center(wind, rcScreen);
}