private BufferedImage convertToTransparent(BufferedImage src,
Color transparentColor) {
int w = src.getWidth();
int h = src.getHeight();
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
BufferedImage dest = gc.createCompatibleImage(w, h, Transparency.BITMASK);
int trgb;
if (transparentColor != null) {
trgb = transparentColor.getRGB() | 0xff000000;
} else {
trgb = 0xffff00ff;
}
// Copy pixels a scan line at a time
int buf[] = new int[w];
for (int y = 0; y < h; y++) {
src.getRGB(0, y, w, 1, buf, 0, w);
for (int x = 0; x < w; x++) {
if (buf[x] == trgb) {
buf[x] = 0;
}
}
dest.setRGB(0, y, w, 1, buf, 0, w);
}
return dest;
}