Example usage for java.awt Transparency TRANSLUCENT

List of usage examples for java.awt Transparency TRANSLUCENT

Introduction

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

Prototype

int TRANSLUCENT

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

Click Source Link

Document

Represents image data that contains or might contain arbitrary alpha values between and including 0.0 and 1.0.

Usage

From source file:ImageOpByRomain.java

/**
 * <p>//  ww  w.ja v a2  s  .co  m
 * Returns a new translucent compatible image of the specified width and
 * height.
 * </p>
 * 
 * @see #createCompatibleImage(java.awt.image.BufferedImage)
 * @see #createCompatibleImage(java.awt.image.BufferedImage, int, int)
 * @see #createCompatibleImage(int, int)
 * @see #loadCompatibleImage(java.net.URL)
 * @see #toCompatibleImage(java.awt.image.BufferedImage)
 * @param width
 *            the width of the new image
 * @param height
 *            the height of the new image
 * @return a new translucent compatible <code>BufferedImage</code> of the
 *         specified width and height
 */
public static BufferedImage createTranslucentCompatibleImage(int width, int height) {
    return CONFIGURATION.createCompatibleImage(width, height, Transparency.TRANSLUCENT);
}

From source file:org.openstreetmap.josm.tools.ImageProvider.java

/**
 * Decorate one icon with an overlay icon.
 *
 * @param ground the base image//from  ww  w .  j ava  2 s .c o  m
 * @param overlay the overlay image (can be smaller than the base image)
 * @param pos position of the overlay image inside the base image (positioned
 * in one of the corners)
 * @return an icon that represent the overlay of the two given icons. The second icon is layed
 * on the first relative to the given position.
 */
public static ImageIcon overlay(Icon ground, Icon overlay, OverlayPosition pos) {
    GraphicsConfiguration conf = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
            .getDefaultConfiguration();
    int w = ground.getIconWidth();
    int h = ground.getIconHeight();
    int wo = overlay.getIconWidth();
    int ho = overlay.getIconHeight();
    BufferedImage img = conf.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
    Graphics g = img.createGraphics();
    ground.paintIcon(null, g, 0, 0);
    int x = 0, y = 0;
    switch (pos) {
    case NORTHWEST:
        x = 0;
        y = 0;
        break;
    case NORTHEAST:
        x = w - wo;
        y = 0;
        break;
    case SOUTHWEST:
        x = 0;
        y = h - ho;
        break;
    case SOUTHEAST:
        x = w - wo;
        y = h - ho;
        break;
    }
    overlay.paintIcon(null, g, x, y);
    return new ImageIcon(img);
}

From source file:com.josue.tileset.editor.Editor.java

private BufferedImage createTileImage() {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gs = ge.getDefaultScreenDevice();
    GraphicsConfiguration gc = gs.getDefaultConfiguration();
    BufferedImage tileImage = gc.createCompatibleImage(TILE_SIZE, TILE_SIZE, Transparency.TRANSLUCENT);
    //        return new BufferedImage(Tile.TILE_SIZE, Tile.TILE_SIZE, BufferedImage.TYPE_INT_ARGB);
    return tileImage;
}

From source file:com.occamlab.te.parsers.ImageParser.java

