hello everyone…
i’m getting sick of this graphics stuff
i just want to change my gif image (256 color with transparent) to png image (24bit with alphamask)
i rip all this code to make transparent png
but it makes the game very very slow!!
then i use BufferedImage, and the game fps back to normal (same as like using the gif with automatic transparent image)
but the images loading took very long time!! (not the game fps, only the init images)
anybody can share any png masking code?
and if u don’t mind, tell me what’s wrong with this code!
okay, here is the code :
public static Image getBitmaskImage(GraphicsConfiguration gc, MediaTracker tracker,
String imageName,
Color keyColor) {
Image image = Toolkit.getDefaultToolkit().getImage(imageName);
tracker.addImage(image, 0);
try { tracker.waitForID(0);
} catch (Exception e) { }
// works fine below 5 images
Image alphaImage = applyMask(image, keyColor);
// with BufferedImage the game fps back to normal
// but takes very long time to load the images
/*BufferedImage bufferedImage = gc.createCompatibleImage(
alphaImage.getWidth(null), alphaImage.getHeight(null),
Transparency.BITMASK);
Graphics g = (Graphics2D) bufferedImage.getGraphics();
g.drawImage(alphaImage, 0,0,null, null);
g.dispose();*/
return alphaImage;
// return bufferedImage;
}
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;
}
public static final Image getImage(int[] pixels, int w, int h) {
return Toolkit.getDefaultToolkit().createImage(
new MemoryImageSource(w, h, pixels, 0, w));
}
public static final Image applyMask(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] = pxls[i] & 0x00ffffff;
//pxls[i] = blank;
}
}
return getImage(pxls, w, h);
}
thanxx