Loops with counters.

Use this picture ->

Use this information to fill in the code:

  1. Given this array of pixels from the picture. Pixel[] dots = pictureObj.getPixels(); How many pixels are in the array? ______________________________
  2. Given the integer int index = 0; How do you say keep going as long as the index is less than the number of pixels in the array? __________________________
  3. How do you increment the index each time you go through the loop? __________________
  4. How do you get the pixel at the index position in the array of pixels? ________________________
  5. How do you change a pixel, named p, to be all black? _____________________________
  6. How do you make something happen once every 25 times? (Remember using if(index%2==0) makes something happen every other time) _________________________________
  7. What are the values of index % 5 for these values of index:
    0 1 14 17
           
  8. If the variable index counts from 0 to 20, what numbers will print in this code:
    if(index%5 < 2)
    print(index);

    ______________________________________
  9. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
                                                                                                       
    In a picture that is 50 pixels wide, if we want a black bar 4 pixels wide each 25 pixels, how can the calculation be done? _________________________________________
  10. Create the program using the prisoner.jpg picture.
	import java.awt.Color;
	/**
	 * Add 4 pixel wide bars every 25 pixels
	 */
	 public class PictureBars
{ public static void addBars(){ String fileName = "prisoner.jpg"; Picture pictureObj = new Picture(fileName); pictureObj.explore(); // make the picture show System.out.println("this picture is " + pictureObj.getWidth() + " pixels wide."); // get all the dots Pixel[] dots = pictureObj.getPixels(); // loop through and add bars int index=0; while( _________________________________ ){ if( ___________________ ){ //if this is the right place Pixel p = _________________ ; // get the pixel at this index position ________________________ ; // set the color of this pixel to black } _____________________ ; // increment the index } pictureObj.explore(); } }

What went wrong? The same program was run with a slightly different picture.

Test your program with these pictures: winter, and beach
winter


beach