I think this code demonstrates what you’re asking for. I’m not saying this is the best way to do collision detection. I’m just trying to help answer your question.
Explanation: Notice that the red, green, blue values are not used here (just for demonstration). The alphaThreshold
is arbitrary [0…255]. If using bitmask transparency, then just set this to zero. If using translucent transparency (for example, if your graphics have anti-aliased edges), setting this higher will avoid collision detection on very transparent parts. The meaning of add to collision matrix is whatever data structure you want to keep for the collision detection. (My original method actually constructs a rectangular bounding box for every non-transparent pixel in the image, but I edited that out for demonstration, and because you’re asking about per-pixel transparency detection.)
int R_BAND = 0;
int G_BAND = 1;
int B_BAND = 2;
int A_BAND = 3;
int alphaThreshold = 64;
int w = image.getWidth();
int h = image.getHeight();
Raster raster = bufferedImage.getRaster();
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
int r = raster.getSample(x,y,R_BAND);
int g = raster.getSample(x,y,G_BAND);
int b = raster.getSample(x,y,B_BAND);
int a = raster.getSample(x,y,A_BAND);
if (a > alphaThreshold)
{
// add to collision matrix
}
}
}