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 Eclipse and import the cs116 project. 
     
  3. In the last lab we used loops to modify ALL of the pixels in a picture.  Now we will examine loops more closely so that we can modify just portions of an image.  Suppose in the image caterpillar.jpg we wish to to set pixels (100,100), (101,101), ... ,(124,124) to black.  We could write 25 individual statements to set the colors or we could use a for loop as follows:

    for (int i=0; i<25; i++){
        picture.getPixel(100+i,100+i).setColor(Color.black);
    }


    Modify MyProgram so that it reads in a file and performs the loop above, and finally shows the picture.
     
  4. Assignment:
    Modify the program above so that it draws a vertical line and a horizontal line of 25 pixels in the picture starting at (100,100).


     

  5. 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, ii=x; i<w; i++,ii++) {
         for (int j=0, jj=y; j<h; j++, jj++) {
            pixel = result.getPixel(i,j);
            pixel.setColor(this.getPixel(ii,jj).getColor());
        
    }
      }

      return result;
    }
     

    Add this method to the Picture class and try it out.
     


  6.  
    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.


     


  7.  
    Assignment:
    Write a program Collage.java to create a collage of images.  Your collage should contain at least 3 images. You may use methods that are described in your text.  In addition you must implement at least three additional methods for the Picture class.  Here are some suggestions but feel free to think of your own (just clear them with me first).
    • Overlay: Place one image overtop of another.  You should provide the (x,y) coordinates to specify the upper/left corner of the overlay position. 
       
    • Rotation:  Specify the angle of rotation and rotate the image counter clockwise.
      Hints:  Create a new picture of dimensions size x size,  larger than the original with the original copied into the center. This will allow for the corners of the image when rotated. You can crop to the original size after the rotation.
      To pixel[i,j] in the rotated image will be taken from pixel[x,y] in the original where x and y are computed from the formuli:
      x =(i+cornerx-size/2)*cos(angle) - (j+cornery-size/2)*sin(angle) + size/2
      y =(i+cornerx-size/2)*sin(angle) + (j+cornery-size/2)*cos(angle) + size/2
      where size x size is the dimension of the larger image, (cornerx,cornery)  is the location of the upper/left corner of the copied original in the larger image.
       
    • Swirl:  Change rotation so that the amount of rotation (degrees) changes as a function of the radius.  This way some things are rotated more or less as they are farther away from the center points, creating a swirl effect.
       
    • Copy a triangular area of a picture (rather than a rectangle).
       
    • Copy a circular area of a picture (rather than a rectangle).
      Hint:  The equation of a circle is x*x + y*y = radius*radius
       
    • CarnivalMirror:  Mirrors either distort horizontally or vertically but usually not both.  So you can choose one or implement both.  Consider the horizontal distortion:  the vertical distances are all left unchanged but the horizontal features are either expanded or compressed from the mid axis running vertically through the image.  You can use a sine function to nicely imitate this gradual compression/expansion.

    Many of the methods will create images that exceed the dimensions of the jpg file.  Just ignore these parts so that the new image stays rectangular.

    Use the write method to save the resulting collage image.


     

  8. Email your modified code Picture.java, MyProgram.java, Collage.java, and your resulting collage to me at jill.zimmerman@goucher.edu