LAB 15 - Modifying Pictures in a Matrix (part 3 - Copying and Transforming Pictures)

Topics

n      To copy and transform pixels (rotating image) in a matrix (picture) using nested loops

 

5.2 Copying and Transforming Pictures

Type in the following code in the Program window (changing the comments):

 

/* Program that copies and transforms pictures using nested loops

 * Picture and Pixel classes are defined in bookClasses developed

 * at Georgia Tech by Mark Guzdial / Barbara Ericson

 *

 * @author Wayne Summers

 * @date   Sept. 19, 2007

 */

 

public class CopyPictures

{

  public static void main(String[] args)

  {

    String fileName1, fileName2;

    System.out.println(“Find the source file”);

    fileName1 = FileChooser.pickAFile();  // note that this pops up a window to select a jpg file

 

    Picture sourcePicture;

    sourcePicture = new Picture(fileName1);

 

    System.out.println(“Find the target file”);

    fileName2 = FileChooser.pickAFile();  // note that this pops up a window to select a jpg file

 

    Picture targetPicture;

   targetPicture = new Picture(fileName2);

 

// calls to methods that manipulate images go here

 

   targetPicture.show(); 

  }

}

 

5.2.4 Rotation

i) Type in the following code in the Program window below the main method and before the last }:

 

/**

  * Method that copies and rotates a source picture left onto a target picture

  * @param sourcePic - picture Object to be copied

  * @param targetPic – picture Object to be copied onto

 */

  public static void copyPictureLeftRotation (Picture sourcePic, Picture targetPic)

  {

      Pixel sourcePixel = null;

      Pixel targetPixel = null;

      int targetX, targetY = 0;

      int width = sourcePic.getWidth();

      int height = sourcePic.getHeight();

 

      // loop through the columns

      for (int sourceX = 0; sourceX < width; sourceX++)  

      {

          // loop through the rows

          for (int sourceY = 0; sourceY < height; sourceY++)  

          {

              // set the target pixel color to the source pixel color

              sourcePixel = sourcePic.getPixel(sourceX,sourceY);

              targetX = sourceY;

              targetY = width - 1 - sourceX;

              targetPixel = targetPic.getPixel(targetX,targetY);

              targetPixel.setColor(sourcePixel.getColor());

          }

      }

 }

 

ii) Type in the following code in the Program window in the main method and before the pic.show();

 

copyPictureLeftRotation (sourcePicture, targetPicture);

 

iii) Test your program.

 

 

iv) Rewrite copyPictureLeftRotation as copyPictureRightRotation by assigning (height - 1- sourceY) to targetX and sourceX to targetY

 

v) Type in the following code in the Program window in the main method and before the pic.show();

 

copyPictureRightRotation (sourcePicture, targetPicture);

 

vi) Test your program.

 

 

 

QUESTIONS:

Rewrite the copyPictureLeftRotation as copyPicture180Rotation.

 

Could you have done two “copyPictureLeftRotation” instead?

 

 

Submit the .java file and the answer to the question through the DropBox in WebCT.