04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.

Slides:



Advertisements
Similar presentations
ManipulatingPictures-Mod6-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology.
Advertisements

Georgia Institute of Technology Movies part 3 Barb Ericson Georgia Institute of Technology April 2006.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops part 1.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops part 1 Barb Ericson Georgia Institute of Technology August 2005.
Conditionals-part11 Barb Ericson Georgia Institute of Technology Nov 2009.
Intro-Sound-part21 Introduction to Processing Digital Sounds part 2 Barb Ericson Georgia Institute of Technology Oct 2009.
NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009.
Georgia Institute of Technology Introduction to Media Computation Barb Ericson Georgia Institute of Technology May 2006.
NestedLoops-part11 Nested Loops – part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
ManipulatingPictures-part11 Manipulating Pictures, Arrays, and Loops part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
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.
ManipulatingPictures-Mod6-part11 Manipulating Pictures, Arrays, and Loops part 1 Barb Ericson Georgia Institute of Technology.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
TOPIC 6 MODIFYING PICTURES USING LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and.
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.
NestedLoops-Mody7-part51 Two-Dimensional Arrays and Nested Loops – part 5 Rotations Barb Ericson Georgia Institute of Technology May 2007.
TOPIC 5 INTRODUCTION TO PICTURES 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
CPSC1301 Computer Science 1 Chapter 4 Manipulating Pictures, Arrays, and Loops part 5.
Georgia Institute of Technology Creating Classes part 4 Barb Ericson Georgia Institute of Technology May 2006.
Georgia Institute of Technology Movies part 4 Barb Ericson Georgia Institute of Technology April 2006.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Georgia Institute of Technology Creating Classes part 2 Barb Ericson Georgia Institute of Technology June 2006.
Georgia Institute of Technology Speed part 4 Barb Ericson Georgia Institute of Technology May 2006.
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.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Intro-Sound-Mod10-part31 Introduction to Processing Digital Sounds part 3 while loop, tracing, for loop, parameters Barb Ericson Georgia Institute of Technology.
ManipulatingPictures-part31 Manipulating Pictures, Arrays, and Loops part 3 Barb Ericson Georgia Institute of Technology Nov 2009.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Barbara Ericson Georgia Tech Sept 2005
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Topic 6 Modifying Pictures Using Loops
Manipulating Pictures, Arrays, and Loops part 3
Barb Ericson Georgia Institute of Technology Dec 2009
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Georgia Institute of Technology
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
Workshop for Programming And Systems Management Teachers
Introduction to Processing Digital Sounds part 2
Manipulating Pictures, Arrays, and Loops part 5
Two-Dimensional Arrays and Nested Loops – part 1
Arrays versus ArrayList
Manipulating Pictures, Arrays, and Loops part 4
Processing Sound Ranges part 1
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops part 3
Introduction to Media Computation
Barb Ericson Georgia Institute of Technology June 2006
Introduction to Processing Digital Sounds part 3
Workshop for Programming And Systems Management Teachers
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops
Georgia Institute of Technology
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops part 6
Barb Ericson Georgia Institute of Technology May 2006
Manipulating Pictures, Arrays, and Loops
CSC1401 Manipulating Pictures 2
Manipulating Pictures, Arrays, and Loops part 6
Presentation transcript:

04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008

04-ManipulatingPictures-part22 Learning Goals Understand at a conceptual and practical level –How to use the picture explorer –Why we need some way to repeat a series of statements –What a for-each loop is

04-ManipulatingPictures-part23 The Picture Explorer Tool that creates a copy of the current picture and lets you explore it –See the color, x, and y values at the cursor To use the tool on a picture object –pictureObj.explore(); Use it to see if the colors have changed

04-ManipulatingPictures-part24 Changing the Red in a Picture One way to change a picture is to reduce the amount of red in it –What if we want to decrease it by half? If we have a value of 200 what should the new value be? How do we reduce any value by half? –What if we want to increase it by 25%? If we have a value of 100 what should the new value be? How do we increase any value by 25%?

04-ManipulatingPictures-part25 Changing all the Pixels in a Picture There are 329 * 150 = 49,350 pixels in the caterpillar picture Do we really want to write the code to change each one of these? –Get the current pixel –Get the red value of the current pixel –Change the red value of the current pixel to half the original value (value / 2) –Put the new red value in the current pixel

04-ManipulatingPictures-part26 We Need a Loop (Iteration) A way to execute a series of statements in the body of the loop –With something changing each time the statements are executed Different pixel to change –And some way to tell when we are done with the repetition Some test to see if the loop should stop –Done with all of the items in an array

04-ManipulatingPictures-part27 Looping Through a List in Alice Open the RockettesFinal Alice world –See how the for all in order loops through the items in a list –Java has a similar loop called a for-each loop

04-ManipulatingPictures-part28 For-each Loop In Java if we want to do something to each item in an array –We can use the for-each loop (new in 1.5) for (Type : variableName arrayName) { // body of the loop } –Which means for each element in the array do the statements in the body of the loop

04-ManipulatingPictures-part29 Method to Decrease Red Loop through all the pixels in an array of Pixel objects and change the red in each public void decreaseRed() { Pixel[] pixelArray = this.getPixels(); int value = 0; // loop through all the pixels in the array for (Pixel pixelObj : pixelArray) { // get the red value value = pixelObj.getRed(); // decrease the red value by 50% (1/2) value = value / 2; // set the red value of the current pixel to the new value pixelObj.setRed(value); } The body of the loop is in {}

04-ManipulatingPictures-part210 Testing decreaseRed Add decreaseRed to Picture.java –Before the last } Compile Picture.java –Click on Compile All Button Type the following in the main method String fileName = FileChooser.pickAFile(); Picture pictObj = new Picture(fileName); pictObj.explore(); pictObj.decreaseRed(); pictObj.explore();

04-ManipulatingPictures-part211 How This Works First we have the method declaration public void decreaseRed() –This is a public method that doesn't return anything and the name is decreaseRed. This method does not take any parameters since there is nothing in the () Next we start the body of the method { –Inside of the body of the method are the statements to be executed when the method is called (executed) Next we declare an array of pixels and get them from the current Picture object Pixel[] pixelArray = this.getPixels(); Next we declare a primitive variable to hold the current red value at the pixel int value = 0;

04-ManipulatingPictures-part212 How This Works - Cont Next start the for-each loop for (Pixel pixelObj : pixelArray) { –Each time through the loop set the variable pixelObj to refer to the next Pixel object in the array of Pixel objects called pixelArray. Execute the statements in the body of the loop. Get the red value from the pixelObj value = pixelObj.getRed(); Decrease the red by 1/2 value = value / 2; Set the red value at the pixelObj to the new value pixelObj.setRed(value); End the for-each loop body and the body of the method }

04-ManipulatingPictures-part213 Exercise Write a method increaseRed() to loop through all the pixels in a picture and double the red values –Multiply by 2 To try this method do the following in the main method: String fileName = FileChooser.pickAFile(); Picture pictObj = new Picture(fileName); pictObj.explore(); pictObj.increaseRed(); pictObj.explore();

04-ManipulatingPictures-part214 Summary You can use the picture explorer to test if your methods worked You can use a for-each loop to execute a series of statements on each item in an array