I have a sprite sheet which has each image centered in a 32x32 cell. The actual images are not 32x32, but slightly smaller. What I\'d like to do is take a cell and crop the tr
If your sheet already has transparent pixels, the BufferedImage
returned by getSubimage()
will, too. The default Graphics2D
composite rule is AlphaComposite.SRC_OVER
, which should suffice for drawImage()
.
If the sub-images have a distinct background color, use a LookupOp with a four-component LookupTable
that sets the alpha component to zero for colors that match the background.
I'd traverse the pixel raster only as a last resort.
Addendum: Extra transparent pixels may interfere with collision detection, etc. Cropping them will require working with a WritableRaster directly. Rather than working from the center out, I'd start with the borders, using a pair of getPixels()
/setPixels()
methods that can modify a row or column at a time. If a whole row or column has zero alpha, mark it for elimination when you later get a sub-image.