1 / 14

Manipulating Pictures, Arrays, and Loops Stepping through programs

Manipulating Pictures, Arrays, and Loops Stepping through programs. Barb Ericson Georgia Institute of Technology May 2007. Learning Goals. How to trace code How to fake a sunset. Tracing Code. An important skill to develop is the ability to trace code

season
Download Presentation

Manipulating Pictures, Arrays, and Loops Stepping through programs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Manipulating Pictures, Arrays, and LoopsStepping through programs Barb Ericson Georgia Institute of Technology May 2007 ManipulatingPictures-Mod6-part4

  2. Learning Goals • How to trace code • How to fake a sunset ManipulatingPictures-Mod6-part4

  3. Tracing Code • An important skill to develop is the ability to trace code • Also called walking through or stepping through your code • Look at each line and predict what will happen • Show the variables and their values ManipulatingPictures-Mod6-part4

  4. Step Through decreaseRed() using a while loop • A picture object was created from your picture file and then was sent the message decreaseRed() • The picture object can be referred to by this because the decreaseRed () method is inside the Picture class • The array of pixel objects was returned from sending getPixels() to the picture object Pixel[] pixelArray = this.getPixels(); • Some variables were declared for later use in the loop Pixel pixelObj = null; int value = 0; // needed if we use a while loop instead of a for:each loop int index = 0; ManipulatingPictures-Mod6-part4

  5. The loop in increaseRed() that allows you to choose where to start or stop public void increaseRed() { Pixel[] pixelArray = this.getPixels(); Pixel pixelObj = null; int value = 0; int index = 0;// set the starting position in the array while (index < pixelArray.length) // loop through all the pixels { pixelObj = pixelArray[index]; // get the current pixel value = pixelObj.getRed(); // get the value value = (int) (value * 1.3); // increase value by 30% pixelObj.setRed(value); // set the red value index++;// increment the index } } ManipulatingPictures-Mod6-part4

  6. Step Through decreaseRed() - cont • The while loop tests if the index is less than the length of the array while (index < pixelArray.length) { • And if so it executes the statements in the body of the loop {} • It sets the variable pixelObj to the pixel at the current index in the array of pixels pixelObj = pixelArray[index]; • It gets the red value of that pixel value = pixelObj.getRed(); • it sets the value to the integer part of (red value/ 2) value = value / 2; • It sets the pixel’s red to the new value pixelObj.setRed(value); • It increments the index value index++; ManipulatingPictures-Mod6-part4

  7. R=252, G=254, B=251, X=0, Y=0 R=253, G=255, B=254, X=1, Y=0 R=254, G=254, B=254, X=2, Y=0 Memory Map of decreaseRed() width=329 height=150 • What does memory look like the first time through the loop? • How about the 2nd time through? • How about the 3rd time through? • How about the last time through? Picture: Class getPixels() … this pixels … pixel Pixel: Class getRed() setRed()… value = 252 index = 0 ManipulatingPictures-Mod6-part4

  8. To debug in BlueJ • 1stcompile your code • Click in the left margin to turn on the debugger at that point • When you run the program it will halt and allow you to single step through ManipulatingPictures-Mod6-part4

  9. Press Step to look through the code as it runs • The variables will appear in the debugger window and change as you step through the code ManipulatingPictures-Mod6-part4

  10. 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% Only the top half of the picture is the sky ManipulatingPictures-Mod6-part4

  11. 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 / 2 to get the top half of the picture • 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 ManipulatingPictures-Mod6-part4

  12. Faking a Sunset Method ManipulatingPictures-Mod6-part4

  13. Testing makeSunset Picture pictureObj = new Picture(“beach.jpg”); pictureObj.explore(); pictureObj.makeSunset(); pictureObj.explore(); Challenge: Create a sunset method that will do this sunset effect on the top half of the picture and darken the bottom half of the picture. ManipulatingPictures-Mod6-part4

  14. Summary • Tracing code is walking through code and saying what each line is doing • It can also help to show the values of the variables after each line executes • You can increase the red in a picture by multiplying the red value by a number > 1.0 • You can change more than one color in a pixel ManipulatingPictures-Mod6-part4

More Related