LAB 12 – Modifying Pictures Using Loops (part 3 –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.7 Using a For Loop

4.3.8 Lightening and Darkening

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

/**

  * Method to lighten the colors in a picture

  * @param pict - picture Object to be modified

 */

public static void lighten(Picture pict)

{

 

    Color color = null;

    Pixel pixel = null;

 

    // get the array of pixels

    Pixel[] pixelArray = pict.getPixels();

   

    // loop through all the pixels

    for (int i = 0; i < pixelArray.length; i++)

    {

      // get the current pixel

      pixel = pixelArray[i];

     

      // get the current color

      color = pixel.getColor();

     

      // select a lighter color

      color = color.brighter();

     

      //set the pixel color to the lighter color

      pixel.setColor(color);

    }

  }

 

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

 

lighten(pic);

 

iii) Test your program.

 

4.3.9 Creating a Negative

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

  /**

  * Method to negate the picture

  * @param pict - picture Object to be modified

  */

  public static void negate(Picture pict)

  {

    Pixel[] pixelArray = pict.getPixels();

    Pixel pixelObj = null;

    int redValue, blueValue, greenValue = 0;

 

    // loop through all the pixels

    for (int i = 0; i < pixelArray.length; i++)

    {

      // get the current pixel

      pixelObj = pixelArray[i];

 

      // get the values

      redValue = pixelObj.getRed();

      greenValue = pixelObj.getGreen();

      blueValue = pixelObj.getBlue();

 

      // set the pixel's color

      pixelObj.setColor(new Color(255 - redValue,

                               255 - greenValue,

                               255 - blueValue));

    }

  }

 

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

 

negate(pic);

 

iii) Test your program.

 

4.3.10 Converting to Grayscale

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

  /**

  * Method to change the picture to gray scale

  * @param pict - picture Object to be modified

  */

  public static void grayscale(Picture pict)

  {

    Pixel[] pixelArray = pict.getPixels();

    Pixel pixelObj = null;

    int intensity = 0;

 

    // loop through all the pixels

    for (int i = 0; i < pixelArray.length; i++)

{

      // get the current pixel

      pixelObj = pixelArray[i];

 

      // compute the average intensity

      intensity =   (pixelObj.getRed() +

                      pixelObj.getGreen() +

                      pixelObj.getBlue()) / 3;

 

      // set the pixel color

      pixelObj.setColor(new Color(intensity,intensity,intensity));

    }

  }

 

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

 

grayscale(pic);

 

iii) Test your program.

 

QUESTIONS:

Write a program that uses a method that modifies the red, green, and blue values of a picture by different amounts.

 

Submit the .java file through the DropBox in WebCT.