Example usage for java.awt Transparency OPAQUE

List of usage examples for java.awt Transparency OPAQUE

Introduction

In this page you can find the example usage for java.awt Transparency OPAQUE.

Prototype

int OPAQUE

To view the source code for java.awt Transparency OPAQUE.

Click Source Link

Document

Represents image data that is guaranteed to be completely opaque, meaning that all pixels have an alpha value of 1.0.

Usage

From source file:com.t3.image.ImageUtil.java

public static int pickBestTransparency(BufferedImage image) {

    // See if we can short circuit
    ColorModel colorModel = image.getColorModel();
    if (colorModel.getTransparency() == Transparency.OPAQUE) {
        return Transparency.OPAQUE;
    }//  w ww  . j  a v a 2 s.  co m

    // Get the pixels
    int width = image.getWidth();
    int height = image.getHeight();

    // Look for specific pixels
    boolean foundTransparent = false;
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            // Get the next pixel
            int pixel = image.getRGB(x, y);
            int alpha = (pixel >> 24) & 0xff;

            // Is there translucency or just pure transparency ?
            if (alpha > 0 && alpha < 255) {
                return Transparency.TRANSLUCENT;
            }

            if (alpha == 0 && !foundTransparent) {
                foundTransparent = true;
            }
        }
    }

    return foundTransparent ? Transparency.BITMASK : Transparency.OPAQUE;
}

From source file:de.mpg.imeji.logic.storage.util.ImageUtils.java

/**
 * Convenience method that returns a scaled instance of the provided {@link BufferedImage}.
 * //w  ww  . j av a2 s  .  com
 * @param img the original image to be scaled
 * @param targetWidth the desired width of the scaled instance, in pixels
 * @param targetHeight the desired height of the scaled instance, in pixels
 * @param hint one of the rendering hints that corresponds to {@code RenderingHints.KEY_INTERPOLATION} (e.g.
 *            {@code RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR},
 *            {@code RenderingHints.VALUE_INTERPOLATION_BILINEAR},
 *            {@code RenderingHints.VALUE_INTERPOLATION_BICUBIC})
 * @param higherQuality if true, this method will use a multi-step scaling technique that provides higher quality
 *            than the usual one-step technique (only useful in downscaling cases, where {@code targetWidth} or
 *            {@code targetHeight} is smaller than the original dimensions, and generally only when the
 *            {@code BILINEAR} hint is specified)
 * @return a scaled version of the original {@link BufferedImage}
 */
public static BufferedImage getScaledInstance(BufferedImage img, int targetWidth, int targetHeight, Object hint,
        boolean higherQuality) {
    int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
            : BufferedImage.TYPE_INT_ARGB;
    BufferedImage ret = img;
    int w, h;
    if (higherQuality) {
        // Use multi-step technique: start with original size, then
        // scale down in multiple passes with drawImage()
        // until the target size is reached
        w = img.getWidth();
        h = img.getHeight();
    } else {
        // Use one-step technique: scale directly from original
        // size to target size with a single drawImage() call
        w = targetWidth;
        h = targetHeight;
    }
    do {
        if (higherQuality && w > targetWidth) {
            w /= 2;
            if (w < targetWidth) {
                w = targetWidth;
            }
        }
        if (higherQuality && h > targetHeight) {
            h /= 2;
            if (h < targetHeight) {
                h = targetHeight;
            }
        }
        BufferedImage tmp = new BufferedImage(w, h, type);
        Graphics2D g2 = tmp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
        g2.drawImage(ret, 0, 0, w, h, null);
        g2.dispose();
        ret = tmp;
    } while (w != targetWidth || h != targetHeight);
    return ret;
}

From source file:net.rptools.lib.image.ImageUtil.java