private static Node processFrame(ImageReader reader, int frame, NodeList nodes, PrintWriter logger)
        throws Exception {
    if (nodes.getLength() == 0) {
        return null;
    }/*from w  ww. j  a v a  2 s .  com*/
    String formatName = reader.getFormatName().toLowerCase(); // 2011-09-08
                                                              // PwD
    BufferedImage image = reader.read(frame);

    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            // System.out.println(node.getLocalName());
            if (node.getLocalName().equals("type")) {
                node.setTextContent(formatName); // 2011-09-08 PwD was
                                                 // reader.getFormatName().toLowerCase()
            } else if (node.getLocalName().equals("height")) {
                node.setTextContent(Integer.toString(image.getHeight()));
            } else if (node.getLocalName().equals("width")) {
                node.setTextContent(Integer.toString(image.getWidth()));
            } else if (node.getLocalName().equals("metadata")) {
                try { // 2011--08-23 PwD
                    IIOMetadata metadata = reader.getImageMetadata(frame);
                    if (metadata != null) {
                        String format = ((Element) node).getAttribute("format");
                        if (format.length() == 0) {
                            format = metadata.getNativeMetadataFormatName();
                        }
                        Node tree = metadata.getAsTree(format);
                        TransformerFactory tf = TransformerFactory.newInstance();
                        Transformer t = tf.newTransformer();
                        t.transform(new DOMSource(tree), new DOMResult(node));
                    }
                } catch (javax.imageio.IIOException e) { // 2011--08-23 PwD
                    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                    DocumentBuilder db = dbf.newDocumentBuilder();
                    Document doc = db.newDocument();
                    String format = reader.getFormatName().toLowerCase();
                    String formatEltName = "javax_imageio_" + format + "_1.0";
                    Element formatElt = doc.createElement(formatEltName);
                    TransformerFactory tf = TransformerFactory.newInstance();
                    Transformer t = tf.newTransformer();
                    t.transform(new DOMSource(formatElt), new DOMResult(node));
                }
            } else if (node.getLocalName().equals("model")) {
                int imagetype = -1;
                String model = ((Element) node).getAttribute("value");
                if (model.equals("MONOCHROME")) {
                    imagetype = BufferedImage.TYPE_BYTE_BINARY;
                } else if (model.equals("GRAY")) {
                    imagetype = BufferedImage.TYPE_BYTE_GRAY;
                } else if (model.equals("RGB")) {
                    imagetype = BufferedImage.TYPE_3BYTE_BGR;
                } else if (model.equals("ARGB")) {
                    imagetype = BufferedImage.TYPE_4BYTE_ABGR;
                } else {
                    model = "CUSTOM";
                }
                ((Element) node).setAttribute("value", model);
                BufferedImage buffImage = image;
                if (image.getType() != imagetype && imagetype != -1) {
                    buffImage = new BufferedImage(image.getWidth(), image.getHeight(), imagetype);
                    Graphics2D g2 = buffImage.createGraphics();
                    ImageTracker tracker = new ImageTracker();
                    boolean done = g2.drawImage(image, 0, 0, tracker);
                    if (!done) {
                        while (!tracker.done) {
                            sleep(50);
                        }
                    }
                }
                processBufferedImage(buffImage, formatName, node.getChildNodes());
            } else if (node.getLocalName().equals("transparency")) { // 2011-08-24
                                                                     // PwD
                int transparency = image.getTransparency();
                String transparencyName = null;
                switch (transparency) {
                case Transparency.OPAQUE: {
                    transparencyName = "Opaque";
                    break;
                }
                case Transparency.BITMASK: {
                    transparencyName = "Bitmask";
                    break;
                }
                case Transparency.TRANSLUCENT: {
                    transparencyName = "Translucent";
                    break;
                }
                default: {
                    transparencyName = "Unknown";
                }
                }
                node.setTextContent(transparencyName);

            } else if (node.getLocalName().equals("base64Data")) { // 2011-09-08
                                                                   // PwD
                String base64Data = getBase64Data(image, formatName, node);
                node.setTextContent(base64Data);
            } else {
                logger.println("ImageParser Error: Invalid tag " + node.getNodeName());
            }
        }
    }
    return null;
}

From source file:org.geoserver.wms.wms_1_1_1.GetMapIntegrationTest.java

@Test
public void testTransparentPaletteTransparentOutput() throws Exception {
    String url = "wms?LAYERS=" + getLayerId(MockData.TASMANIA_DEM) + "&styles=demTranslucent&"
            + "FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1" + "&REQUEST=GetMap&SRS=EPSG%3A4326"
            + "&BBOX=145,-43,146,-41&WIDTH=100&HEIGHT=200&transparent=true";
    BufferedImage bi = getAsImage(url, "image/png");

    ColorModel cm = bi.getColorModel();
    assertTrue(cm instanceof IndexColorModel);
    assertEquals(Transparency.TRANSLUCENT, cm.getTransparency());

    // grab a pixel in the low left corner, should be transparent
    int[] pixel = new int[1];
    bi.getRaster().getPixel(4, 196, pixel);
    int[] color = new int[4];
    cm.getComponents(pixel[0], color, 0);
    assertEquals(0, color[3]);// w w  w .  j  a  v  a  2  s .co  m

    // a pixel high enough to be solid, should be solid green
    bi.getRaster().getPixel(56, 49, pixel);
    cm.getComponents(pixel[0], color, 0);
    assertEquals(0, color[0]);
    assertEquals(255, color[1]);
    assertEquals(0, color[2]);
    assertEquals(255, color[3]);
}

From source file:org.geoserver.wms.wms_1_1_1.GetMapIntegrationTest.java

