How do I set a transparent color in a picture? ;D
Here you go, have fun :
/**
* Gets the image pixels in a one dimension array
* @param img the original image
* @return int[] the image's pixels
*/
public static final int[] getPixels(Image img) {
final int[] pix = new int[img.getWidth(null) * img.getHeight(null)];
final PixelGrabber pg =
new PixelGrabber(
img,
0,
0,
img.getWidth(null),
img.getHeight(null),
pix,
0,
img.getWidth(null));
try {
pg.grabPixels();
} catch (InterruptedException e) {
}
return pix;
}
/**
* Converts a unidimensional array of pixels into an image
* @param pixels the image's pixels
* @param w the image width
* @param h the image height
* @return Image the resulting image
*/
public static final Image getImage(int[] pixels, int w, int h) {
return Toolkit.getDefaultToolkit().createImage(
new MemoryImageSource(w, h, pixels, 0, w));
}
/**
* This applies an alpha mask, removing all images coicident to argument's
* Color. This doesn't alter the original image.
* @param img the image you want to transform
* @param keyColor the color you want to become invisible
* @return Image the resulting image
*/
public static final Image applyKeyColorMask(Image img, Color keyColor) {
final int w = img.getWidth(null);
final int h = img.getHeight(null);
final int[] pxls = getPixels(img);
final int blank = new Color(255, 0, 0, 0).getRGB();
for (int i = 0; i < pxls.length; i++) {
if (pxls[i] == keyColor.getRGB()) {
pxls[i] = blank;
}
}
return getImage(pxls, w, h);
}