Example usage for java.awt GraphicsConfiguration createCompatibleImage

List of usage examples for java.awt GraphicsConfiguration createCompatibleImage

Introduction

In this page you can find the example usage for java.awt GraphicsConfiguration createCompatibleImage.

Prototype

public BufferedImage createCompatibleImage(int width, int height) 

Source Link

Document

Returns a BufferedImage with a data layout and color model compatible with this GraphicsConfiguration .

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gs = ge.getDefaultScreenDevice();
    GraphicsConfiguration gc = gs.getDefaultConfiguration();

    // Create an image that does not support transparency
    BufferedImage bimage = gc.createCompatibleImage(100, 100);

}

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
 *//*  w w w .j a  va  2s.c  om*/
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:com.ables.pix.utility.PictureOps.java

public static BufferedImage tilt(BufferedImage image, double rotation) {
    double sin = Math.abs(Math.sin(getAngle()));
    double cos = Math.abs(Math.cos(getAngle()));
    int w = image.getWidth(), h = image.getHeight();

    int neww = (int) Math.floor(w * cos + sin * h), newh = (int) Math.floor(h * cos + sin * w);
    GraphicsConfiguration gc = getDefaultConfiguration();
    BufferedImage rotated = gc.createCompatibleImage(neww, newh);
    Graphics2D g = rotated.createGraphics();
    g.translate((neww - w) / 2, (newh - h / 2));
    g.rotate(getAngle(), w / 2, h / 2);/*from w  ww .  ja  v  a 2 s  .c o  m*/
    g.drawRenderedImage(image, null);
    g.dispose();
    return rotated;
}

From source file:io.github.moosbusch.lumpi.util.LumpiUtil.java

public static BufferedImage createCompatibleImage(int width, int height) {
    GraphicsEnvironment gd = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsConfiguration conf = gd.getDefaultScreenDevice().getDefaultConfiguration();
    return conf.createCompatibleImage(width, height);
}

From source file:eu.novait.imagerenamer.model.ImageFile.java

private BufferedImage getCompatibleImage(int w, int h) {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd = ge.getDefaultScreenDevice();
    GraphicsConfiguration gc = gd.getDefaultConfiguration();
    BufferedImage image = gc.createCompatibleImage(w, h);
    return image;
}

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  ww w .  j  av  a2  s .  c om*/
        // 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));
    }
}

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  w w.j  a  v  a2s  .  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();
    }
}