@Test
public void testTransparentPaletteTransparentOutputPng8() throws Exception {
    String url = "wms?LAYERS=" + getLayerId(MockData.TASMANIA_DEM) + "&styles=demTranslucent&"
            + "FORMAT=image%2Fpng8&SERVICE=WMS&VERSION=1.1.1" + "&REQUEST=GetMap&SRS=EPSG%3A4326"
            + "&BBOX=145,-43,146,-41&WIDTH=100&HEIGHT=200&transparent=true";
    BufferedImage bi = getAsImage(url, "image/png; mode=8bit");

    ColorModel cm = bi.getColorModel();
    assertTrue(cm instanceof IndexColorModel);
    assertEquals(Transparency.TRANSLUCENT, cm.getTransparency());

    // grab a pixel in the low left corner, should be transparent
    int[] pixel = new int[1];
    bi.getRaster().getPixel(4, 196, pixel);
    int[] color = new int[4];
    cm.getComponents(pixel[0], color, 0);
    assertEquals(0, color[3]);/*  ww w  .j av  a  2s . c  o  m*/

    // a pixel high enough to be solid, should be solid green
    bi.getRaster().getPixel(56, 49, pixel);
    cm.getComponents(pixel[0], color, 0);
    assertEquals(0, color[0]);
    assertEquals(255, color[1]);
    assertEquals(0, color[2]);
    assertEquals(255, color[3]);
}

From source file:lucee.runtime.img.Image.java

private Object toStringTransparency(int transparency) {
    if (Transparency.OPAQUE == transparency)
        return "OPAQUE";
    if (Transparency.BITMASK == transparency)
        return "BITMASK";
    if (Transparency.TRANSLUCENT == transparency)
        return "TRANSLUCENT";
    return "Unknown type of transparency";
}

From source file:com.t3.model.Token.java

private Asset createAssetFromIcon(ImageIcon icon) {
    if (icon == null)
        return null;

    // Make sure there is a buffered image for it
    Image image = icon.getImage();
    if (!(image instanceof BufferedImage)) {
        image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), Transparency.TRANSLUCENT);
        Graphics2D g = ((BufferedImage) image).createGraphics();
        icon.paintIcon(null, g, 0, 0);//w w  w .j  a va 2s  . co m
    }
    // Create the asset
    Asset asset = null;
    try {
        asset = new Asset(name, ImageUtil.imageToBytes((BufferedImage) image));
        if (!AssetManager.hasAsset(asset))
            AssetManager.putAsset(asset);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return asset;
}

From source file:tufts.vue.LWComponent.java

/**
 * Create a new buffered image, of max dimension maxSize, and render the LWComponent
 * (and all it's children), to it using the given alpha.
 * @param alpha 0.0 (invisible) to 1.0 (no alpha)
 * @param maxSize max dimensions for image. May be null.  Image may be smaller than maxSize.
 * @param fillColor -- if non-null, will be rendered as background for image.  If null, presume alpha 0 fill.
 * @param zoomRequest -- desired zoom; ignored if maxSize is non-null
 * also set, background fill will have transparency of alpha^3 to enhance contrast.
 *///from  w ww  . j  a v a 2s  . c o  m

// Note: as of Mac OS X 10.4.10 (Intel), when a java drag source declares it can
// generate an image (as we do when we Apple-drag something), if you drop it on the
// desktop, it will create a special mac "picture clipping", which is some kind of
// raw format, probabaly TIFF, tho you CANNOT open these in Preview.  Apparently
// there's some kind of bug in the special .pictClipping, where sometimes when
// opening it up it shows entirely as a blank space (I think if the image starts to
// get "very large"), tho the data is actually there -- if you drag the picture
// clipping into an Apple Mail message, it shows up again (and if you dragged from
// VUE to Apple Mail in the first place, it also works fine).  Note that AFTER
// dragging into Apple Mail, you can THEN double-click the attachment, and it will
// open it up in Preview as a .tiff file (Apple Mail appears to be converting the
// .pictClipping to tiff).  Note that uncompressed TIFF isn't exactly a friendly
// mail attachment format as it's huge.  But once you open the image in Preview, you
// have the option of saving it / exporting it as a jpeg, and you can even adjust
// the quality to your liking.

