Background
I have the following png
file that contains the player sprite.
As with any png
file, the non-colored pixels are transparent.
(Width: 16px
; Height: 16px
)
Currently, I can erect a rectangle (collision-bounds), using java.awt.Rectangle
around the image using the image's width and height, as seen below.
I would like to crop the image so that the width and height are changed to the edges of the colored portion of the image, much like what is shown below.
This will allow for more accurate collision-detection between entities or objects in a game.
I want to construct a method that handles this modification dynamically, as seen below.
//Method calculates how far in from [top, bottom, left and right]
//the [non-zero] or [transparent] pixel-data of an image are
//in order to construct a new image that starts at the edges
//of the colored portion of the image.
public BufferedImage adjustImage(BufferedImage image)
{
BufferedImage result = null;
return result;
}
Problem
However, I am uncertain as to the algorithm to use to achieve the result above.
Can anyone give a step-by-step on how to detect these bounds and how to implement such a fuction as described above?