LAB 11 – Modifying Pictures Using Loops (part 2 –Changing Color Values)

Lab Exercises

Topics

n      To change color values

 

Exercises

4.3 Changing Color Values

 

Type in the following code in the Program window (changing the comments):

 

/* Program that modifies pictures using loops

 * Picture and Pixel classes are defined in bookClasses developed

 * at Georgia Tech by Mark Guzdial / Barbara Ericson

 *

 * @author Wayne Summers

 * @date   Sept. 14, 2007

 */

 

public class ModifyPictures

{

  public static void main(String[] args)

  {

    String fileName;

    fileName = FileChooser.pickAFile();  // note that this pops up a window to select a jpg file

 

    Picture pic;

    pic = new Picture(fileName);

    pic.show();

  

  }

}

 

4.3.1 Using a For-Each Loop

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

  public static void decreaseRed(Picture pict)

    /**

     * Method that decreases the amount of redness in the picture

     * @param pict - picture Object to be modified

    */

  {

    Pixel[] pixelArray = pict.getPixels();

    int value = 0;

 

    // loop through all the pixels in the array

    for (Pixel pixelObj : pixelArray)

    {

 

      // get the red value

      value = pixelObj.getRed();

 

      // decrease the red value by 50% (1/2)

      value = (int) (value * 0.5);

 

      // set the red value of the current pixel to the new value

      pixelObj.setRed(value);

    }

  }

 

ii) Type in the following code in the Program window in the main method and before the pic.show();

 

decreaseRed(pic);

 

iii) Test your program. What happens if you use 0.25 instead of 0.5? 0.75?

 

 

4.3.2 Using While Loops

4.3.3 Increasing/Decreasing Red(Green, Blue)

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

    /**

    * Method that increases the amount of blueness in the picture

    * @param pict - picture Object to be modified

    */

public static void increaseBlue(Picture pict)

{

    Pixel[] pixelArray = pict.getPixels();

    Pixel pixelObj = null;

    int value = 0;

    int index = 0;

 

    // loop through all the pixels

    while (index < pixelArray.length)

    {

      // get the current pixel

      pixelObj = pixelArray[index];

      // get the value

      value = pixelObj.getBlue();

 

      // increase value

      value = (int) (value * 1.3);

 

      // set the blue value

      pixelObj.setBlue(value);

 

      // increment the index

      index++;

    }

  }

 

ii) Type in the following code in the Program window in the main method and before the pic.show();

 

increaseBlue(pic);

 

iii) Test your program. What happens if you use 1.1 instead of 1.3? 1.6?

 

4.3.4 Creating a Sunset

4.3.5 Making Sense of Methods

4.3.6 Variable Name Scope

 

 

QUESTIONS:

Write a program that reduces the redness of the picture by 10% and increases the greenness of the picture by 20%.

 

Submit the .java file through the DropBox in WebCT.

 

Challenge1: How would you use a second parameter for increaseBlue to be able to adjust the amount to change?

 

Challenge2: How would you use increaseBlue and increaseGreen to make a picture look like a sunset?