/** * Class for creating a template for a simple Java program * * @author Pat Troy: troy AT uic DOT edu */ // import the library with the color information import java.awt.Color; // Note the name of the class in the following line MUST // match the name of the file. This is stored in a file // named: Template.java public class Lect107h { public static void main (String[] args) { System.out.println("Begin Java Exection"); System.out.println(""); // Select a picture for the turtle to draw on String filename = FileChooser.pickAFile(); System.out.println (filename); Picture pict1 = new Picture(filename); // call the method to modify the picture Picture anotherPicture; anotherPicture = modifyPicture (pict1); // display the picture anotherPicture.show(); // Save the picture //String fname; //fname = FileChooser.pickAFile(); //System.out.println ("File: " + filename); //pict1.write (fname); System.out.println(""); System.out.println("End Java Exection"); } // end of the method main public static Picture modifyPicture (Picture pict1) { // get the width and height of the picture int width = pict1.getWidth(); int height = pict1.getHeight(); //System.out.println ("Width: " + width); //System.out.println ("Height: " + height); // create a new Picture to store the grayScale image Picture pict2 = new Picture (width, height); // set up a nested loop (one loop inside another) to access each pixel int xLoopCounter; int yLoopCounter; // the outer loop varies the X position (which column the pixel is in) xLoopCounter = 0; while (xLoopCounter < width) { // the inner loop varies the Y position (which row the pixel is in) yLoopCounter = 0; while (yLoopCounter < height) { // access a pixel Pixel p1 = pict1.getPixel (xLoopCounter, yLoopCounter); // access the color values from the pixel int red = p1.getRed(); int green = p1.getGreen(); int blue = p1.getBlue(); // modify the color value as needed int grayAmount = (int)(red * 0.299 + green * 0.587 + blue * 0.114); red = grayAmount; green = grayAmount; blue = grayAmount; // save the modified color values Pixel p2 = pict2.getPixel (xLoopCounter, yLoopCounter); p2.setRed(red); p2.setGreen(green); p2.setBlue(blue); yLoopCounter++; } xLoopCounter++; } // add the code to return the newly created picture return pict2; } // end of the method to modify the picture } // end of Lect914b class