/** * 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 Lect1021a { 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); System.out.println (pict1); filename = FileChooser.pickAFile(); System.out.println (filename); Picture pict2 = new Picture(filename); System.out.println (pict2); // call the method to modify the picture Picture anotherPicture; anotherPicture = createBlankCanvas (pict1, pict2); System.out.println (anotherPicture); // copy the pixels from pict1 into anotherPicture anotherPicture = copyPicture2 (pict1, anotherPicture, 0, 0); anotherPicture = copyPicture2 (pict2, anotherPicture, pict1.getWidth(), 0); // 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 createBlankCanvas (Picture p1, Picture p2) { // get the height and width of my two pictures int width1 = p1.getWidth(); int height1 = p1.getHeight(); int width2 = p2.getWidth(); int height2 = p2.getHeight(); // calcuate the width needed for the final picture int width3 = width1 + width2; int height3; if ( height1 > height2 ) height3 = height1; else height3 = height2; // create and return the new picture of the proper size Picture blankCanvas = new Picture (width3, height3); return blankCanvas; } public static Picture copyPicture (Picture pictA, Picture pictC) { // get the width and height of the picture int width = pictA.getWidth(); int height = pictA.getHeight(); // 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 red = pA.getRed(); int green = pA.getGreen(); int blue = pA.getBlue(); // calculate the new position for the pixel information int newXPos, newYPos; newXPos = xLoopCounter; newYPos = yLoopCounter; // save the modified color values Pixel pC = pictC.getPixel (newXPos, newYPos); 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 public static Picture copyPicture2 (Picture pictA, Picture pictC, int offsetX, int offsetY) { // get the width and height of the picture int width = pictA.getWidth(); int height = pictA.getHeight(); // 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 red = pA.getRed(); int green = pA.getGreen(); int blue = pA.getBlue(); // calculate the new position for the pixel information int newXPos, newYPos; newXPos = xLoopCounter + offsetX; newYPos = yLoopCounter + offsetY; // save the modified color values Pixel pC = pictC.getPixel (newXPos, newYPos); 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 public static Picture modifyPicture (Picture pictA, int startX, int startY, int endX, int endY) { // get the width and height of the picture int width = pictA.getWidth(); int height = pictA.getHeight(); // determine the width and height of the cropped portion int cWidth = endX - startX; int cHeight = endY - startY; // create a new Picture to store the grayScale image Picture pictC = new Picture (cWidth, cHeight); // 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 = startX ; xLoopCounter < endX ; xLoopCounter++ ) { // the inner loop varies the Y position (which row the pixel is in) for ( yLoopCounter = startY ; yLoopCounter < endY ; yLoopCounter++ ) { // access a pixel and its colors from the first picture Pixel pA = pictA.getPixel (xLoopCounter, yLoopCounter); int red = pA.getRed(); int green = pA.getGreen(); int blue = pA.getBlue(); // calculate the new position for the pixel information int newXPos, newYPos; newXPos = xLoopCounter - startX; newYPos = yLoopCounter - startY; // save the modified color values Pixel pC = pictC.getPixel (newXPos, newYPos); 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