====================== pixel operations that depend on location: nested loops // Look at Delbert T. Quimby. > Picture quimby; > quimby = new Picture( "/Users/wallingf/Desktop/delbert-t-quimby.jpg" ); > quimby.show(); // I'd like to doctor the image so that it has a bookcase // across the entire background. // First, explore with PictureExplorer... > quimby.explore(); > int width = quimby.getWidth(); > width 400 // First, explore with PictureExplorer... // Find (393, 184) as bottom of bookcase on right. // Decide to copy right half of image, down to y = 184, // into left half, column by column. // First, try left into right: for (int y = 0; y < 184; y++) { Pixel source = quimby.getPixel(0, y); Pixel target = quimby.getPixel(width-1, y); target.setColor( source.getColor() ); } // ... can't see. Try somewhere in middle: for (int y = 0; y < 184; y++) { Pixel source = quimby.getPixel(100, y); Pixel target = quimby.getPixel(width-101, y); target.setColor( source.getColor() ); } // ... and find line! // Do that for all x's, from border to the middle: for (int x = 0; x < width; x++) // all x's, from border to the middle for (int y = 0; y < 184; y++) { Pixel source = quimby.getPixel(x, y); Pixel target = quimby.getPixel(width-1-x, y); target.setColor( source.getColor() ); } // [1] MOVE THE CODE INTO A METHOD // This method attempts to mirror the bookcase in Delbert // T. Quimby's study. VERSION 1 public void mirror() { int width = this.getWidth(); for (int x = 0; x < width; x++) for (int y = 0; y < 184; y++) { Pixel source = this.getPixel(x, y); Pixel target = this.getPixel(width-1-x, y); target.setColor( source.getColor() ); } } // [2] and 'reverse' the source and target pixels public void mirror() { int width = this.getWidth(); for (int x = 0; x < width; x++) for (int y = 0; y < 184; y++) { Pixel source = this.getPixel(width-1-x, y); Pixel target = this.getPixel(x, y); target.setColor( source.getColor() ); } } > quimby.write( "/Users/wallingf/Desktop/quimby-mirrored.jpg" ); // ... WHAT IS LEFT TO DO? // - "uncopy" Delbert -- maybe by copying smaller regions // - re-insert the caption -- find with explorer // DON'T COPY Delbert ------------------------------------------- > quimby = new Picture( "/Users/wallingf/Desktop/delbert-t-quimby.jpg" ); > quimby.show(); > quimby.explore(); // ... find point at top corner of CHAIR // x == 300, so copy only to their in first loop for (int x = 0; x < 100; x++) // ... then (300,125) back to x == 275 for (int x = 100; x < 125; x++) for (int y = 0; y < 125; y++) // ... find point at top corner of HEAD (275,96) back to x == 240 for (int x = 125; x < 160; x++) for (int y = 0; y < 96; y++) // ... find point at top corner of LAMP (240,82) back to x == 200 (middle) for (int x = 160; x < 200; x++) for (int y = 0; y < 96; y++) // ALL THOSE LOOPS ARE THE SAME! HOW DO WE MAKE THE DUPLICATION GO AWAY? // AND MAKE IT EASIER TO COPY MORE REGIONS? **** a method... > Picture quimby=new Picture("/Users/wallingf/Desktop/delbert-t-quimby.jpg"); > quimby.mirror(); > quimby.explore(); // RE-INSERT caption ------------------------------------------------- // -- find with explorer Picture quimby = new Picture("/Users/wallingf/Desktop/delbert-t-quimby.jpg"); quimby.explore(); farthest x == 135 leftmost x == 20 highest y == 170 lowest y == 85 // -- make a new picture of the caption > Picture caption = new Picture( 115, 85 ); Pixel sourcePixel, targetPixel; int xOffset = 20; int yOffset = 85; for (int x = 0; x < caption.getWidth(); x++) for (int y = 0; y < caption.getHeight(); y++) { sourcePixel = quimby.getPixel( x + xOffset, y + yOffset ); targetPixel = caption.getPixel( x, y ); targetPixel.setColor( sourcePixel.getColor() ); } caption.show(); caption.write( "/Users/wallingf/Desktop/quimby-caption.jpg" ); quimby.mirror(); Pixel sourcePixel, targetPixel; int xOffset = 20; int yOffset = 85; for (int x = 0; x < caption.getWidth(); x++) for (int y = 0; y < caption.getHeight(); y++) { sourcePixel = caption.getPixel( x, y ); // targetPixel = quimby.getPixel( x + xOffset, y + yOffset ); // targetPixel.setColor( sourcePixel.getColor() ); } ============================== EXERCISE: write a loop over an array of doubles double[] mileageForWeek = {3, 8, 10, 7, 10, 0, 20}; double sum; for (int i = 0; i < mileageForWeek.length-1; i++) { sum = mileageForWeek[i] + mileageForWeek[i+1]; System.out.println( sum / 2 ); } OR TO "BLUR" THE DATA: for (int i = 0; i < mileageForWeek.length-1; i++) { sum = mileageForWeek[i] + mileageForWeek[i+1]; mileageForWeek[i] = sum / 2; } // BONUS QUESTION: Why might we want to do something like this to an image? ================ Demo some fun drawBorder() methods from Homework 1