CS 140 - Lab 8, Thursday October 23

Objectives

  1. Experiment with your own Picture methods.

Picture Methods

Create a new Java class named Lab8 and save it to your P: drive in a file named Lab8.java. Add a main method and make sure it compiles before you proceed.

  1. When we call our blackAndWhite method as in
    pic.blackAndWhite();
    
    it changes the picture pic, the object to the left of the "." . Modify your blackAndWhite method so that it doesn't modify the actual picture object but is a function that returns a new picture that is black and white. For example the sequence:
        Picture pic = new Picture(path + "sunset.jpg");
        Picture pic2 = pic.blackAndWhite();
        pic.repaint();
        pic2.repaint();
    
    should show both the color and black and white version of the sunset.
  2. Does the following work?
        Picture pic = new Picture(path + "sunset.jpg");
        pic.blackAndWhite().repaint();
    
    Notice that we are calling the repaint method on the object that is returned from blackAndWhite.
  3. Write a Picture method named enlarge() that enlarges a picture by doubling its width and height. This method should return the new larger picture. Here is an example of how you would call it.
        Picture pic = new Picture(path + "sunset.jpg");
        pic.enlarge().repaint();
      
  4. Now that shrink and enlarge are methods that return pictures what happens when we do the following? What does the picture look like?
      pic.smaller().smaller().bigger().bigger().repaint();
    
  5. Write a method named decreaseRed() that decreases the amount of red in the picture by a percentage that is passed as an actual argument. This method should return a new picture. For example, pic2 = pic.decreaseRed(1) the picture pic2 should be a picture with all of the red in pic removed. Calling pic2 = pic.decreaseRed(.25) should decrease the red by 25%.
  6. Write a method named flipVertical() that returns a new picture that is the picture flipped up-side-down.
       Picture pic = new Picture(path + "sunset.jpg");
       pic.flipVertical().repaint();
    
  7. Write a method named copyRegion(x,y,width,height) that copies a region of a picture starting at the upper left coordinate (x,y) for width and height pixels. For example
        Picture pic = new Picture(path + "fish.jpg");
        pic.copyRegion(214,3,170,80).repaint();    
    
    should display a fish extracted from a larger picture.
  8. What does the following code snippet show? (show Ed or Dan if you think it is correct).
        Picture pic = new Picture(path + "comic.jpg");
        pic.copyRegion(10,60,210,20).flipVertical().enlarge().decreaseRed(1).repaint();