/** * 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 Lect1012c { public static void main (String[] args) { System.out.println("Begin Java Exection"); System.out.println(""); // Select two pictures String filename = FileChooser.pickAFile(); System.out.println (filename); Picture pict1 = new Picture(filename); filename = FileChooser.pickAFile(); System.out.println (filename); Picture pict2 = new Picture(filename); // call the method to modify the picture Picture anotherPicture; anotherPicture = modifyPicture (pict1, pict2); // 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 pictA, Picture pictB) { // get the width and height of the picture int width = pictA.getWidth(); int height = pictA.getHeight(); //System.out.println ("Width: " + width); //System.out.println ("Height: " + height); // create a new Picture to store the grayScale image Picture pictC = 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) for ( xLoopCounter = 0 ; xLoopCounter < width ; xLoopCounter++ ) { // the inner loop varies the Y position (which row the pixel is in) for ( yLoopCounter = 0 ; yLoopCounter < height ; yLoopCounter++ ) { // access a pixel and its colors from the first picture Pixel pA = pictA.getPixel (xLoopCounter, yLoopCounter); int redA = pA.getRed(); int greenA = pA.getGreen(); int blueA = pA.getBlue(); // access a pixel and its colors from the second picture Pixel pB = pictB.getPixel (xLoopCounter, yLoopCounter); int redB = pB.getRed(); int greenB = pB.getGreen(); int blueB = pB.getBlue(); // modify the color value as needed double percentOfWidth = (1.0 * xLoopCounter) / (double)(width); // we want float division int red = (int)((1.0 - percentOfWidth)*redA + percentOfWidth*redB); int green = (int)((1.0 - percentOfWidth)*greenA + percentOfWidth*greenB); int blue = (int)((1.0 - percentOfWidth)*blueA + percentOfWidth*blueB); // save the modified color values Pixel pC = pictC.getPixel (xLoopCounter, yLoopCounter); pC.setRed(red); pC.setGreen(green); pC.setBlue(blue); } } // add the code to return the newly created picture return pictC; } // end of the method to modify the picture } // end of Lect914b class