public static int pickBestTransparency(BufferedImage image) {
    // See if we can short circuit
    ColorModel colorModel = image.getColorModel();
    if (colorModel.getTransparency() == Transparency.OPAQUE) {
        return Transparency.OPAQUE;
    }/* www .  j a  va 2 s  .co  m*/
    // Get the pixels
    int width = image.getWidth();
    int height = image.getHeight();

    // Look for specific pixels
    boolean foundTransparent = false;
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            // Get the next pixel
            int pixel = image.getRGB(x, y);
            int alpha = (pixel >> 24) & 0xff;

            // Is there translucency or just pure transparency ?
            if (alpha > 0 && alpha < 255) {
                return Transparency.TRANSLUCENT;
            }
            if (alpha == 0 && !foundTransparent) {
                foundTransparent = true;
            }
        }
    }
    return foundTransparent ? Transparency.BITMASK : Transparency.OPAQUE;
}

From source file:it.reexon.lib.files.FileUtils.java

/**
 * Convenience method that returns a scaled instance of the provided {@code BufferedImage}. 
 *  //from ww w.  ja v a2s.com
 * @param img the original image to be scaled 
 * @param targetWidth the desired width of the scaled instance, in pixels 
 * @param targetHeight the desired height of the scaled instance, in pixels 
 * @param hint one of the rendering hints that corresponds to {@code RenderingHints.KEY_INTERPOLATION} (e.g. 
 *        {@code RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR}, {@code RenderingHints.VALUE_INTERPOLATION_BILINEAR}, 
 *        {@code RenderingHints.VALUE_INTERPOLATION_BICUBIC}) 
 * @param higherQuality if true, this method will use a multi-step scaling technique that provides higher quality than the 
 *        usual one-step technique (only useful in downscaling cases, where {@code targetWidth} or {@code targetHeight} is 
 *        smaller than the original dimensions, and generally only when the {@code BILINEAR} hint is specified) 
 * @return a scaled version of the original {@code BufferedImage} 
 */
public static BufferedImage getScaledInstance(BufferedImage img, int targetWidth, int targetHeight, Object hint,
        boolean higherQuality) {
    final int type = img.getTransparency() == Transparency.OPAQUE ? BufferedImage.TYPE_INT_RGB
            : BufferedImage.TYPE_INT_ARGB;
    BufferedImage ret = img;
    int w;
    int h;
    if (higherQuality) {
        // Use multi-step technique: start with original size, then 
        // scale down in multiple passes with drawImage() 
        // until the target size is reached 
        w = img.getWidth();
        h = img.getHeight();
    } else {
        // Use one-step technique: scale directly from original 
        // size to target size with a single drawImage() call 
        w = targetWidth;
        h = targetHeight;
    }

    do {
        if (higherQuality && w > targetWidth) {
            w /= 2;
            if (w < targetWidth) {
                w = targetWidth;
            }
        }

        if (higherQuality && h > targetHeight) {
            h /= 2;
            if (h < targetHeight) {
                h = targetHeight;
            }
        }

        final BufferedImage tmp = new BufferedImage(w, h, type);
        final Graphics2D g2 = tmp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
        g2.drawImage(ret, 0, 0, w, h, null);
        g2.dispose();

        ret = tmp;
    } while (w != targetWidth || h != targetHeight);

    return ret;
}

From source file:ImageComponentByReferenceTest.java

