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:org.yccheok.jstock.gui.Utils.java

public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }/*from   w  ww  .  jav a  2  s  .  co 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 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:ded.ui.DiagramController.java

@Override
public void paint(Graphics g) {
    // Swing JPanel is double buffered already, but that is not
    // sufficient to avoid rendering bugs on Apple computers
    // with HiDPI/Retina displays.  This is an attempt at a
    // hack that might circumvent it, effectively triple-buffering
    // the rendering step.
    if (this.tripleBufferMode != 0) {
        // The idea here is if I create an in-memory image with no
        // initial association with the display, whatever hacks Apple
        // has added should not kick in, and I get unscaled pixel
        // rendering.
        BufferedImage bi;//from  w  ww.  j  a  v a 2  s.co m

        if (this.tripleBufferMode == -1) {
            // This is not right because we might be drawing on a
            // different screen than the "default" screen.  Also, I
            // am worried that a "compatible" image might be one
            // subject to the scaling effects I'm trying to avoid.
            GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
            GraphicsConfiguration gc = gd.getDefaultConfiguration();
            bi = gc.createCompatibleImage(this.getWidth(), this.getHeight());
        } else {
            // This is not ideal because the color representation
            // for this hidden image may not match that of the display,
            // necessitating a conversion during 'drawImage'.
            try {
                bi = new BufferedImage(this.getWidth(), this.getHeight(), this.tripleBufferMode);
            } catch (IllegalArgumentException e) {
                // This would happen if 'tripleBufferMode' were invalid.
                if (this.tripleBufferMode == BufferedImage.TYPE_INT_ARGB) {
                    // I don't know how this could happen.  Re-throw.
                    this.log("creating a BufferedImage with TYPE_INT_ARGB failed: "
                            + Util.getExceptionMessage(e));
                    this.log("re-throwing exception...");
                    throw e;
                } else {
                    // Change it to something known to be valid and try again.
                    this.log("creating a BufferedImage with imageType " + this.tripleBufferMode + " failed: "
                            + Util.getExceptionMessage(e));
                    this.log("switching type to TYPE_INT_ARGB and re-trying...");
                    this.tripleBufferMode = BufferedImage.TYPE_INT_ARGB;
                    this.paint(g);
                    return;
                }
            }
        }
        Graphics g2 = bi.createGraphics();
        this.innerPaint(g2);
        g2.dispose();

        g.drawImage(bi, 0, 0, null /*imageObserver*/);
    } else {
        this.innerPaint(g);
    }

    if (this.fpsMeasurementMode) {
        // Immediately trigger another paint cycle.
        this.repaint();
    }
}

From source file:ded.ui.DiagramController.java

/** Get and log some details related to display scaling, particularly
  * to help diagnose the graphics bugs on HiDPI/Retina displays. */
public void logDisplayScaling() {
    // Based on code from
    // http://lubosplavucha.com/java/2013/09/02/retina-support-in-java-for-awt-swing/

    try {//from   w  w w . j a  v  a 2 s  .  com
        // Dump a bunch of possibly interesting JVM properties.
        String propertyNames[] = { "awt.toolkit", "java.awt.graphicsenv", "java.runtime.name",
                "java.runtime.version", "java.vendor", "java.version", "java.vm.name", "java.vm.vendor",
                "java.vm.version", };
        for (String name : propertyNames) {
            this.log("property " + name + ": " + System.getProperty(name));
        }

        // Try a property specific to the Apple JVM.
        this.log("apple.awt.contentScaleFactor: "
                + Toolkit.getDefaultToolkit().getDesktopProperty("apple.awt.contentScaleFactor"));

        // Try something specific to OpenJDK.  Here, we
        // reflectively query some private field.  Yuck.
        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        try {
            Field field = gd.getClass().getDeclaredField("scale");
            field.setAccessible(true);
            this.log("GraphicsEnvironment.scale: " + field.get(gd));
        } catch (NoSuchFieldException e) {
            this.log("GraphicsEnvironment does not have a 'scale' field");
        }

        // Check some details of "compatible" images.
        GraphicsConfiguration gc = gd.getDefaultConfiguration();
        BufferedImage bi = gc.createCompatibleImage(64, 64);
        ColorModel cm = bi.getColorModel();
        this.log("compatible image color model: " + cm);

        // Do the same for a specific imageType that seems to be
        // commonly used, and that I am using when saving to PNG.
        bi = new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB);
        cm = bi.getColorModel();
        this.log("TYPE_INT_ARGB color model: " + cm);

        // And one more.
        bi = new BufferedImage(64, 64, BufferedImage.TYPE_INT_RGB);
        cm = bi.getColorModel();
        this.log("TYPE_INT_RGB color model: " + cm);
    } catch (Exception e) {
        this.log("exception during logDisplayScaling(): " + Util.getExceptionMessage(e));
        this.logNoNewline(Util.getExceptionStackTrace(e));
    }
}