LAB 14 - Modifying Pictures in a Matrix (part 2 - Copying and Transforming Pictures)

Topics

n      To copy and transform pixels 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.1 Copying

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

 

/**

  * Method that a source picture onto a target picture

  * @param sourcePic - picture Object to be copied

  * @param targetPic – picture Object to be copied onto

 */

public static void copyPicture(Picture sourcePic, Picture targetPic)

{

   Pixel sourcePixel = null;

   Pixel targetPixel = null;

 

   // loop through the columns

   for (int sourceX = 0, targetX = 0; sourceX < sourcePicture.getWidth(); sourceX++, targetX++)  

   {

     // loop through the rows

     for (int sourceY = 0, targetY = 0; sourceY < sourcePicture.getHeight(); sourceY++, targetY++)  

     {

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

       sourcePixel = sourcePic.getPixel(sourceX,sourceY);

       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();

 

copyPicture(sourcePicture, targetPicture);

 

iii) Test your program.

 

5.2.2 Creating a Collage

 

i) Add two integer parameters to copyPicture for the starting x and y locations on the target picture

 

ii) Use these parameters in the counted For statements as the starting values for targetX and targetY.

 

iii) Declare the two integer arguments in the main method and assign them values so that the source image will fit on the target image at the indicated location.

 

iv) Test your program.

 

QUESTIONS:

Modify your program so that displays two different images (or one image with two different versions) on the 7inx95in.jpg canvas.

 

Does it matter which order you place the images?

 

CHALLENGE: Write a method that places an image in each corner of the canvas.

 

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