/** * Simple Picture manipulation * * @author Pat Troy: troy AT uic DOT edu */ // 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 Lect1006g { public static void main (String[] args) { // Access and open the picture String filename = FileChooser.pickAFile (); Picture p = new Picture (filename); // call the method to modify the Pictture modifyPicture (p); // explore (display) the picture p.explore(); // get a new filename and save the picture String saveFilename = FileChooser.pickAFile (); p.write (saveFilename); } // end of main public static void modifyPicture (Picture p) { // get the width and height of the picture int width = p.getWidth(); int height = p.getHeight(); System.out.println ("Width: " + width + ", Height: " + height); // Set up a loop to access all the X position int xPos; int yPos; Pixel pix; int redAmount; int greenAmount; int blueAmount; int grayAmount; for ( xPos = 0 ; xPos < width ; ++xPos ) { for ( yPos = 0 ; yPos < height ; ++yPos ) { // access the pixel to be modifed pix = p.getPixel (xPos, yPos); // modify the pixel redAmount = pix.getRed (); greenAmount = pix.getGreen (); blueAmount = pix.getBlue (); grayAmount = (int)(redAmount * 0.299 + greenAmount * 0.587 + blueAmount * 0.114); pix.setRed (grayAmount); pix.setGreen (grayAmount); pix.setBlue (grayAmount); } } } // end of modifyPicture } // end of class