public BufferedImage createImage(double alpha, Dimension maxSize, Color fillColor, double zoomRequest) {
    final Rectangle2D.Float bounds = getImageBounds();

    if (DEBUG.IMAGE) {
        System.out.println();
        System.out.println(TERM_CYAN + "createImage: " + this + "\n\t zoomRequst: " + zoomRequest
                + "\n\t    maxSize: " + maxSize + "\n\t  mapBounds: " + fmt(bounds) + "\n\t  fillColor: "
                + fillColor + "\n\t      alpha: " + alpha + TERM_CLEAR);
    }

    final Size imageSize = new Size(bounds);
    final double usedZoom = computeZoomAndSize(bounds, maxSize, zoomRequest, imageSize);

    // Image type ARGB is needed if at any point in the generated image, there is a
    // not 100% opaque pixel all the way through the background.  So TYPE_INT_RGB
    // will handle transparency with a map fine -- but we need TYPE_INT_ARGB if,
    // say, we're generating drag image that we want to be a borderless node (fully
    // transparent image border), or if the whole drag image itself is
    // semi-transparent.

    final int imageType;
    final int transparency;

    if (fillColor == null || alpha != OPAQUE || fillColor.getAlpha() != 255) {
        imageType = BufferedImage.TYPE_INT_ARGB;
        transparency = Transparency.TRANSLUCENT;
    } else {
        imageType = BufferedImage.TYPE_INT_RGB;
        transparency = Transparency.OPAQUE;
    }

    //        final boolean fillHasAlpha = (fillColor != null && fillColor.getAlpha() != 255);
    //         //if (alpha == OPAQUE && fillColor != null && fillColor.getAlpha() == 255) {
    //         if (alpha == OPAQUE && (fillColor == null || fillColor.getAlpha() == 255)) {
    //             imageType = BufferedImage.TYPE_INT_RGB;
    //             transparency = Transparency.OPAQUE;
    //         } else {
    //             imageType = BufferedImage.TYPE_INT_ARGB;
    //             transparency = Transparency.TRANSLUCENT;
    //         }

    final int width = imageSize.pixelWidth();
    final int height = imageSize.pixelHeight();

    if (width >= 512 || height >= 512)
        Log.info("creating large image: " + imageSize + " = approx " + Util.abbrevBytes(width * height * 4));

    try {
        Log.info(this + "; createImage:" + "\n\t requestSize: " + imageSize + "\n\trequestAlpha: " + alpha
                + "\n\t requestFill: " + fillColor + "\n\t   pixelSize: " + width + "x" + height
                + "\n\t renderScale: " + usedZoom + "\n\t        type: "
                + (imageType == BufferedImage.TYPE_INT_RGB ? "RGB (opaque)" : "ARGB (translucent)"));
    } catch (Throwable t) {
        Log.error("logging", t);
    }

    //         if (DEBUG.IMAGE) out(TERM_CYAN
    //                              + "createImage:"
    //                              //+ "\n\tfinal size: " + width + "x" + height
    //                              + "\n\t neededSize: " + imageSize
    //                              + "\n\t   usedZoom: " + usedZoom
    //                              + "\n\t       type: " + (imageType == BufferedImage.TYPE_INT_RGB ? "OPAQUE" : "TRANSPARENT")
    //                              + TERM_CLEAR);

    if (mImageBuffer != null && mImageBuffer.getWidth() == width && mImageBuffer.getHeight() == height
            && mImageBuffer.getType() == imageType) {
        // todo: could also re-use if cached image is > our needed size as long it's
        // an ARGB and we fill it with full alpha first, tho we really shouldn't
        // have each component caching it's own image: some kind of small
        // recently used image buffers cache would make more sense.
        if (DEBUG.DND || DEBUG.IMAGE)
            out(TERM_CYAN + "\ngot cached image: " + mImageBuffer + TERM_CLEAR);
    } else {
        try {

            // TODO: manage this in a separate cache -- not one per node

            mImageBuffer = tufts.vue.gui.GUI.getDeviceConfigForWindow(null).createCompatibleImage(width, height,
                    transparency);

        } catch (Throwable t) {
            Log.error("creating image", t);
            Log.error("creating image: failing node: " + Util.tags(this));
            return null;
        }

        if (DEBUG.DND || DEBUG.IMAGE)
            out(TERM_RED + "created image: " + mImageBuffer + TERM_CLEAR);
        else
            Log.info("created image " + mImageBuffer);

    }

    drawImage((Graphics2D) mImageBuffer.getGraphics(), alpha, maxSize, fillColor, zoomRequest);

    return mImageBuffer;
}