public static java.awt.Image ApplyAlphaMask(java.awt.Image original, java.awt.Image alphamask) {
    int width = original.getWidth(null), height = original.getHeight(null);
    if (width <= 0) width = 1;
    if (height <= 0) height = 1;
    BufferedImage resultImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    BufferedImage image = toBufferedImage(original);
    BufferedImage mask = toBufferedImage(alphamask);
    for (int y = 0; y < image.getHeight(null); y++) {
      for (int x = 0; x < image.getWidth(null); x++) {
        try {
          Color c = new Color(image.getRGB(x, y));
          Color maskC = new Color(mask.getRGB(x, y));
          Color maskedColor = new Color(c.getRed(), c.getGreen(), c.getBlue(), maskC.getRed());
          resultImg.setRGB(x, y, maskedColor.getRGB());
        } catch (Exception e) {

        }
      }
    }
    return toImage(resultImg);
  }
示例#2
0
  public static java.awt.image.BufferedImage loadBufferedImageFromResources(
      Component c, String filename) {

    try {
      Misc m = new Misc();
      java.awt.Image img = loadImageFromResources(filename);
      MediaTracker mt = new MediaTracker(c);
      mt.addImage(img, 0);
      mt.waitForID(0);
      int width = img.getWidth(null);
      int height = img.getHeight(null);
      BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
      Graphics gg = bi.getGraphics();
      gg.drawImage(img, 0, 0, null);
      gg.dispose();
      return bi;
    } catch (Exception ex) {
      System.out.println(ex.toString());
    }
    return null;
  }