Example usage for java.awt GraphicsEnvironment getLocalGraphicsEnvironment

List of usage examples for java.awt GraphicsEnvironment getLocalGraphicsEnvironment

Introduction

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

Prototype

public static GraphicsEnvironment getLocalGraphicsEnvironment() 

Source Link

Document

Returns the local GraphicsEnvironment .

Usage

From source file:Main.java

/**
 * Gets the insets of the screen.// w w  w . j a v  a  2s  .  c o  m
 * <p>
 * <b>Attention: </b>Due to <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6899304">Java bug 6899304</a>
 * this method only returns correct insets values for the primary screen device. For other screen devices empty insets
 * will be returned. In Windows environments these circumstances (task bar on a none primary screen) will be very rare
 * and therefore ignored until the bug will be fixed in a future Java version.
 * </p>
 *
 * @param screenDevice
 *          a screen thats {@link GraphicsConfiguration} will be used to determine the insets
 * @return the insets of this toolkit's screen, in pixels, if the given screen device is the primary screen, otherwise
 *         empty insets
 * @see Toolkit#getScreenInsets(GraphicsConfiguration)
 */
public static Insets getScreenInsets(GraphicsDevice screenDevice) {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    // <bko 2012-02-29>
    // "Fix" for Sun bug 6899304 ("java.awt.Toolkit.getScreenInsets(GraphicsConfiguration) returns incorrect values")
    // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6699851
    if (screenDevice == ge.getDefaultScreenDevice()) {
        // only return Toolkit.getScreenInsets for primary screen device
        return Toolkit.getDefaultToolkit().getScreenInsets(screenDevice.getDefaultConfiguration());
    } else {
        // return empty insets for other screen devices
        return new Insets(0, 0, 0, 0);
    }
    // </bko>
}

From source file:Main.java

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

    GraphicsDevice device;/*from   w ww. ja  va  2s .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:Utils.java

public static BufferedImage createTranslucentImage(int width, int height) {

    return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration()
            .createCompatibleImage(width, height, Transparency.TRANSLUCENT);

}

From source file:TextLayoutDemo.java

public void paint(Graphics g) {
    Graphics2D graphics2D = (Graphics2D) g;
    GraphicsEnvironment.getLocalGraphicsEnvironment();
    Font font = new Font("LucidaSans", Font.PLAIN, 14);
    AttributedString messageAS = new AttributedString(textMessage);
    messageAS.addAttribute(TextAttribute.FONT, font);
    AttributedCharacterIterator messageIterator = messageAS.getIterator();
    FontRenderContext messageFRC = graphics2D.getFontRenderContext();
    LineBreakMeasurer messageLBM = new LineBreakMeasurer(messageIterator, messageFRC);

    Insets insets = getInsets();//ww  w  .  j  a va 2 s. c om
    float wrappingWidth = getSize().width - insets.left - insets.right;
    float x = insets.left;
    float y = insets.top;

    while (messageLBM.getPosition() < messageIterator.getEndIndex()) {
        TextLayout textLayout = messageLBM.nextLayout(wrappingWidth);
        y += textLayout.getAscent();
        textLayout.draw(graphics2D, x, y);
        y += textLayout.getDescent() + textLayout.getLeading();
        x = insets.left;
    }
}

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//  ww w  .  j  a  v a  2 s . c om
 * @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:Main.java

/**
 * Returns the graphics device that should apply to a window.
 * /*w ww  . j a  va 2 s  .com*/
 * @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:Utils.java

/**
 * Creates an image compatible with the current display
 * /* w w  w  .j a  v  a2s.co  m*/
 * @return A BufferedImage with the appropriate color model
 */
public static BufferedImage createCompatibleImage(int width, int height) {
    GraphicsConfiguration configuration = GraphicsEnvironment.getLocalGraphicsEnvironment()
            .getDefaultScreenDevice().getDefaultConfiguration();
    return configuration.createCompatibleImage(width, height, Transparency.TRANSLUCENT);
}

From source file:Utils.java

/**
 * <p/>/*from   ww  w. ja va 2s  . co  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:org.github.jipsg.sanselan.ManagedImageBufferedImageFactory.java

public BufferedImage getColorBufferedImage(final int width, final int height, final boolean hasAlpha) {
    final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    final GraphicsDevice gd = ge.getDefaultScreenDevice();
    final GraphicsConfiguration gc = gd.getDefaultConfiguration();
    return gc.createCompatibleImage(width, height, Transparency.TRANSLUCENT);
}

From source file:Utils.java

private static BufferedImage createCompatibleImage(int width, int height) {

    return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration()
            .createCompatibleImage(width, height);

}