TiledImage() {
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    int[] nBits = { 8, 8, 8, 8 };
    int i, j, k, cc = 255;
    int[] bandOffset = new int[4];
    colorModel = new ComponentColorModel(cs, nBits, true, false, Transparency.OPAQUE, 0);
    // Create 9 tiles
    bandOffset[0] = 3;/*w  w  w  . jav a  2  s .  com*/
    bandOffset[1] = 2;
    bandOffset[2] = 1;
    bandOffset[3] = 0;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            tile[i][j] = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, 8, 8, 32, 4, bandOffset, null);
        }
    }

    // tile {-2, -1}
    byte[] byteData = ((DataBufferByte) tile[0][0].getDataBuffer()).getData();
    for (i = 4, k = 8 * 4 * 4 + 4 * 4; i < 8; i++, k += 16) {
        for (j = 4; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) cc;
            byteData[k + 2] = (byte) 0;
            byteData[k + 3] = (byte) cc;
        }
    }

    // tile {-1, -1}
    byteData = ((DataBufferByte) tile[1][0].getDataBuffer()).getData();
    for (i = 4, k = 8 * 4 * 4; i < 8; i++) {
        for (j = 4; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) cc;
            byteData[k + 2] = (byte) 0;
            byteData[k + 3] = (byte) cc;
        }
        for (j = 4; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) 0;
            byteData[k + 2] = (byte) 0;
            byteData[k + 3] = (byte) cc;
        }
    }

    // tile {1, -1}
    byteData = ((DataBufferByte) tile[2][0].getDataBuffer()).getData();
    for (i = 4, k = 8 * 4 * 4; i < 8; i++, k += 16) {
        for (j = 0; j < 4; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) 0;
            byteData[k + 2] = (byte) 0;
            byteData[k + 3] = (byte) cc;
        }
    }

    // tile {-2, 0}
    byteData = ((DataBufferByte) tile[0][1].getDataBuffer()).getData();
    for (i = 0, k = 16; i < 4; i++, k += 16) {
        for (j = 4; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) cc;
            byteData[k + 2] = (byte) 0;
            byteData[k + 3] = (byte) cc;
        }
    }
    for (i = 4, k = 8 * 4 * 4 + 16; i < 8; i++, k += 16) {
        for (j = 4; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) 0;
            byteData[k + 2] = (byte) cc;
            byteData[k + 3] = (byte) 0;
        }
    }
    // tile {-1, 0}
    byteData = ((DataBufferByte) tile[1][1].getDataBuffer()).getData();
    for (i = 0, k = 0; i < 4; i++) {
        for (j = 4; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) cc;
            byteData[k + 2] = (byte) 0;
            byteData[k + 3] = (byte) cc;
        }
        for (j = 4; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) 0;
            byteData[k + 2] = (byte) 0;
            byteData[k + 3] = (byte) cc;
        }
    }
    for (i = 0, k = 8 * 4 * 4; i < 4; i++) {
        for (j = 4; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) 0;
            byteData[k + 2] = (byte) cc;
            byteData[k + 3] = (byte) 0;
        }

        for (j = 4; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) 0;
            byteData[k + 2] = (byte) cc;
            byteData[k + 3] = (byte) cc;
        }

    }

    // tile {0, 0}
    byteData = ((DataBufferByte) tile[2][1].getDataBuffer()).getData();
    for (i = 0, k = 0; i < 4; i++, k += 16) {
        for (j = 4; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) 0;
            byteData[k + 2] = (byte) 0;
            byteData[k + 3] = (byte) cc;
        }
    }
    for (i = 4, k = 8 * 4 * 4; i < 8; i++, k += 16) {
        for (j = 4; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) 0;
            byteData[k + 2] = (byte) cc;
            byteData[k + 3] = (byte) cc;
        }
    }

    // tile {-2, 1}
    byteData = ((DataBufferByte) tile[0][2].getDataBuffer()).getData();
    for (i = 4, k = 16; i < 8; i++, k += 16) {
        for (j = 4; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) 0;
            byteData[k + 2] = (byte) cc;
            byteData[k + 3] = (byte) 0;
        }
    }

    // tile {-1, 1}
    byteData = ((DataBufferByte) tile[1][2].getDataBuffer()).getData();
    for (i = 0, k = 0; i < 8; i++) {
        for (j = 4; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) 0;
            byteData[k + 2] = (byte) cc;
            byteData[k + 3] = (byte) 0;
        }
        for (j = 4; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) 0;
            byteData[k + 2] = (byte) cc;
            byteData[k + 3] = (byte) cc;
        }
    }

    // tile {0, 1}
    byteData = ((DataBufferByte) tile[2][2].getDataBuffer()).getData();
    for (i = 4, k = 0; i < 8; i++, k += 16) {
        for (j = 4; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) 0;
            byteData[k + 2] = (byte) cc;
            byteData[k + 3] = (byte) cc;
        }
    }

    bigTile = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, 16, 16, 64, 4, bandOffset, null);
    ;
    byteData = ((DataBufferByte) bigTile.getDataBuffer()).getData();
    for (i = 0, k = 0; i < 8; i++) {
        for (j = 0; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) cc;
            byteData[k + 2] = (byte) 0;
            byteData[k + 3] = (byte) cc;
        }
        for (; j < 16; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) 0;
            byteData[k + 2] = (byte) 0;
            byteData[k + 3] = (byte) cc;
        }
    }
    for (; i < 16; i++) {
        for (j = 0; j < 8; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) 0;
            byteData[k + 2] = (byte) cc;
            byteData[k + 3] = (byte) 0;
        }
        for (; j < 16; j++, k += 4) {
            byteData[k] = (byte) 0;
            byteData[k + 1] = (byte) 0;
            byteData[k + 2] = (byte) cc;
            byteData[k + 3] = (byte) cc;
        }
    }
    checkBoard = new BufferedImage(colorModel, bigTile, false, null);
}

