CS116      Lab 3 - Modifying Pixels in a Matrix

Objectives:

  1. Before starting this lab you should have read chapter 5 in your text.
     
  2. Open DrJava and the Picture class. 
     
  3. We can nest one loop inside another loop.  The following method uses nested loops to crop a picture. 

    /* Method to return a cropped image
    * @params  x, y is the upper left hand corner of the sub-image
    * @param  w is the width of the sub-image
    * @param  h is the height of the sub-image
    * @return a the sub-image at (x,y) of size w by h
    */

    public Picture crop(int x, int y, int w, int h){
      Picture result = new Picture(w,h);
      Pixel pixel;
      for (int i = 0; i<w; i++) {
         for (int j=0; j<h; j++) {
            pixel = result.getPixel(i,j);
            pixel.setColor(this.getPixel(i+x,j+y).getColor());
        
    }
      }

      return result;
    }
     

    Add this method to the Picture class and try it out.  Make sure you understand how this code works!  Why are we getting pixel (i+x,j+y) from the original picture and then changing the pixel at (i,j) in the resulting picture?
     


  4.  
    Assignment:
    Write a method for Picture that flips a picture over so that things that were on the left are now on the right.  You will want to create a new picture and copy the pixels from the passed-in picture to the new picture.  Return the new picture.

    This is NOT the same as the mirror example in the textbook.  I want the entire picture flipped and displayed as a single image.


     

  5. Assignment:
    Write a method for Picture that overlays one picture on top of another picture.  It should not modify "this" picture but instead make a copy of "this" into a resulting picture which will serve as the base image.  It will then overlay a smaller picture, smallPic, at with its upper left corner positioned at (x,y) in the base image.

    public Picture overlay(Picture smallPic, int x, int y)


     


  6.  
    Assignment:
    Write a method for Picture that crops a triangluar region of a picture given the x,y coordinates of the upper left corner in the original picture and the height of the cropped region.  You should return a new picture rather than modifying the orginal, "this", picture.

    public Picture triangleCrop(int x, int y, int h)

                          


     


  7.  
    Assignment:
    Write a method for Picture that squishes a picture vertically (or horizontally).

    Hint:  Copy every other row (or column)


     


  8.  
    Assignment:
    Write a program Collage.java which contains a main program to create a collage of images.  Your collage should contain at least 3 images.  You should use overlay and any other of the picture methods that we have created or are in the textbook.


    Use the write method to save the resulting collage image to a file.


     

  9. Send your modified code Picture.java, Collage.java, and your resulting collage to me using GoucherLearn.