






General crop method
/**
* Method to create a new picture by cropping the
* current picture to the size given by startX,
* startY, endX, and endY
* @param startX the x value to start with
* @param startY the y value to start with
* @param endX the last x value to use
* @param endY the last y value to use
* @return a new picture with just the cropped pixels
*/
public Picture crop(int startX, int startY,
int endX, int endY)
{
int width = endX - startX + 1;
int height = endY - startY + 1;
Picture targetPicture = new Picture(width,height);
Pixel sourcePixel = null;
Pixel targetPixel = null;
// copy the pixels in the cropped area
for (int x = startX, tx = 0;
x <= endX && x < this.getWidth();
x++,tx++)
{
for (int y = startY, ty = 0;
y <= endY && y < this.getHeight();
y++,ty++)
{
sourcePixel = this.getPixel(x,y);
targetPixel = targetPicture.getPixel(tx,ty);
targetPixel.setColor(sourcePixel.getColor());
}
}
return targetPicture;
}
Link to this Page