From source file:edu.ku.brc.ui.GraphicsUtils.java

/**
 * Convenience method that returns a scaled instance of the
 * provided {@code BufferedImage}./* w ww. j ava 2  s. c o m*/
 * 
 * Code stolen from http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html
 *
 * @param img the original image to be scaled
 * @param targetWidth the desired width of the scaled instance,
 *    in pixels
 * @param targetHeight the desired height of the scaled instance,
 *    in pixels
 * @param hint one of the rendering hints that corresponds to
 *    {@code RenderingHints.KEY_INTERPOLATION} (e.g.
 *    {@code RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR},
 *    {@code RenderingHints.VALUE_INTERPOLATION_BILINEAR},
 *    {@code RenderingHints.VALUE_INTERPOLATION_BICUBIC})
 * @param higherQuality if true, this method will use a multi-step
 *    scaling technique that provides higher quality than the usual
 *    one-step technique (only useful in down-scaling cases, where
 *    {@code targetWidth} or {@code targetHeight} is
 *    smaller than the original dimensions, and generally only when
 *    the {@code BILINEAR} hint is specified)
 * @return a scaled version of the original {@code BufferedImage}
 */
public static BufferedImage getScaledInstance(final BufferedImage img, final int targetWidth,
        final int targetHeight, final boolean higherQuality) {
    int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
            : BufferedImage.TYPE_INT_ARGB;
    BufferedImage temp = img;

    BufferedImage result = new BufferedImage(targetWidth, targetHeight, type);
    Graphics2D g2 = result.createGraphics();
    if (higherQuality) {
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    }
    g2.drawImage(temp, 0, 0, targetWidth, targetHeight, null);
    g2.dispose();

    return result;
}

From source file:omr.jai.TestImage3.java

