Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I played recently a bit with this stuff, to display an image over another one, and to fade an image to gray.<br> Also masking an image with a mask with transparency (my previous version of this message!).</p> <p>I took my little test program and tweaked it a bit to get the wanted result.</p> <p>Here are the relevant bits:</p> <pre><code>TestMask() throws IOException { m_images = new BufferedImage[3]; m_images[0] = ImageIO.read(new File("E:/Documents/images/map.png")); m_images[1] = ImageIO.read(new File("E:/Documents/images/mapMask3.png")); Image transpImg = TransformGrayToTransparency(m_images[1]); m_images[2] = ApplyTransparency(m_images[0], transpImg); } private Image TransformGrayToTransparency(BufferedImage image) { ImageFilter filter = new RGBImageFilter() { public final int filterRGB(int x, int y, int rgb) { return (rgb &lt;&lt; 8) &amp; 0xFF000000; } }; ImageProducer ip = new FilteredImageSource(image.getSource(), filter); return Toolkit.getDefaultToolkit().createImage(ip); } private BufferedImage ApplyTransparency(BufferedImage image, Image mask) { BufferedImage dest = new BufferedImage( image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = dest.createGraphics(); g2.drawImage(image, 0, 0, null); AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.DST_IN, 1.0F); g2.setComposite(ac); g2.drawImage(mask, 0, 0, null); g2.dispose(); return dest; } </code></pre> <p>The remainder just display the images in a little Swing panel.<br> Note that the mask image is gray levels, black becoming full transparency, white becoming full opaque.</p> <p>Although you have resolved your problem, I though I could share my take on it. It uses a slightly more Java-ish method, using standard classes to process/filter images.<br> Actually, my method uses a bit more memory (making an additional image) and I am not sure it is faster (measuring respective performances could be interesting), but it is slightly more abstract.<br> At least, you have choice! :-)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload