NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009.

Slides:



Advertisements
Similar presentations
A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.
Advertisements

Conditionals-part31 Conditionals – part 3 Barb Ericson Georgia Institute of Technology Nov 2009.
ManipulatingPictures-Mod6-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology.
Georgia Institute of Technology Movies part 3 Barb Ericson Georgia Institute of Technology April 2006.
Conditionals-part11 Barb Ericson Georgia Institute of Technology Nov 2009.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 6 Barb Ericson Georgia Institute of Technology August 2005.
TOPIC 9 MODIFYING PIXELS IN A MATRIX: COPYING, CROPPING 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
CSE 8A Lecture 8 Reading for next class: None Prepare for In-term exam 2 PSA4: Collage and Picture Flip, DON’T WAIT (it’s longer than the previous PSAs)
Copying and Transforming Pictures. First, finding the min or max… Next homework asks you to write a function to find the darkest and lightest shade of.
Georgia Institute of Technology Introduction to Media Computation Barb Ericson Georgia Institute of Technology May 2006.
How to use the Java class libraries Brief documentation of how to do this all with Java.
TOPIC 7 MODIFYING PIXELS IN A MATRIX NESTED FOR LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by.
NestedLoops-part11 Nested Loops – part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures.
Georgia Institute of Technology Movies part 5 Barb Ericson Georgia Institute of Technology April 2006.
Georgia Institute of Technology Processing Sound Ranges Barb Ericson Georgia Institute of Technology July 2005.
UsingSoundRanges-part21 Processing Sound Ranges part 2 Barb Ericson Georgia Institute of Technology Oct 2009.
TOPIC 11 RETURNING VALUES FROM METHODS PICTURE TRANSFORMATIONS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
CSE 8A Lecture 8 Reading for next class: PSA4: Collage and Picture Flip, DON’T WAIT (it’s longer than the previous PSAs)
1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.
NestedLoops-Mod7-part31 Two-Dimensional Arrays and Nested Loops – part 3 Bugs in the garden Originally by Barb Ericson Georgia Institute of Technology.
ManipulatingPictures-Mod6-part61 Manipulating Pictures, Arrays, and Loops: Eliminating color, Inversion, grey scale and adjusting for luminance Barb Ericson.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 5 Barb Ericson Georgia Institute of Technology August 2005.
NestedLoops-part41 Nested Loops – part 4 Barb Ericson Georgia Institute of Technology Nov 2009.
NestedLoops-Mody7-part51 Two-Dimensional Arrays and Nested Loops – part 5 Rotations Barb Ericson Georgia Institute of Technology May 2007.
Conditionals-part41 Conditionals – part 4 Barb Ericson Georgia Institute of Technology Dec 2009.
CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.
Conditionals-part21 Conditionals – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
Georgia Institute of Technology What is new in Java 5.0 (1.5)? Barb Ericson Georgia Institute of Technology June 2006.
NestedLoops-part21 Nested Loops – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
CSC 112Introduction to Media Computation 1 Rotating Images.
CS1315: Introduction to Media Computation Transforming pictures by index number.
ManipulatingPictures-part31 Manipulating Pictures, Arrays, and Loops part 3 Barb Ericson Georgia Institute of Technology Nov 2009.
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 2 Barb Ericson Georgia Institute of Technology August 2005.
NestedLoops-Mod7-part61 Two-Dimensional Arrays and Nested Loops – part 6 Enlarge Barb Ericson Georgia Institute of Technology August 2005.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 4 Barb Ericson Georgia Institute of Technology August 2005.
Topic 9 Modifying Pixels in a Matrix: Copying, Cropping
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Workshop for Programming And Systems Management Teachers
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Georgia Institute of Technology
Working with ranges in pictures
Manipulating Pictures, Arrays, and Loops
Barb Ericson Georgia Institute of Technology August 2005
Two-Dimensional Arrays and Nested Loops – part 1
Barb Ericson Georgia Institute of Technology August 2005
Two-Dimensional Arrays and Nested Loops – part 5
Two-Dimensional Arrays and Nested Loops – part 1
Manipulating Pictures, Arrays, and Loops part 4
Processing Sound Ranges part 1
Two-Dimensional Arrays and Nested Loops – part 4
Two-Dimensional Arrays and Nested Loops – part 3
Two-Dimensional Arrays and Nested Loops – part 6
Two-Dimensional Arrays and Nested Loops – part 2
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops
Two-Dimensional Arrays and Nested Loops – part 6
Manipulating Pictures, Arrays, and Loops
Processing Sound Ranges part 2
Processing Sound Ranges
CSC1401 Viewing a picture as a 2D image - 2
Two-Dimensional Arrays and Nested Loops – part 6
Barb Ericson Georgia Institute of Technology May 2006
Manipulating Pictures, Arrays, and Loops
Processing Sound Ranges part 3
Manipulating Pictures, Arrays, and Loops part 6
Presentation transcript:

NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009

NestedLoops-part32 Learning Goals Understand at a conceptual and practical level –What makes a good method? –How to write more general methods? By adding parameters to the method –How to rewrite methods? To reduce copied code –How to blend two pictures together Like blending two sounds

NestedLoops-part33 What makes a Good Method? A method should do one and only one thing –Accomplish some task –The name should tell you what it does A method can call other methods to do some of the work –Procedural decomposition We shouldn’t copy code between methods –We should make general methods that are reusable A method should be in the class that has the data the method is working on

NestedLoops-part34 Where the last two methods general? We specified the file to copy from in the method –Meaning we would need to change the method –or make another method –to copy a different picture

NestedLoops-part35 General Copy Algorithm Create a method that copies pixels from a passed source picture –Giving a start x and y and end x and y for the source picture If the start x and y and end x and y cover the entire picture then the whole picture will be copied If the start x and y and end x and y are part of the picture then cropping will occur –To the current picture object with a target start x and target start y If the start x and y are 0 then it copies to the upper left corner

NestedLoops-part36 General Copy Algorithm Loop through the x values between xStart and xEnd Loop through the y values between yStart and yEnd Get the pixel from the source picture for the current x and y values Get the pixel from the target picture for the targetStartX + x and targetStartY + y values Set the color in the target pixel to the color in the source pixel