public static void print(PlanarImage pi) {
        // Show the image dimensions and coordinates.
        System.out.print("Image Dimensions: ");
        System.out.print(pi.getWidth() + "x" + pi.getHeight() + " pixels");

        // Remember getMaxX and getMaxY return the coordinate of the next point!
        System.out.println(" (from " + pi.getMinX() + "," + pi.getMinY() + " to " + (pi.getMaxX() - 1) + ","
                + (pi.getMaxY() - 1) + ")");
        if ((pi.getNumXTiles() != 1) || (pi.getNumYTiles() != 1)) { // Is it tiled?
            // Tiles number, dimensions and coordinates.
            System.out.print("Tiles: ");
            System.out.print(pi.getTileWidth() + "x" + pi.getTileHeight() + " pixels" + " (" + pi.getNumXTiles()
                    + "x" + pi.getNumYTiles() + " tiles)");
            System.out.print(" (from " + pi.getMinTileX() + "," + pi.getMinTileY() + " to " + pi.getMaxTileX() + ","
                    + pi.getMaxTileY() + ")");
            System.out.println(" offset: " + pi.getTileGridXOffset() + "," + pi.getTileGridXOffset());
        }//from  ww w . j  a  v  a  2 s .c om

        // Display info about the SampleModel of the image.
        SampleModel sm = pi.getSampleModel();
        System.out.println("Number of bands: " + sm.getNumBands());
        System.out.print("Data type: ");
        switch (sm.getDataType()) {
        case DataBuffer.TYPE_BYTE:
            System.out.println("byte");
            break;
        case DataBuffer.TYPE_SHORT:
            System.out.println("short");
            break;
        case DataBuffer.TYPE_USHORT:
            System.out.println("ushort");
            break;
        case DataBuffer.TYPE_INT:
            System.out.println("int");
            break;
        case DataBuffer.TYPE_FLOAT:
            System.out.println("float");
            break;
        case DataBuffer.TYPE_DOUBLE:
            System.out.println("double");
            break;
        case DataBuffer.TYPE_UNDEFINED:
            System.out.println("undefined");
            break;
        }

        // Display info about the ColorModel of the image.
        ColorModel cm = pi.getColorModel();
        if (cm != null) {
            System.out.println("Number of color components: " + cm.getNumComponents());
            System.out.println("Bits per pixel: " + cm.getPixelSize());
            System.out.print("Image Transparency: ");
            switch (cm.getTransparency()) {
            case Transparency.OPAQUE:
                System.out.println("opaque");
                break;
            case Transparency.BITMASK:
                System.out.println("bitmask");
                break;
            case Transparency.TRANSLUCENT:
                System.out.println("translucent");
                break;
            }
        } else
            System.out.println("No color model.");
    }

From source file:com.flexive.shared.media.impl.FxMediaNativeEngine.java

/**
 * Rotate an image using the requested angle
 *
 * @param bufferedImage imeg to rotate/*from  ww  w .  j av  a 2  s.c om*/
 * @param angle         angle to rotate
 * @return BufferedImage containing the rotation
 */
@SuppressWarnings({ "UnusedAssignment" })
private static BufferedImage rotate(BufferedImage bufferedImage, int angle) {
    angle = angle % 360;
    if (angle == 0)
        return bufferedImage;
    if (angle < 0)
        angle += 360;
    switch (angle) {
    case 90:
        BufferedImageOp rot90 = new AffineTransformOp(AffineTransform.getRotateInstance(Math.PI / 2.0,
                bufferedImage.getHeight() / 2.0, bufferedImage.getHeight() / 2.0),
                AffineTransformOp.TYPE_BILINEAR);
        BufferedImage img90 = new BufferedImage(bufferedImage.getHeight(), bufferedImage.getWidth(),
                bufferedImage.getType());
        return rot90.filter(bufferedImage, img90);
    case 180:
        BufferedImageOp rot180 = new AffineTransformOp(AffineTransform.getRotateInstance(Math.PI,
                bufferedImage.getWidth() / 2.0, bufferedImage.getHeight() / 2.0),
                AffineTransformOp.TYPE_BILINEAR);
        BufferedImage img180 = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(),
                bufferedImage.getType());
        return rot180.filter(bufferedImage, img180);
    case 270:
        BufferedImageOp rot270 = new AffineTransformOp(AffineTransform.getRotateInstance(-Math.PI / 2.0,
                bufferedImage.getWidth() / 2.0, bufferedImage.getWidth() / 2.0),
                AffineTransformOp.TYPE_BILINEAR);
        BufferedImage img270 = new BufferedImage(bufferedImage.getHeight(), bufferedImage.getWidth(),
                bufferedImage.getType());
        return rot270.filter(bufferedImage, img270);
    default:
        //rotate using a non-standard angle (have to draw a box around the image that can fit it)
        int box = (int) Math.sqrt(bufferedImage.getHeight() * bufferedImage.getHeight()
                + bufferedImage.getWidth() * bufferedImage.getWidth());
        BufferedImage imgFree = new BufferedImage(box, box, bufferedImage.getType());
        BufferedImage imgRet = new BufferedImage(box, box, bufferedImage.getType());
        Graphics2D g = imgFree.createGraphics();
        if (bufferedImage.getTransparency() == Transparency.OPAQUE) {
            //draw a white background on opaque images since they dont support transparency
            g.setBackground(Color.WHITE);
            g.clearRect(0, 0, box, box);
            Graphics2D gr = imgRet.createGraphics();
            gr.setBackground(Color.WHITE);
            gr.clearRect(0, 0, box, box);
            gr = null;
        }
        g.drawImage(bufferedImage, box / 2 - bufferedImage.getWidth() / 2,
                box / 2 - bufferedImage.getHeight() / 2, bufferedImage.getWidth(), bufferedImage.getHeight(),
                null);
        g = null;
        AffineTransform at = new AffineTransform();
        at.rotate(angle * Math.PI / 180.0, box / 2.0, box / 2.0);
        BufferedImageOp bio;
        bio = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
        return bio.filter(imgFree, imgRet);
    }
}

