* specify an offset y coordinate.
* @return Calculated graphics path
*/
public static Region calculateControlGraphicsPath(ImageData data,
int transparentPixel, int offsetX, int offsetY) {
Region region = new Region();
if (data == null)
return region;
int colorTransparentPixel;
// Use the top left pixel as our transparent color
if (transparentPixel <= 0) {
if (data.transparentPixel == -1)
colorTransparentPixel = data.getPixel(0, 0);
else
colorTransparentPixel = data.transparentPixel;
} else
colorTransparentPixel = transparentPixel;
// This is to store the column value where an opaque pixel is first
// found.This value will determine where we start scanning for trailing
// opaque pixels.
int colOpaquePixel = 0;
int width = data.width;
int height = data.height;
// Go through all rows (Y axis)
for (int row = 0; row < height; row++) {
// Reset value
colOpaquePixel = 0;
// Go through all columns (X axis)
for (int col = 0; col < width; col++) {
// If this is an opaque pixel, mark it and search for anymore
// trailing behind
if (data.getPixel(col, row) != colorTransparentPixel) {
// Opaque pixel found, mark current position
colOpaquePixel = col;
// Create another variable to set the current pixel position
int colNext = col;
// Starting from current found opaque pixel, search for
// anymore opaque pixels trailing behind, until a
// transparent pixel is found or minimum width is reached
for (colNext = colOpaquePixel; colNext < width; colNext++)
if (data.getPixel(colNext, row) == colorTransparentPixel)
break;
// Form a rectangle for line of opaque pixels found and add
// // it to our graphics path
region.add(new int[] { offsetX + colOpaquePixel,
offsetY + row, offsetX + colNext, offsetY + row,
offsetX + colNext, offsetY + row + 1,
offsetX + colOpaquePixel, offsetY + row + 1 });
// No need to scan the line of opaque pixels just found