General right rotation method
/**
* Method to copy the current picture to a new
* picture but rotated to the right 90 degrees
* @return the new rotated picture
*/
public Picture copyWithRightRotation()
{
Picture targetPicture = new Picture(this.getHeight(),
this.getWidth());
Pixel sourcePixel = null;
Pixel targetPixel = null;
// loop through the columns
for (int sourceX = 0;
sourceX < this.getWidth();
sourceX++)
{
// loop through the rows
for (int sourceY = 0;
sourceY < this.getHeight();
sourceY++)
{
// set the target pixel color to the source pixel color
sourcePixel = this.getPixel(sourceX,sourceY);
targetPixel = targetPicture.getPixel(
this.getHeight() - 1 - sourceY, sourceX);
targetPixel.setColor(sourcePixel.getColor());
}
}
return targetPicture;
}
Here is a revised copyKatieRightRotation method that uses the general right rotation method
/**
* Method to copy Katie rotated to the left 90 degrees
* @return the picture after Katie has been copied and rotated to the left 90
*/
public static Picture copyKatieRightRotation()
{
String sourceFile = Picture.getMediaPath("KatieFancy.jpg");
Picture sourcePicture = new Picture(sourceFile);
String targetFile = Picture.getMediaPath("7inx95in.jpg");
Picture targetPicture = null;
// do the right rotation
targetPicture = sourcePicture.copyWithRightRotation();
// show the source and target pictures
sourcePicture.show();
targetPicture.show();
return targetPicture;
}
Link to this Page