From source file:com.t3.client.TabletopTool.java

public static BufferedImage takeMapScreenShot(final PlayerView view) {
    final ZoneRenderer renderer = clientFrame.getCurrentZoneRenderer();
    if (renderer == null) {
        return null;
    }//  ww w . ja va  2  s.c  om

    Dimension size = renderer.getSize();
    if (size.width == 0 || size.height == 0) {
        return null;
    }

    BufferedImage image = new BufferedImage(size.width, size.height, Transparency.OPAQUE);
    final Graphics2D g = image.createGraphics();
    g.setClip(0, 0, size.width, size.height);

    // Have to do this on the EDT so that there aren't any odd side effects
    // of rendering
    // using a renderer that's on screen
    if (!EventQueue.isDispatchThread()) {
        try {
            EventQueue.invokeAndWait(new Runnable() {
                @Override
                public void run() {
                    renderer.renderZone(g, view);
                }
            });
        } catch (InterruptedException ie) {
            TabletopTool.showError("While creating snapshot", ie);
        } catch (InvocationTargetException ite) {
            TabletopTool.showError("While creating snapshot", ite);
        }
    } else {
        renderer.renderZone(g, view);
    }

    g.dispose();

    return image;
}

From source file:net.rptools.maptool.client.MapTool.java

public static BufferedImage takeMapScreenShot(final PlayerView view) {
    final ZoneRenderer renderer = clientFrame.getCurrentZoneRenderer();
    if (renderer == null) {
        return null;
    }//  w ww.j ava  2s .  c  om

    Dimension size = renderer.getSize();
    if (size.width == 0 || size.height == 0) {
        return null;
    }

    BufferedImage image = new BufferedImage(size.width, size.height, Transparency.OPAQUE);
    final Graphics2D g = image.createGraphics();
    g.setClip(0, 0, size.width, size.height);

    // Have to do this on the EDT so that there aren't any odd side effects
    // of rendering
    // using a renderer that's on screen
    if (!EventQueue.isDispatchThread()) {
        try {
            EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    renderer.renderZone(g, view);
                }
            });
        } catch (InterruptedException ie) {
            MapTool.showError("While creating snapshot", ie);
        } catch (InvocationTargetException ite) {
            MapTool.showError("While creating snapshot", ite);
        }
    } else {
        renderer.renderZone(g, view);
    }

    g.dispose();

    return image;
}