CS 140 - Homework 6 - Due Feb 13

Name:______________________________________


Read Chapter 5 up to and including Section 5.1 in your book. Pages 131-142.

Answer the following questions:

  1. This section is about using loops to modify some of the pixels in an image. This differs from our past image programs where we modified every pixel in the entire image. Again, you should implement every program written in this chapter to get a good understanding of the material.
  2. In the reading, loops are used to iterate across pixels in an image. What will the following loop, that has nothing to do with pictures, print when executed? Hint, cut and paste this into a main class and let it run. Make sure you understand what it is doing.
        String word1 = "Loops are";
        String word2 = "really"; 
        int value = 1332; 
        System.out.println(word1); 
        for (int i = 0; i < 5; i++) {     
          System.out.println(word2);
          value++; 
        } 
        System.out.println(value); 
      
  3. In one brief sentence, explain what the following method for the Picture class does:
        public void mystery() {
          for (int x = 0; x < this.getWidth()/2; x++) {
            for (int y = 0; y < this.getHeight()/2; y++) {
              this.getPixel(x,y).setColor(Color.black);
            }
          }
        }
    			

    Hint: cut and paste this into your Picture class and call it from main on an example picture.

  4. Below is the definition of the method mirrorVertical from page 135-136 of your text. Make sure you understand what it does and how it does it. Hint: cut and paste it into the Picture class and call it from the main class using an example picture.
        public void mirrorVertical() {
          int width = this.getWidth();
          int mirrorPoint = width/2;
       
          for (int y = 0; y < this.getHeight(); y++) {
            for (int x = 0; x < mirrorPoint; x++) {
              Pixel leftPixel = this.getPixel(x,y);
              Pixel rightPixel = this.getPixel(width - 1 - x, y);
              rightPixel.setColor(leftPixel.getColor());
            }
          }
        }
    			

    Modify this function so that the top 10 rows, bottom 10 rows, and left and right 10 columns of pixels are left unmirrored. This will create a frame of unmirrored pixels around the final image.

    When working on this problem, try to make one modification at a time. For example, first try to modify the function so only the top 10 rows of pixels are unmodified, then try to work on the bottom 10, and so on.

    Turn in a printout of your modified method.