title: Overlapping of images in java
body: <p>I want to overlap a black image containing some white spots on a grey shade image.I want to make this black image transparent. I used the code mentioned <a href="https://i.stack.imgur.com/foVZ1.jpg" rel="nofollow noreferrer">here</a> in Java.</p>
  
  <p>But the output shows a red image. Why?</p>
  
  <p>The code is mentioned here :</p>
  
  <pre><code>    public class OverlayImage{
          public static void main(String[] args) {
              File path = new File("/home/dmdd/Desktop/images"); // base path of the images
  
              // load source images
              BufferedImage image=null;
              BufferedImage overlay=null;
              try {
                  image = ImageIO.read(new File(path, "image.jpg"));
                  overlay = ImageIO.read(new File(path, "overlay.jpg"));
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
              //overlay =(BufferedImage) makeImageTransparent(overlay,50);
              OverlayImage over=new OverlayImage();
              // create the new image, canvas size is the max. of both image sizes
              int w = Math.max(image.getWidth(), overlay.getWidth());
              int h = Math.max(image.getHeight(), overlay.getHeight());
              //BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  
              Image overlay1=over.TransformGrayToTransparency(overlay);
              BufferedImage combined=over.ApplyTransparency(image, overlay1);
              try {
                  ImageIO.write(over.toBufferedImage(combined, w, h), "JPG", new File(path, "combined.jpg"));
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
  
          }
          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 white spot image is:
  <img src="https://i.stack.imgur.com/foVZ1.jpg" alt="enter image description here"></p>
  
  <p>The black image is:
  <img src="https://i.stack.imgur.com/jzlm3.jpg" alt="enter image description here"></p>
  
  <p>The overlayed red image is:
  <img src="https://i.stack.imgur.com/UM5Pt.jpg" alt="enter image description here"></p>