NestedLoops-part37 General Copy Method public void copy(Picture sourcePicture, int startX, int startY, int endX, int endY, int targetStartX, int targetStartY) { Pixel sourcePixel = null; Pixel targetPixel = null; // loop through the x values for (int x = startX, tx = targetStartX; x < endX; x++, tx++) { // loop through the y values for (int y = startY, ty = targetStartY; y < endY; y++, ty++) {

NestedLoops-part38 General Copy Method - Continued // copy the source color to the target color sourcePixel = sourcePicture.getPixel(x,y); targetPixel = this.getPixel(tx,ty); targetPixel.setColor(sourcePixel.getColor()); }

NestedLoops-part39 Creating a Collage We can use the general copy method to make it easier to create an image collage.

NestedLoops-part310 Collage Method /** * Method to copy two flowers in a pattern to the * top of the current picture */ public void copyFlowersBetter() { // create the flower pictures Picture flower1Picture = new Picture(FileChooser.getMediaPath("flower1.jpg")); Picture flower2Picture = new Picture(FileChooser.getMediaPath("flower2.jpg")); // copy the first flower picture this.copyPicture(flower1Picture,0);

NestedLoops-part311 Collage Method - continued // copy the flower2 picture starting with x = 100 this.copyPicture(flower2Picture,100); // copy the flower1 negated to x = 200 in the canvas flower1Picture.negate() ; this.copyPicture(flower1Picture,200); /* clear the blue in flower 2 picture and * add at x=300 in the canvas */ flower2Picture.clearBlue(); this.copyPicture(flower2Picture,300); // copy the negated flower 1 to x=400 this.copyPicture(flower1Picture,400); }

NestedLoops-part312 Challenge Rewrite your collage method to use the general copy method

NestedLoops-part313 Blend Pictures If we want to blend two pictures we need to take 50% of the red from one picture and add it to 50% of the red from the other picture –And do the same for blue and green

NestedLoops-part314 Blend Pictures Algorithm Create the two pictures to blend Copy the pixels from the first part of picture1 –First 150 columns from picture1 X loops from 0 and stops when equal to 150 Y loops from 0 and stops when equal to the picture1 height Copy the rest of the columns from picture1 blended with the pixels from picture2 –Set the color to a new color that is a combination of half of the red from each pictures, half of the green from each picture and half of the blue from each picture –X loops from 150 to the width of picture1 –Y loops from 0 to the height of picture1 Copy the rest of the pixels from picture2

NestedLoops-part315 Blend Pictures Method public void blendPictures() { // create the sister pictures Picture katiePicture = new Picture(FileChooser.getMediaPath("KatieFancy.jpg")); Picture jennyPicture = new Picture(FileChooser.getMediaPath("JenParty.jpg")); // declare the source and target pixel variables Pixel katiePixel = null; Pixel jennyPixel = null; Pixel targetPixel = null; /* declare the target x and source x since we will need * the values after the for loop */ int sourceX = 0; int targetX = 0;

NestedLoops-part316 blendPictures() continued // copy the first 150 pixels of katie to the canvas for (; sourceX < 150; sourceX++, targetX++) { for (int sourceY=0, targetY=0; sourceY < katiePicture.getHeight(); sourceY++, targetY++) { katiePixel = katiePicture.getPixel (sourceX,sourceY) ; targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(katiePixel.getColor()); }

NestedLoops-part317 blendPictures() continued /* copy 50% of katie and 50% of jenny till * the end of katie’s width */ for (; sourceX < katiePicture.getWidth(); sourceX++, targetX++) { for (int sourceY=0,targetY=0; sourceY < katiePicture.getHeight(); sourceY++, targetY++) { katiePixel = katiePicture.getPixel(sourceX,sourceY); jennyPixel = jennyPicture.getPixel(sourceX - 150,sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor( new Color((int) (katiePixel.getRed() * jennyPixel.getRed() * 0.5), (int) (katiePixel.getGreen() * jennyPixel.getGreen() * 0.5), (int) (katiePixel.getBlue() * jennyPixel.getBlue() * 0.5))); }

NestedLoops-part318 blendPictures() continued // copy the rest of Jenny sourceX = sourceX - 150; for (; sourceX < jennyPicture.getWidth(); sourceX++, targetX++) { for (int sourceY = 0, targetY = 0; sourceY < jennyPicture.getHeight(); sourceY++, targetY++) { jennyPixel = jennyPicture.getPixel(sourceX,sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(jennyPixel.getColor()); }

NestedLoops-part319 Trying out blendPictures() > String fileName = FileChooser.getMediaPath("640x480.jpg"); > Picture picture = new Picture(fileName); > picture.blendPictures(); > picture.show();

NestedLoops-part320 For Loop Syntax The general for loop syntax is: for (init area ; continuation test; change area) { Each part of this loop is optional –But the semicolons are required You can only have 2 semicolons We didn't put anything in the init area since we wanted to keep using the values of sourceX and targetX

NestedLoops-part321 Challenge Write a method to blend two pictures together –And only use 25% of one picture's color added to 75% of the other picture's color Also blend the entire pictures together –Best to use two pictures of the same size

NestedLoops-part322 Summary To copy part of one picture to another –Change the start and end conditions in the for loop A good method should do one and only one thing Use parameters to make methods more reusable Don’t copy code from one method to another –Create a general method instead –And call the general method from other methods You can blend pictures together