Example usage for java.awt Window getGraphicsConfiguration

List of usage examples for java.awt Window getGraphicsConfiguration

Introduction

In this page you can find the example usage for java.awt Window getGraphicsConfiguration.

Prototype

public GraphicsConfiguration getGraphicsConfiguration() 

Source Link

Document

Gets the GraphicsConfiguration associated with this Component .

Usage

From source file:Main.java

public static void center(Window window, double ratio) {
    Rectangle bounds = window.getGraphicsConfiguration().getBounds();
    int width = bounds.width / 2;
    int height = bounds.height / 2;
    int centerX = (int) (bounds.x + bounds.width * ratio);
    int centerY = (int) (bounds.y + bounds.height * ratio);
    window.setLocation(centerX - width / 2, centerY - height / 2);
    window.setPreferredSize(new Dimension(width, height));
    window.pack();// www .  j a v a  2s  . c o  m
}

From source file:Main.java

public static GraphicsConfiguration getGraphicsConfigurationForCurrentLocation(Window window) {
    GraphicsConfiguration ownedConfig = window.getGraphicsConfiguration();
    Point currLocation = window.getLocation();
    // Shortcut for "owned" case
    if (ownedConfig.getBounds().contains(currLocation))
        return ownedConfig;

    // Search for the right screen
    GraphicsDevice[] screens = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
    for (GraphicsDevice screen : screens)
        for (GraphicsConfiguration config : screen.getConfigurations())
            if (config.getBounds().contains(currLocation))
                return config;

    // If none found, lets return the "owned" one
    return ownedConfig;
}

From source file:Main.java

public static void center(Window window) {
    window.pack();/*from w  w  w  . j av a 2  s  .c  om*/
    Rectangle bounds = window.getGraphicsConfiguration().getBounds();
    int width = window.getWidth();
    int height = window.getHeight();
    int centerX = bounds.x + bounds.width / 2;
    int centerY = bounds.y + bounds.height / 2;
    window.setLocation(centerX - width / 2, centerY - height / 2);
    window.setPreferredSize(new Dimension(width, height));
}

From source file:Main.java

/**
 * Retrieves the bounds represents the available space on the Window's display that is not used up by things like
 * the task bar or dock./*from w  w w  . j a v a  2 s.  c o m*/
 *
 * @param window
 *         The window to center
 * @return Bounds of the display's available space
 */
public static Rectangle getBoundsOfAvailableDisplaySpace(Window window) {
    GraphicsConfiguration graphicsConfigToUse = window.getGraphicsConfiguration();
    // If the Window does not have a GraphicsConfiguration yet (perhaps when the Window is not set up yet) then use the default screen's GraphicsConfiguration.
    if (graphicsConfigToUse == null) {
        graphicsConfigToUse = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
                .getDefaultConfiguration();
    }
    Rectangle boundsOfEntireScreen = graphicsConfigToUse.getBounds();
    Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(graphicsConfigToUse);

    return new Rectangle(screenInsets.left, screenInsets.top,
            boundsOfEntireScreen.width - screenInsets.left - screenInsets.right,
            boundsOfEntireScreen.height - screenInsets.top - screenInsets.bottom);
}

From source file:Main.java

/**
 * Returns the graphics device that should apply to a window.
 * //from   w  w w  .  j a v a2 s  . c o m
 * @param window The window.
 * @return The graphics device.
 */
public static GraphicsDevice getGraphicsDevice(Window window) {
    if (window != null) {
        return window.getGraphicsConfiguration().getDevice();
    }
    return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
}

From source file:Main.java

/**
 * Positions the specified frame at a relative position in the screen, where 50% is considered to be the center of the
 * screen./*from   w w w .  j  a v a2s . c  o  m*/
 *
 * @param frame             the frame.
 * @param horizontalPercent the relative horizontal position of the frame (0.0 to 1.0, where 0.5 is the center of the
 *                          screen).
 * @param verticalPercent   the relative vertical position of the frame (0.0 to 1.0, where 0.5 is the center of the
 *                          screen).
 */
public static void positionFrameOnScreen(final Window frame, final double horizontalPercent,
        final double verticalPercent) {

    final Rectangle s = frame.getGraphicsConfiguration().getBounds();
    final Dimension f = frame.getSize();

    final int spaceOnX = Math.max(s.width - f.width, 0);
    final int spaceOnY = Math.max(s.height - f.height, 0);
    final int x = (int) (horizontalPercent * spaceOnX) + s.x;
    final int y = (int) (verticalPercent * spaceOnY) + s.y;
    frame.setBounds(x, y, f.width, f.height);
    frame.setBounds(s.intersection(frame.getBounds()));
}

From source file:Main.java

public static int getMonitor(Component c) {
    Window w = (c instanceof Window) ? (Window) c : SwingUtilities.windowForComponent(c);
    GraphicsConfiguration gc = (w == null) ? null : w.getGraphicsConfiguration();
    GraphicsDevice gd = (gc == null) ? null : gc.getDevice();
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] gs = ge.getScreenDevices();

    for (int i = 0; i < gs.length; i++) {
        if (gs[i].equals(gd)) {
            return i;
        }/*from  w  w w . ja  va  2s.  c  o  m*/
    }

    return -1;
}

From source file:Main.java

/**
 * Positions the specified frame at a relative position in the screen, where 50% is considered
 * to be the center of the screen.//from www.  j  ava  2  s  .  c  om
 *
 * @param frame  the frame.
 * @param horizontalPercent  the relative horizontal position of the frame (0.0 to 1.0,
 *                           where 0.5 is the center of the screen).
 * @param verticalPercent  the relative vertical position of the frame (0.0 to 1.0, where
 *                         0.5 is the center of the screen).
 */
public static void positionFrameOnScreen(final Window frame, final double horizontalPercent,
        final double verticalPercent) {

    final Rectangle s = frame.getGraphicsConfiguration().getBounds();
    final Dimension f = frame.getSize();
    final int w = Math.max(s.width - f.width, 0);
    final int h = Math.max(s.height - f.height, 0);
    final int x = (int) (horizontalPercent * w) + s.x;
    final int y = (int) (verticalPercent * h) + s.y;
    frame.setBounds(x, y, f.width, f.height);

}

From source file:Main.java

public static Rectangle getScreenSize(Window win) {
    Rectangle sb;//from w  w  w . j  av  a 2 s.  c o  m
    if (win == null) {
        sb = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
                .getDefaultConfiguration().getBounds();
    } else {
        sb = win.getGraphicsConfiguration().getBounds();
    }
    return sb;
}

From source file:Main.java

public static Insets getScreenInsets(Window win) {
    Insets si;//w ww  .  j  ava  2 s.c  o m
    if (win == null) {
        si = Toolkit.getDefaultToolkit().getScreenInsets(GraphicsEnvironment.getLocalGraphicsEnvironment()
                .getDefaultScreenDevice().getDefaultConfiguration());
    } else {
        si = win.getToolkit().getScreenInsets(win.getGraphicsConfiguration());
    }
    return si;
}