/** * 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 import java.io.File; public class Lect1025d { public static void main (String[] args) { // Access and open the first picture String filename = FileChooser.pickAFile (); System.out.println (filename); int pos = filename.lastIndexOf (File.separatorChar); String temp1 = filename.substring(0, pos+1); System.out.println (temp1); String name1 = temp1 + "flower1.jpg"; String name2 = temp1 + "flower2.jpg"; System.out.println (name1); System.out.println (name2); Picture p = new Picture (name1); // Access and open the second picture Picture p2 = new Picture (name2); // call the method to modify the Pictture Picture p3; p3 = modifyPicture (p, p2); Picture p4; p4 = modifyPicture (p3, p); // explore (display) the picture p4.explore(); // get a new filename and save the picture //String saveFilename = FileChooser.pickAFile (); //p2.write (saveFilename); } // end of main public static Picture modifyPicture (Picture p1, Picture p2) { // get the width and height of the picture int width1 = p1.getWidth(); int height1 = p1.getHeight(); System.out.println ("Width: " + width1 + ", Height: " + height1); // get the width and height of the picture int width2 = p2.getWidth(); int height2 = p2.getHeight(); System.out.println ("Width: " + width2 + ", Height: " + height2); // determine the width and height of the result int width3 = width1 + width2; int height3; if ( height1 > height2 ) { height3 = height1; } else { height3 = height2; } // create the new Picture that will be returned Picture resultPicture; resultPicture = new Picture (width3, height3); // Set up a loop to access all the X position int xPos; int yPos; int xNew; int yNew; Pixel pix1; Pixel pix2; Pixel pix3; int red; int green; int blue; for ( xPos = 0 ; xPos < width1 ; ++xPos ) { for ( yPos = 0 ; yPos < height1 ; ++yPos ) { // access the pixel to be modifed pix1 = p1.getPixel (xPos, yPos); xNew = xPos; yNew = yPos + (height3 - height1); pix3 = resultPicture.getPixel (xNew, yNew); // get the colr values from the original pixel red = pix1.getRed(); green = pix1.getGreen(); blue = pix1.getBlue(); // set those color values into the result pixel pix3.setRed(red); pix3.setGreen(green); pix3.setBlue(blue); } } for ( xPos = 0 ; xPos < width2 ; ++xPos ) { for ( yPos = 0 ; yPos < height2 ; ++yPos ) { // access the pixel to be modifed pix2 = p2.getPixel (xPos, yPos); xNew = xPos + width1; yNew = yPos + (height3 - height2); pix3 = resultPicture.getPixel (xNew, yNew); // get the colr values from the original pixel red = pix2.getRed(); green = pix2.getGreen(); blue = pix2.getBlue(); // set those color values into the result pixel pix3.setRed(red); pix3.setGreen(green); pix3.setBlue(blue); } } return resultPicture; } // end of modifyPicture } // end of class