code: public static BufferedImage composeMaskedImage (
          ImageCreator isrc, BufferedImage mask, BufferedImage base)
      {
          int wid = base.getWidth();
          int hei = base.getHeight();
  
          Raster maskdata = mask.getData();
          Raster basedata = base.getData();
  
          // create a new image using the rasters if possible
          if (maskdata.getNumBands() == 4 && basedata.getNumBands() >= 3) {
              WritableRaster target = basedata.createCompatibleWritableRaster(wid, hei);
  
              // copy the alpha from the mask image
              int[] adata = maskdata.getSamples(0, 0, wid, hei, 3, (int[]) null);
              target.setSamples(0, 0, wid, hei, 3, adata);
  
              // copy the RGB from the base image
              for (int ii=0; ii < 3; ii++) {
                  int[] cdata = basedata.getSamples(0, 0, wid, hei, ii, (int[]) null);
                  target.setSamples(0, 0, wid, hei, ii, cdata);
              }
  
              return new BufferedImage(mask.getColorModel(), target, true, null);
  
          } else {
              // otherwise composite them by rendering them with an alpha
              // rule
              BufferedImage target = isrc.createImage(wid, hei, Transparency.TRANSLUCENT);
              Graphics2D g2 = target.createGraphics();
              try {
                  g2.drawImage(mask, 0, 0, null);
                  g2.setComposite(AlphaComposite.SrcIn);
                  g2.drawImage(base, 0, 0, null);
              } finally {
                  g2.dispose();
              }
              return target;
          }
      }
docs: Create an image using the alpha channel from the first and the RGB values from the second.