Manipulating Pictures, Arrays, and Loops part 5

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.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
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.
ManipulatingPictures-part11 Manipulating Pictures, Arrays, and Loops part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
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.
CPSC1301 Computer Science 1 Chapter 8 Introduction to Processing Digital Sounds part 3.
CPSC1301 Computer Science 1 Chapter 4 Manipulating Pictures, Arrays, and Loops part 5.
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 Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
CMSC 150 LOOPS CS 150: Fri 20 Jan Representing DNA AGTCCAGTGTCAA.
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.
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Barbara Ericson Georgia Tech Sept 2005
Manipulating Pictures, Arrays, and Loops part 1
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
Arrays, For loop While loop Do while loop
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
Outline Altering flow of control Boolean expressions
Introduction to Processing Digital Sounds part 2
Two-Dimensional Arrays and Nested Loops – part 1
Georgia Institute of Technology
CSE 8A Lecture 6 Reading for next class:
Manipulating Pictures, Arrays, and Loops part 4
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops part 3
Barb Ericson Georgia Institute of Technology June 2006
Manipulating Pictures, Arrays, and Loops part 1
Introduction to Processing Digital Sounds part 3
Two-Dimensional Arrays and Nested Loops – part 2
Workshop for Programming And Systems Management Teachers
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops
Building Java Programs
Georgia Institute of Technology
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops part 6
Creative Commons Attribution Non-Commercial Share Alike License
Building Java Programs
Barb Ericson Georgia Institute of Technology May 2006
Manipulating Pictures, Arrays, and Loops
The for-statement.
Building Java Programs
Barb Ericson Georgia Institute of Technology Oct 2005
CSC1401 Manipulating Pictures 2
Barb Ericson Georgia Institute of Technology April 2006
Manipulating Pictures, Arrays, and Loops part 6
Presentation transcript:

Manipulating Pictures, Arrays, and Loops part 5 Georgia Institute of Technology

Georgia Institute of Technology Learning Goals Understand at a conceptual and practical level How to change more than one color in a method How to use a for loop How to print out values in a loop How to convert a while loop into a for loop Georgia Institute of Technology

Georgia Institute of Technology Faking a Sunset If you want to make an outdoor scene look like it happened during sunset You might want to increase the red But you can’t increase past 255 Another idea is to reduce the blue and green To emphasize the red Try to reduce the blue and green by 30% Georgia Institute of Technology

Faking a Sunset Algorithm Reduce the blue and green by 30% Get the array of pixels from the picture Set up an index to start at 0 Loop while the index is less than the length of the array Get the pixel at the current index from the array of pixels Set the blue value at the pixel to 0.7 times the original value Set the green value at the pixel to 0.7 times the original value Increment the index and go back to step 3 Georgia Institute of Technology

Georgia Institute of Technology Faking a Sunset Method /** * Method to simulate a sunset by * decreasing the green * and blue */ public void makeSunset() { Pixel[] pixelArray = this.getPixels(); Pixel pixelObj = null; int value = 0; int i = 0; // loop through all the pixels while (i < pixelArray.length) { // get the current pixel pixelObj = pixelArray[i]; // change the blue value value = pixelObj.getBlue(); pixelObj.setBlue((int) (value * 0.7)); // change the green value value = pixelObj.getGreen(); pixelObj.setGreen((int) (value * 0.7)); // increment the index i++; } Programmers often use a variable named i for index. Georgia Institute of Technology

Georgia Institute of Technology Testing makeSunset String file = “c:/intro-prog-java/mediasources/beach.jpg”; Picture pictureObj = new Picture(file); pictureObj.explore(); pictureObj.makeSunset(); Georgia Institute of Technology

Georgia Institute of Technology For Loops Programmers like shortcuts Especially those that reduce errors And mean less typing We have been using a while loop with an index We had to declare the index variable and initialize it before the loop If you forget this there will be a compiler error We had to increment the index in the loop If you forget this it will be an infinite loop The shortcut for this is a For Loop Georgia Institute of Technology

Georgia Institute of Technology For Loop Syntax for (initialization area; continuation test; change area) Initialization area Declare variables and initialize them Continuation test If true do body of loop If false jump to next statement after the loop Change area Change the loop variables Increment or decrement them Each of the three parts of a for loop are optional. However, the ‘;’s have to be there. You can declare and initialize more than one variable in the initialization area, just separate them with commas. Georgia Institute of Technology

Comparison of While and For Loops int index = 0; while (index < pixelArray.length) { statements . index++; } for (int i=0; i < pixelArray.length; i++) { statements . } Programmers often use i as an index variable name. Even though the variable i is declared and initialized in the for loop it actually only is declared before the test. It is not repeated each time through the loop. Even though the increment is right after the continuation test in the for loop it doesn’t happen till after the last statement in the loop. Georgia Institute of Technology

Using System.out.println() in a Loop One way to check what is happening in your program is to add System.out.println(expression); You might add this to the loop to check the value of ‘i’ during it. And to verify that the increment happens after the last statement in the loop Georgia Institute of Technology

Change to For Loop Exercise Edit makeSunset() and change it from using a while loop to using a for loop Move the declaration of the index to the for loop initialization area Move the index increment to the for loop change area Execute the code to make sure it still works Georgia Institute of Technology

Georgia Institute of Technology Summary You can change more than one color in a method Like in make sunset You can use a for loop to repeat a series of Java statements until a test is false for (initialization area; continuation test; change area) Executes the same way a while loop does But less error prone when executing a loop at set number of times You can convert code that uses a while loop into using a for loop You can print values in a loop using System.out.println(expression); Georgia Institute of Technology