Nested Loops – part 2 Barb Ericson Georgia Institute of Technology Nov 2009 NestedLoops-part2 1 Learning Goals • Understand at a conceptual and practical level – How to mirror part of a picture – How to calculate the number of times a nested loop will execute – How to copy pixels from one picture to another – How to declare, initialize and change several variables in a for loop – How to copy part of a picture to another picture Georgia Institute of Technology Mirror part of a picture • Use the explorer to figure out the part to mirror NestedLoops-part2 3 Mirror temple method /** * Method to mirror part of the temple picture around a * vertical line at a mirror point */ public void mirrorTemple() { int mirrorPoint = 276; Pixel leftPixel = null; Pixel rightPixel = null; // loop through the rows for (int y = 27; y < 97; y++) { // loop from 13 to just before the mirror point for (int x = 13; x < mirrorPoint; x++) NestedLoops-part2 4 Mirror temple - continued { leftPixel = getPixel(x, y); rightPixel = getPixel(mirrorPoint + (mirrorPoint - x), y); right Pixel .setColor(leftPixel .getColor()); } } } NestedLoops-part2 5 Testing the mirrorTemple method > String fileName = "C:/intro-prog-java/mediasources/temple.jpg"; > Picture picture = new Picture(fileName); > picture.explore(); > picture.mirrorTemple(); > picture.explore(); NestedLoops-part2 6 How many pixels were changed? • We could put print statements in the code – to see the x and y values, but that would take a long time • We can also just keep a count – And print the value of the count after the loop ends • We can calculate the number of times a loop will run using end – start + 1 = number of times – numOuter = 96 – 27 + 1 = 70 – numInner = 275 – 13 + 1 = 263 – Total for a nested loop is numInner * numOuter • 70 * 263 = 18,410 NestedLoops-part2 7 Copying Pixels to a New Picture • Need to track the source picture x and y – And the target picture x and y 1 2 3 4 • We can use a blank picture – As the target picture • Several blank pictures are available 1 3 – 640x480.jpg – 7inX95in.jpg Georgia Institute of Technology 2 4 Copy Picture Algorithm • Copy a picture to the 7 by 9.5 inch blank picture – Create the target picture object – Invoke the method on the target picture • Create the source picture object • Loop through the source picture pixels – Get the source and target pixels – Set the color of the target pixel to the color of the source pixel Georgia Institute of Technology Copy Algorithm to Code • Loop through the source pixels // loop through the columns for (int sourceX = 0, targetX = 0; sourceX < sourcePicture.getWidth(); sourceX++, targetX++) { // loop through the rows for (int sourceY = 0, targetY = 0; sourceY < sourcePicture.getHeight(); sourceY++, targetY++) { Georgia Institute of Technology Copy Algorithm to Code – Cont • Get the source and target pixels sourcePixel = sourcePicture.getPixel(sourceX,sourceY); targetPixel = this.getPixel(targetX,targetY); • Set the color of the target pixel to the color of the source pixel targetPixel.setColor(sourcePixel.getColor()); Georgia Institute of Technology Copy Method public void copyKatie() { String sourceFile = FileChooser.getMediaPath("KatieFancy.jpg"); Picture sourcePicture = new Picture(sourceFile); Pixel sourcePixel = null; Pixel targetPixel = null; // loop through the columns for (int sourceX = 0, targetX = 0; sourceX < sourcePicture.getWidth(); sourceX++, targetX++) { Georgia Institute of Technology Copy Method - Continued // loop through the rows for (int sourceY = 0, targetY = 0; sourceY < sourcePicture.getHeight(); sourceY++, targetY++) { // set the target pixel color to the source pixel color sourcePixel = sourcePicture.getPixel(sourceX,sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } } } Georgia Institute of Technology Trying the copyKatie Method • Create a picture object using the 7inX95in.jpg file in the mediasources directory – Picture p1 = new Picture(FileChooser.getMediaPath(“7inX95in.jpg”)); • Show the picture – p1.show(); • Invoke the method on this picture object – p1.copyKatie(); • Repaint the picture – p1.repaint(); Georgia Institute of Technology Result of copyKatie Method Georgia Institute of Technology Copying Pixels to a New Picture • What if we want to copy the target to a different location in the source 0 0 1 2 1 3 4 – Than 0,0 – Say startX and startY • What is an algorithm that will do this? 1 0 1 2 3 0 1 2 3 Georgia Institute of Technology 1 3 2 4 Copy to Position Exercise • Write a method copyRobot to copy – robot.jpg – To location • 100, 100 in 7inx95in.jpg • Test with String file = FileChooser.getMediaPath(“7inx95in.jpg”); Picture p = new Picture(file); p.copyRobot(); p.show(); Georgia Institute of Technology Cropping • We can copy just part of a picture to a new picture – Just change the start and end source x and y values to the desired values – Use pictureObj.explore() to find the x and y values – What are the x and y values to get the face of the girl in KatieFancy.jpg? Copy Face Method public void copyKatiesFace() { String sourceFile = FileChooser.getMediaPath("KatieFancy.jpg"); Picture sourcePicture = new Picture(sourceFile); Pixel sourcePixel = null; Pixel targetPixel = null; // loop through the columns for (int sourceX = 70, targetX = 100; sourceX < 135; sourceX++, targetX++) { // loop through the rows for (int sourceY = 3, targetY = 100; sourceY < 80; sourceY++, targetY++) { Copy Face Method - Continued // set the target pixel color to the source pixel color sourcePixel = sourcePicture.getPixel(sourceX,sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } } } Testing Copy Katie’s Face • Create a picture object – Picture p1 = new Picture(FileChooser.getMediaPath( “7inX95in.jpg”)); • Show the picture – p1.show(); • Invoke the method – p1.copyKatiesFace(); • Repaint the picture – p1.repaint(); Creating a Collage • You can create a collage by copying several pictures to a blank picture – You can use the general copy method on flower1.jpg and flower2.jpg Create Collage Method /** * Method to copy flower pictures to create a * collage. * All the flower pictures will be lined up near the * top of the current picture */ public void copyFlowersTop() { // create the flower pictures Picture flower1Picture = new Picture(FileChooser.getMediaPath( "flower1.jpg")); Picture flower2Picture = new Picture(FileChooser.getMediaPath( "flower2.jpg")); // declare the source and target pixel variables Pixel sourcePixel = null; Pixel targetPixel = null; // copy the first flower picture to the top left // corner for (int sourceX = 0, targetX = 0; sourceX < flower1Picture.getWidth(); sourceX++, targetX++) { for (int sourceY = 0, targetY = 0; sourceY < flower1Picture.getHeight(); sourceY++, targetY++) { sourcePixel = flower1Picture.getPixel(sourceX,sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } } Create Collage Method - cont // copy the flower2 picture starting with x = // 100 for (int sourceX = 0, targetX = 100; sourceX < flower2Picture.getWidth(); sourceX++, targetX++) { for (int sourceY = 0, targetY = 0; sourceY < flower2Picture.getHeight(); sourceY++, targetY++) { sourcePixel = flower2Picture.getPixel(sourceX, sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } } // copy the flower1 negated to x = 200 flower1Picture.negate(); for (int sourceX = 0, targetX = 200; sourceX < flower1Picture.getWidth(); sourceX++, targetX++) { for (int sourceY = 0, targetY = 0; sourceY < flower1Picture.getHeight(); sourceY++, targetY++) { sourcePixel = flower1Picture.getPixel(sourceX, sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } } Create Collage Method - cont // clear the blue in flower 2 picture and add at // x=300 flower2Picture.clearBlue(); for (int sourceX = 0, targetX = 300; sourceX < flower2Picture.getWidth(); sourceX++, targetX++) { for (int sourceY = 0, targetY = 0; sourceY < flower2Picture.getHeight(); sourceY++, targetY++) { sourcePixel = flower2Picture.getPixel(sourceX,sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } } // copy the negated flower 1 to x=400 for (int sourceX = 0, targetX = 400; sourceX < flower1Picture.getWidth(); sourceX++, targetX++) { for (int sourceY = 0, targetY = 0; sourceY < flower1Picture.getHeight(); sourceY++, targetY++) { sourcePixel = flower1Picture.getPixel(sourceX, sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } } } Challenge • Create your own collage – Copy at least two different pictures to the collage – Do at least 3 different picture manipulations to the pictures • Reduce red • Negate • Clear blue – Mirror the collage Summary • To mirror part of a picture – Set the starting and ending values in the nested loop • To copy pixels from one picture to another – Keep track of the sourceX, sourceY and targetX and targetY • You can declare, initialize, and change more than one variable in a for loop for (int sourceX = 0, targetX = 0; sourceX < sourcePicture.getWidth(); sourceX++, targetX++) • You can copy just part of a picture to another picture Georgia Institute of Technology