Example usage for java.awt GraphicsDevice getDefaultConfiguration

List of usage examples for java.awt GraphicsDevice getDefaultConfiguration

Introduction

In this page you can find the example usage for java.awt GraphicsDevice getDefaultConfiguration.

Prototype

public abstract GraphicsConfiguration getDefaultConfiguration();

Source Link

Document

Returns the default GraphicsConfiguration associated with this GraphicsDevice .

Usage

From source file:Main.java

public static GraphicsDevice getCurrentScreen(Point location, Dimension size) {
    GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();

    GraphicsDevice bestMatch = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
    float bestPercentage = 0;
    for (GraphicsDevice device : devices) {
        Rectangle bounds = device.getDefaultConfiguration().getBounds();
        float percentage = getPercentageOnScreen(location, size, bounds);

        if (percentage > bestPercentage) {
            bestMatch = device;/*  w w  w.j a  va  2  s  .c  om*/
            bestPercentage = percentage;
        }
    }
    return bestMatch;
}

From source file:Main.java

public static GraphicsDevice getScreenByLocation(Point p) {
    final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

    final GraphicsDevice[] screens = ge.getScreenDevices();

    for (final GraphicsDevice screen : screens) {
        final Rectangle bounds = screen.getDefaultConfiguration().getBounds();

        if (bounds.contains(p)) {
            return screen;

        }//from w  ww . j  a v a  2 s  .c o m

    }
    return null;

}

From source file:Main.java

/**
 * Used for getting the actual bound of the monitor that contains Point p
 *
 * @param p - point to check monitor for
 * @return - current monitor bounds/*from  w w  w  . j  av  a  2 s  . co m*/
 */
public static Rectangle getScreenBoundsForPoint(Point p) {
    Rectangle bounds;
    GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();

    GraphicsConfiguration graphicsConfiguration = null;
    for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
        if (gd.getDefaultConfiguration().getBounds().contains(p)) {
            graphicsConfiguration = gd.getDefaultConfiguration();
            break;
        }
    }
    if (graphicsConfiguration != null) {
        bounds = graphicsConfiguration.getBounds();
        Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(graphicsConfiguration);
        bounds.x += screenInsets.left;
        bounds.y += screenInsets.top;
        bounds.height -= screenInsets.bottom;
        bounds.width -= screenInsets.right;
    } else {
        bounds = env.getMaximumWindowBounds();
    }
    return (bounds);
}

From source file:Main.java

public static void centerFrame(JFrame frame) {
    GraphicsDevice defaultScreen = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
    Rectangle screenSize = defaultScreen.getDefaultConfiguration().getBounds();

    int midx = screenSize.width / 2;
    int midy = screenSize.height / 2;

    int posx = midx - (frame.getWidth() / 2);
    int posy = midy - (frame.getHeight() / 2);

    frame.setLocation(posx, posy);//from w w  w  .  ja va  2 s.c  o  m
}

From source file:Main.java

public static void showOnSameScreenAsMouseCenter(Container frame) {
    Point mouseLocation = MouseInfo.getPointerInfo().getLocation();

    GraphicsDevice device;//from   w  w  w  . jav  a  2 s  . c  o m

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice lstGDs[] = ge.getScreenDevices();

    ArrayList<GraphicsDevice> lstDevices = new ArrayList<GraphicsDevice>(lstGDs.length);

    for (GraphicsDevice gd : lstGDs) {
        GraphicsConfiguration gc = gd.getDefaultConfiguration();
        Rectangle screenBounds = gc.getBounds();

        if (screenBounds.contains(mouseLocation)) {
            lstDevices.add(gd);
        }
    }

    if (lstDevices.size() > 0) {
        device = lstDevices.get(0);
    } else {
        device = ge.getDefaultScreenDevice();
    }

    Rectangle bounds = device.getDefaultConfiguration().getBounds();
    frame.setLocation(bounds.x + bounds.width / 2 - frame.getWidth() / 2,
            bounds.y + bounds.height / 2 - frame.getHeight() / 2);
}

From source file:Main.java

/**
 * @param bounds/*from w  ww. ja  v  a2 s.  co  m*/
 * @return
 */
