Nested Loops part 2 Barb Ericson Georgia Institute
Nested Loops – part 2 Barb Ericson Georgia Institute of Technology Nov 2009 Nested. Loops-part 2 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 Nested. Loops-part 2 3
Mirror temple method /** * Method to mirror part of the temple picture around a * vertical line at a mirror point */ public void mirror. Temple() { int mirror. Point = 276; Pixel left. Pixel = null; Pixel right. Pixel = 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 < mirror. Point; x++) Nested. Loops-part 2 4
Mirror temple - continued { left. Pixel = get. Pixel(x, y); right. Pixel = get. Pixel(mirror. Point + (mirror. Point - x), y); right Pixel. set. Color(left. Pixel. get. Color()); } } } Nested. Loops-part 2 5
Testing the mirror. Temple method > String file. Name = "C: /intro-prog-java/mediasources/temple. jpg"; > Picture picture = new Picture(file. Name); > picture. explore(); > picture. mirror. Temple(); > picture. explore(); Nested. Loops-part 2 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 – num. Outer = 96 – 27 + 1 = 70 – num. Inner = 275 – 13 + 1 = 263 – Total for a nested loop is num. Inner * num. Outer • 70 * 263 = 18, 410 Nested. Loops-part 2 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 – 640 x 480. jpg – 7 in. X 95 in. 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 source. X = 0, target. X = 0; source. X < source. Picture. get. Width(); source. X++, target. X++) { // loop through the rows for (int source. Y = 0, target. Y = 0; source. Y < source. Picture. get. Height(); source. Y++, target. Y++) { Georgia Institute of Technology
Copy Algorithm to Code – Cont • Get the source and target pixels source. Pixel = source. Picture. get. Pixel(source. X, source. Y); target. Pixel = this. get. Pixel(target. X, target. Y); • Set the color of the target pixel to the color of the source pixel target. Pixel. set. Color(source. Pixel. get. Color()); Georgia Institute of Technology
Copy Method public void copy. Katie() { String source. File = File. Chooser. get. Media. Path("Katie. Fancy. jpg"); Picture source. Picture = new Picture(source. File); Pixel source. Pixel = null; Pixel target. Pixel = null; // loop through the columns for (int source. X = 0, target. X = 0; source. X < source. Picture. get. Width(); source. X++, target. X++) { Georgia Institute of Technology
Copy Method - Continued // loop through the rows for (int source. Y = 0, target. Y = 0; source. Y < source. Picture. get. Height(); source. Y++, target. Y++) { // set the target pixel color to the source pixel color source. Pixel = source. Picture. get. Pixel(source. X, source. Y); target. Pixel = this. get. Pixel(target. X, target. Y); target. Pixel. set. Color(source. Pixel. get. Color()); } } } Georgia Institute of Technology
Trying the copy. Katie Method • Create a picture object using the 7 in. X 95 in. jpg file in the mediasources directory – Picture p 1 = new Picture(File. Chooser. get. Media. Path(“ 7 in. X 95 in. jpg”)); • Show the picture – p 1. show(); • Invoke the method on this picture object – p 1. copy. Katie(); • Repaint the picture – p 1. repaint(); Georgia Institute of Technology
Result of copy. Katie 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 start. X and start. Y • What is an algorithm that will do this? 1 0 1 2 3 Georgia Institute of Technology 1 3 2 4
Copy to Position Exercise • Write a method copy. Robot to copy – robot. jpg – To location • 100, 100 in 7 inx 95 in. jpg • Test with String file = File. Chooser. get. Media. Path(“ 7 inx 95 in. jpg”); Picture p = new Picture(file); p. copy. Robot(); 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 picture. Obj. explore() to find the x and y values – What are the x and y values to get the face of the girl in Katie. Fancy. jpg?
Copy Face Method public void copy. Katies. Face() { String source. File = File. Chooser. get. Media. Path("Katie. Fancy. jpg"); Picture source. Picture = new Picture(source. File); Pixel source. Pixel = null; Pixel target. Pixel = null; // loop through the columns for (int source. X = 70, target. X = 100; source. X < 135; source. X++, target. X++) { // loop through the rows for (int source. Y = 3, target. Y = 100; source. Y < 80; source. Y++, target. Y++) {
Copy Face Method - Continued // set the target pixel color to the source pixel color source. Pixel = source. Picture. get. Pixel(source. X, source. Y); target. Pixel = this. get. Pixel(target. X, target. Y); target. Pixel. set. Color(source. Pixel. get. Color()); } } }
Testing Copy Katie’s Face • Create a picture object – Picture p 1 = new Picture(File. Chooser. get. Media. Path( “ 7 in. X 95 in. jpg”)); • Show the picture – p 1. show(); • Invoke the method – p 1. copy. Katies. Face(); • Repaint the picture – p 1. 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 flower 1. jpg and flower 2. 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 copy. Flowers. Top() { // create the flower pictures Picture flower 1 Picture = new Picture(File. Chooser. get. Media. Path( "flower 1. jpg")); Picture flower 2 Picture = new Picture(File. Chooser. get. Media. Path( "flower 2. jpg")); // declare the source and target pixel variables Pixel source. Pixel = null; Pixel target. Pixel = null; // copy the first flower picture to the top left // corner for (int source. X = 0, target. X = 0; source. X < flower 1 Picture. get. Width(); source. X++, target. X++) { for (int source. Y = 0, target. Y = 0; source. Y < flower 1 Picture. get. Height(); source. Y++, target. Y++) { source. Pixel = flower 1 Picture. get. Pixel(source. X, source. Y); target. Pixel = this. get. Pixel(target. X, target. Y); target. Pixel. set. Color(source. Pixel. get. Color()); } }
Create Collage Method - cont // copy the flower 2 picture starting with x = // 100 for (int source. X = 0, target. X = 100; source. X < flower 2 Picture. get. Width(); source. X++, target. X++) { for (int source. Y = 0, target. Y = 0; source. Y < flower 2 Picture. get. Height(); source. Y++, target. Y++) { source. Pixel = flower 2 Picture. get. Pixel(source. X, source. Y); target. Pixel = this. get. Pixel(target. X, target. Y); target. Pixel. set. Color(source. Pixel. get. Color()); } } // copy the flower 1 negated to x = 200 flower 1 Picture. negate(); for (int source. X = 0, target. X = 200; source. X < flower 1 Picture. get. Width(); source. X++, target. X++) { for (int source. Y = 0, target. Y = 0; source. Y < flower 1 Picture. get. Height(); source. Y++, target. Y++) { source. Pixel = flower 1 Picture. get. Pixel(source. X, source. Y); target. Pixel = this. get. Pixel(target. X, target. Y); target. Pixel. set. Color(source. Pixel. get. Color()); } }
Create Collage Method - cont // clear the blue in flower 2 picture and add at // x=300 flower 2 Picture. clear. Blue(); for (int source. X = 0, target. X = 300; source. X < flower 2 Picture. get. Width(); source. X++, target. X++) { for (int source. Y = 0, target. Y = 0; source. Y < flower 2 Picture. get. Height(); source. Y++, target. Y++) { source. Pixel = flower 2 Picture. get. Pixel(source. X, source. Y); target. Pixel = this. get. Pixel(target. X, target. Y); target. Pixel. set. Color(source. Pixel. get. Color()); } } // copy the negated flower 1 to x=400 for (int source. X = 0, target. X = 400; source. X < flower 1 Picture. get. Width(); source. X++, target. X++) { for (int source. Y = 0, target. Y = 0; source. Y < flower 1 Picture. get. Height(); source. Y++, target. Y++) { source. Pixel = flower 1 Picture. get. Pixel(source. X, source. Y); target. Pixel = this. get. Pixel(target. X, target. Y); target. Pixel. set. Color(source. Pixel. get. Color()); } } }
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 source. X, source. Y and target. X and target. Y • You can declare, initialize, and change more than one variable in a for loop for (int source. X = 0, target. X = 0; source. X < source. Picture. get. Width(); source. X++, target. X++) • You can copy just part of a picture to another picture Georgia Institute of Technology
- Slides: 27