/** * 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 Lect1014b { 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 xPos; int yPos; // the outer loop varies the X position (which column the pixel is in) for (xPos = 0 ; xPos < width ; xPos++) { // the inner loop varies the Y position (which row the pixel is in) for (yPos = 0 ; yPos < height ; yPos++) { // access a pixel Pixel p = pict1.getPixel (xPos, yPos); // access the color values from the pixel int red = p.getRed(); int green = p.getGreen(); int blue = p.getBlue(); // check if the color lumenance if less than 128 if ( red < 128 ) { red = 0; } else { red = 255; } if ( green < 128 ) { green = 0; } else { green = 255; } if ( blue < 128 ) { blue = 0; } else { blue = 255; } // save the modified color values p.setRed(red); p.setGreen(green); p.setBlue(blue); } } } // end of the method to modify the picture } // end of Lect914b class