public static GraphicsDevice getScreenByBounds(Rectangle bounds) {
    final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

    final GraphicsDevice[] screens = ge.getScreenDevices();
    Rectangle biggestIntersection = null;
    GraphicsDevice bestScreen = null;
    for (final GraphicsDevice screen : screens) {
        final Rectangle sb = screen.getDefaultConfiguration().getBounds();
        Rectangle intersection = sb.intersection(bounds);
        if (intersection != null) {
            if (biggestIntersection == null || intersection.width
                    * intersection.height > biggestIntersection.width * biggestIntersection.height) {
                biggestIntersection = intersection;
                bestScreen = screen;
                if (intersection.equals(bounds)) {
                    // it will not get better
                    break;
                }
            }
        }

    }
    return (biggestIntersection == null || biggestIntersection.width * biggestIntersection.height == 0) ? null
            : bestScreen;
}

From source file:Main.java

/**
 * Takes a snapshot of the target component.
 *
 * @param component the component to draw
 * @param usePrint  whether <tt>print()</tt> or <tt>paint()</tt> is used to grab the snapshot
 * @return a Graphics compatible image of the component
 *//*from   www .j  av a2 s  .  c o  m*/
public static Image takeSnapshot(Component component, boolean usePrint) {
    BufferedImage image = null;
    GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd = genv.getDefaultScreenDevice();
    GraphicsConfiguration gc = gd.getDefaultConfiguration();

    if (gc.getColorModel().hasAlpha()) {
        image = gc.createCompatibleImage((int) component.getSize().getWidth(),
                (int) component.getSize().getHeight());
    } else {
        image = new BufferedImage((int) component.getSize().getWidth(), (int) component.getSize().getHeight(),
                BufferedImage.TYPE_INT_ARGB);
    }

    Graphics g = image.getGraphics();
    if (usePrint) {
        component.print(g);
    } else {
        component.paint(g);
    }
    g.dispose();

    return image;
}

From source file:Utils.java

/**
 * <p/>//from   w  w w. j  av a  2  s .  c o  m
 * Returns the <code>Point</code> at which a window should be placed to
 * center that window on the given desktop.
 * </p>
 * <p/>
 * Some thought was taken as to whether to implement a method such as this,
 * or to simply make a method that, given a window, will center it.  It was
 * decided that it is better to not alter an object within a method.
 * </p>
 *
 * @param window  The window (JInternalFrame) to calculate the center point
 *                for.  This object can not be null.
 *
 * @return the <code>Point</code> at which the window should be placed to
 *         center that window on the given desktop
 */
public static Point getPointForCentering(JInternalFrame window) {
    try {
        //assert window != null;
        Point mousePoint = MouseInfo.getPointerInfo().getLocation();
        GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
        for (GraphicsDevice device : devices) {
            Rectangle bounds = device.getDefaultConfiguration().getBounds();
            //check to see if the mouse cursor is within these bounds
            if (mousePoint.x >= bounds.x && mousePoint.y >= bounds.y
                    && mousePoint.x <= (bounds.x + bounds.width)
                    && mousePoint.y <= (bounds.y + bounds.height)) {
                //this is it
                int screenWidth = bounds.width;
                int screenHeight = bounds.height;
                int width = window.getWidth();
                int height = window.getHeight();
                return new Point(((screenWidth - width) / 2) + bounds.x,
                        ((screenHeight - height) / 2) + bounds.y);
            }
        }
    } catch (Exception e) {

    }
    return new Point(0, 0);
}

From source file:Main.java

public static void centerOnScreenAtLocation(Window window, Point desiredLocation) {
    GraphicsDevice currentScreen = getCurrentScreen(desiredLocation, window.getSize());
    Rectangle2D screenBounds = currentScreen.getDefaultConfiguration().getBounds();
    window.setLocation((int) screenBounds.getCenterX() - (window.getWidth() / 2),
            (int) screenBounds.getCenterY() - (window.getHeight() / 2));
}

From source file:zz.pseas.ghost.utils.BrowserUtil.java

public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }//from   www . j  ava2 s.co m
    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();
    BufferedImage bimage = null;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    try {
        int transparency = Transparency.OPAQUE;
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();
        bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
        // The system does not have a screen
    }
    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;
        bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    }
    // Copy image to buffered image
    Graphics g = bimage.createGraphics();
    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();
    return bimage;
}