/** * 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 Lect107f { 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 modifyPicture (pict1); // display the picture pict1.explore(); // 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 void 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); // 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 p = pict1.getPixel (xLoopCounter, yLoopCounter); // access the color values from the pixel int red = p.getRed(); int green = p.getGreen(); int blue = p.getBlue(); // modify the color value as needed int grayAmount = (red + green + blue) / 3; red = grayAmount; green = grayAmount; blue = grayAmount; // save the modified color values p.setRed(red); p.setGreen(green); p.setBlue(blue); yLoopCounter++; } xLoopCounter++; } } // end of the method to modify the picture } // end of Lect914b class