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:SwingUtil.java

/**
 * Verifies if the given point is visible on the screen.
 * /*  ww  w.  jav  a2s.c  o  m*/
 * @param    location       The given location on the screen.
 * @return                True if the location is on the screen, false otherwise.
 */
public static boolean isLocationInScreenBounds(Point location) {

    // Check if the location is in the bounds of one of the graphics devices.
    GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] graphicsDevices = graphicsEnvironment.getScreenDevices();
    Rectangle graphicsConfigurationBounds = new Rectangle();

    // Iterate over the graphics devices.
    for (int j = 0; j < graphicsDevices.length; j++) {

        // Get the bounds of the device.
        GraphicsDevice graphicsDevice = graphicsDevices[j];
        graphicsConfigurationBounds.setRect(graphicsDevice.getDefaultConfiguration().getBounds());

        // Is the location in this bounds?
        graphicsConfigurationBounds.setRect(graphicsConfigurationBounds.x, graphicsConfigurationBounds.y,
                graphicsConfigurationBounds.width, graphicsConfigurationBounds.height);
        if (graphicsConfigurationBounds.contains(location.x, location.y)) {

            // The location is in this screengraphics.
            return true;

        }

    }

    // We could not find a device that contains the given point.
    return false;

}

From source file:Main.java

public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }/*from w  w w .ja va2  s .  c o  m*/

    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();

    // Determine if the image has transparent pixels; for this method's
    // implementation, see Determining If an Image Has Transparent Pixels
    boolean hasAlpha = true;

    // Create a buffered image with a format that's compatible with the screen
    BufferedImage bimage = null;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    try {
        // Determine the type of transparency of the new buffered image
        int transparency = Transparency.OPAQUE;
        if (hasAlpha) {
            transparency = Transparency.BITMASK;
        }

        // Create the buffered image
        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;
        if (hasAlpha) {
            type = BufferedImage.TYPE_INT_ARGB;
        }
        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;
}

From source file:ImageUtil.java

/**
 * Posted by alpha02 at http://www.dreamincode.net/code/snippet1076.htm
 *//*  www  .  j a  v a2s .c  om*/
public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage)
        return (BufferedImage) image;

    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();

    // Determine if the image has transparent pixels
    boolean hasAlpha = hasAlpha(image);

    // Create a buffered image with a format that's compatible with the screen
    BufferedImage bimage = null;

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

    try {
        // Determine the type of transparency of the new buffered image
        int transparency = Transparency.OPAQUE;

        if (hasAlpha == true)
            transparency = Transparency.BITMASK;

        // Create the buffered image
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();

        bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
    } //No screen

    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;

        if (hasAlpha == true) {
            type = BufferedImage.TYPE_INT_ARGB;
        }
        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;
}

From source file:Main.java

/**
 * This method returns a buffered image with the contents of an image
 * This snippet was taken from: http://www.exampledepot.com/egs/java.awt.image/Image2Buf.html
 * @param image/*from   w w  w  .ja  v  a 2s.  com*/
 * @return The buffered image
 */
public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }

    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();

    // Determine if the image has transparent pixels; for this method's
    // implementation, see e661 Determining If an Image Has Transparent Pixels
    boolean hasAlpha = hasAlpha(image);

    // Create a buffered image with a format that's compatible with the screen
    BufferedImage bimage = null;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    try {
        // Determine the type of transparency of the new buffered image
        int transparency = Transparency.OPAQUE;
        if (hasAlpha) {
            transparency = Transparency.BITMASK;
        }

        // Create the buffered image
        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;
        if (hasAlpha) {
            type = BufferedImage.TYPE_INT_ARGB;
        }
        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;
}

From source file:com.ables.pix.utility.PictureOps.java

public static GraphicsConfiguration getDefaultConfiguration() {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd = ge.getDefaultScreenDevice();
    return gd.getDefaultConfiguration();

}

From source file:Main.java

/**
 * Returns the GraphicsConfiguration for a point.
 *///from ww  w. j a va 2 s. c o m
public static GraphicsConfiguration getGraphicsConfiguration(Component aComp, int anX, int aY) {
    // Get initial GC from component (if available) and point on screen
    GraphicsConfiguration gc = aComp != null ? aComp.getGraphicsConfiguration() : null;
    Point spoint = getScreenLocation(aComp, anX, aY);

    // Replace with alternate GraphicsConfiguration if point on another screen
    for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
        if (gd.getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
            GraphicsConfiguration dgc = gd.getDefaultConfiguration();
            if (dgc.getBounds().contains(spoint.x, spoint.y)) {
                gc = dgc;
                break;
            }
        }
    }

    // Return GraphicsConfiguration
    return gc;
}

From source file:org.pentaho.reporting.libraries.designtime.swing.LibSwingUtil.java

public static boolean safeRestoreWindow(final Window frame, final Rectangle bounds) {
    final GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
    final GraphicsDevice[] devices = graphicsEnvironment.getScreenDevices();
    for (int i = 0; i < devices.length; i++) {
        final GraphicsDevice device = devices[i];
        final Rectangle rectangle = device.getDefaultConfiguration().getBounds();
        if (rectangle.contains(bounds) || rectangle.equals(bounds)) {
            logger.info("Found a usable screen-configuration: Restoring frame to " + bounds);
            frame.setBounds(bounds);//from   w w  w  .j ava  2  s . com
            return true;
        }
    }
    return false;
}

From source file:Main.java

/**
 * @param r//  ww w  . j a  v  a 2  s . c  om
 *          the original rectangle
 * @param includeReservedInsets
 *          if taskbar and other windowing insets should be included in the
 *          returned area
 * @return iff there are multiple monitors the other monitor than the
 *         effective view of the monitor that the rectangle mostly coveres, or
 *         null if there is just one screen
 */
public static Rectangle getOppositeFullScreenBoundsFor(Rectangle r, boolean includeReservedInsets) {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    TreeMap<Integer, Rectangle> prioMap = new TreeMap<Integer, Rectangle>();
    for (GraphicsDevice dev : ge.getScreenDevices()) {
        Rectangle bounds;
        if ((!includeReservedInsets) && dev == ge.getDefaultScreenDevice()) {
            bounds = ge.getMaximumWindowBounds();
        } else {
            bounds = dev.getDefaultConfiguration().getBounds();
        }
        Rectangle intersection = bounds.intersection(r);
        prioMap.put(intersection.width * intersection.height, bounds);
    }
    if (prioMap.size() <= 1) {
        return null;
    } else {
        return prioMap.get(prioMap.firstKey());
    }
}

From source file:Main.java

public static Point getCurrentScreenLocation(Window window) {
    GraphicsDevice currentScreen = getCurrentScreen(window);
    if (currentScreen == GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()) {
        return GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().getLocation();
    }/*w w  w  .ja v  a2s .c o m*/
    return currentScreen.getDefaultConfiguration().getBounds().getLocation();
}

From source file:net.team2xh.crt.gui.util.GUIToolkit.java

/**
 * Provides a BufferedImage in the format most compatible with current graphics card.
 *
 * @param  w Width of the image//from w  w w .  ja  v a2s . co  m
 * @param  h Height of the image
 * @return   Optimized BufferedImage
 */
public static BufferedImage getEfficientBuffer(int w, int h) {
    GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice device = env.getDefaultScreenDevice();
    GraphicsConfiguration config = device.getDefaultConfiguration();
    return config.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
}