The following method (for the Picture class) is the plain
copy method. Remark that we do check that both source and target
coordinates remain within their bounds.
public void copyPicture(Picture picSource)
{
for (int sourceX = 0, targetX = 0;
sourceX < picSource.getWidth() && targetX < this.getWidth();
sourceX++, targetX++)
{
for (int sourceY = 0, targetY = 0;
sourceY < picSource.getHeight() && targetY < this.getHeight();
sourceY++, targetY++)
{
Pixel pixSource = picSource.getPixel(sourceX, sourceY);
Pixel pixDest = this.getPixel(targetX, targetY);
pixDest.setColor(pixSource.getColor());
}
}
}
Next, we have the method that copies at a given location
in the target (the parameters targetStartX and
targetStartY).
public void copyPictureTo(Picture picSource, int targetStartX, int targetStartY)
{
for (int sourceX = 0, targetX = targetStartX;
sourceX < picSource.getWidth() && targetX < this.getWidth();
sourceX++, targetX++)
{
for (int sourceY = 0, targetY = targetStartY;
sourceY < picSource.getHeight() && targetY < this.getHeight();
sourceY++, targetY++)
{
Pixel pixSource = picSource.getPixel(sourceX, sourceY);
Pixel pixDest = this.getPixel(targetX, targetY);
pixDest.setColor(pixSource.getColor());
}
}
}
Finally, here is the most complete version: it takes as input 4 extra parameters, that specify which area in the source picture will be copied.
public void copyPictureTo(Picture picSource,
int targetStartX, int targetStartY,
int sourceStartX, int sourceStartY,
int sourceEndX, int sourceEndY)
{
for (int sourceX = sourceStartX, targetX = targetStartX;
sourceX < picSource.getWidth() && sourceX < sourceEndX && targetX < this.getWidth();
sourceX++, targetX++)
{
for (int sourceY = sourceStartY, targetY = targetStartY;
sourceY < picSource.getHeight() && sourceY < sourceEndY && targetY < this.getHeight();
sourceY++, targetY++)
{
Pixel pixSource = picSource.getPixel(sourceX, sourceY);
Pixel pixDest = this.getPixel(targetX, targetY);
pixDest.setColor(pixSource.getColor());
}
}
}