Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 8A Lecture 8 Reading for next class: None Prepare for In-term exam 2 PSA4: Collage and Picture Flip, DON’T WAIT (it’s longer than the previous PSAs)

Similar presentations


Presentation on theme: "CSE 8A Lecture 8 Reading for next class: None Prepare for In-term exam 2 PSA4: Collage and Picture Flip, DON’T WAIT (it’s longer than the previous PSAs)"— Presentation transcript:

1 CSE 8A Lecture 8 Reading for next class: None Prepare for In-term exam 2 PSA4: Collage and Picture Flip, DON’T WAIT (it’s longer than the previous PSAs) How missed classes are calculated… PSA3 Interview, deadline tomorrow! CLICKERS OUT!

2 1. If this is the header to a function defined in Picture.java: public Picture scaleUp(int numTimes) {.... } What should the method call look like if I call it on a Picture object named pictObj and I wanted to use the Picture object that it returns? READING QUIZ – NO TALKING – CORRECTNESS MATTERS A. Picture pictReturned = scaleUp(2); B. Picture pictReturned = pictObj.scaleUp(2); C. String pictReturned = pictObj.scaleUp(2); D. Picture = pictObj.scaleUp(2);

3 2.To scale a picture smaller by 2 (to make a copy of the picture that is half the size of the source) what should go in the blanks public void copySmaller() { Picture pictObj = new Picture(FileChooser.pickAFile()); Pixel sourcePixel = null; Pixel targetPixel = null; //loop through the columns for(int sourceX=0, targetX=0; sourceX < pictObj.getWidth(); ____1_____, targetX++) { //loop through the rows for(int sourceY=0; targetY=0; soruceY < pictObj.getHeight(); ____2____, targetY++) { sourcePixel = pictObj.getPixel(sourceX, sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } } } A. 1. sourceX++ 2. sourceY+=2 B. 1. sourceX+=2 2. sourceY+=2 C. 1. sourceX+=2 2. sourceY++ D. 1. sourceX++ 2. sourceY++ READING QUIZ – NO TALKING – CORRECTNESS MATTERS

4 3. How do I blend two picture objects with 50% of one picture and 50% of another picture? A. By multiplying their colors together and by 0.5 B. By adding them together C. By multiplying their colors by 0.5 and then adding them READING QUIZ – NO TALKING – CORRECTNESS MATTERS

5 4. Imagine that you have a method copyPictureTo, whose header is below, defined in Picture.java. This method copies the source Picture ( sourcePic ) and places its top left corner at ( xStart, yStart ) of the Picture that called the copyPictureTo method. public void copyPictureTo(Picture sourcePic, int xStart, int yStart) Assume you have already created two Picture objects: sourcePic and pictObj, and you make the following call: pictObj.copyPictureTo(sourcePict, pictObj.getWidth()/2, 0); Which position below best represents where the sourcePic will be located in picObj after the call? Assume that sourcePic is much smaller than pictObj. READING QUIZ – NO TALKING – CORRECTNESS MATTERS picObj AB CD Arrows indicate half the target picture width and height

6 Mirroring Even Width versus Odd Width int mirrorPt = getWidth() / 2;... for (int x = 0; x < mirrorPt; x++)

7 Mirroring Odd-width Pictures What happens when this code attempts to mirror a Picture around the vertical axis when the Picture’s width is odd (e.g. 101)? 1)Solo: (30 sec) 2)Discuss: (2min) 3)Group: (30 sec) int mirrorPt = getWidth()/2; Pixel leftP, rightP; for (int y = 0; y < getHeight(); y++) { for (int x = 0; x < mirrorPt; x++) { leftP = getPixel(x,y); rightP = getPixel(getWidth()-1-x,y); rightP.setColor(leftP.getColor()); } A.It will work fine B.It will run, but it won’t mirror correctly C.I won’t run, there will be an index out of bounds exception D.It won’t even compile if getWidth() is odd

8 Mirroring Odd-width Pictures What happens when this code attempts to mirror a Picture around the vertical axis when the Picture’s width is odd (e.g. 101)? 1)Solo: (30 sec) 2)Discuss: (2min) 3)Group: (30 sec) int mirrorPt = getWidth()/2; Pixel leftP, rightP; for (int y = 0; y < getHeight(); y++) { for (int x = 0; x < mirrorPt; x++) { leftP = getPixel(x,y); rightP = getPixel(getWidth()-1-x,y); rightP.setColor(leftP.getColor()); } A.It will work fine B.It will run, but it won’t mirror correctly C.I won’t run, there will be an index out of bounds exception D.It won’t even compile if getWidth() is odd

9 What are the first (x,y) coords for topP and bottomP to mirror around horizontal axis? topP bottomP A. (0,0) (0,3) (0,1) (0,2) (1,0) (1,3) B. (0,0) (0,3) (1,0) (1,3) (2,0) (2,3) 1)Solo: (30 sec) 2)Discuss (1 min) 3)Group C. either A or B will work D. none of the above

10 Challenge: Complete the code that mirrors in the order specified by answer B topP bottomP B. (0,0) (0,3) (1,0) (1,3) (2,0) (2,3) int height = getHeight(); int width = getWidth(); int mid = height/2; Pixel topP, botP; for ( ){ topP = getPixel( ); botP = getPixel( ); botP.setColor(topP.getColor()); }

11 Order of copying pixels When mirroring, we need to copy certain pixels to certain other pixels It doesn’t matter what order we copy in, as long as when we are done, pixels have been copied correctly Two most common orders: Row major order: copy all the pixels in one row, then go on to the next row Column major order: copy all the pixels in one column, then go on to the next column

12 Mirroring around horizontal axis topP botP A. (0,0) (0,3) (0,1) (0,2) (1,0) (1,3) … … int height = getHeight(); int width = getWidth(); int mid = height/2; Pixel topP, botP; for(int y=0; y<mid; y++) { for(int x=0; x<width; x++) { topP = getPixel(x,y); botP = getPixel(x,height-1-y); // copy one to the other… } int height = getHeight(); int width = getWidth(); int mid = height/2; Pixel topP, botP; for(int x=0; x<width; x++) { for(int y=0; y<mid; y++) { topP = getPixel(x,y); botP = getPixel(x,height-1-y); // copy one to the other… } column-major order row-major order topP botP B. (0,0) (0,3) (1,0) (1,3) (2,0) (2,3) … …

13 Challenge: What does this code do? Hint: trace some of the getPixel index values. int magic = getWidth()/2; Pixel foo, bar; for(int y = 0; y < getHeight(); y++) { int countingDown = getWidth()-1; for(int x = 0; x < magic; x++) { foo = getPixel(x,y); bar = getPixel(countingDown,y); bar.setColor(foo.getColor()); countingDown--; } A.Copies top half into bottom half not mirrored. B.Copies left half into right half not mirrored. C.Mirrors around vertical axis, left into right D.Mirrors around horizontal axis, top into bottom E.Some other bizarre transformation 1)Solo: (1 min) 2)Discuss: (2 min) 3)Group: (30 sec)

14 Challenge: What does this code do? Hint: trace some of the getPixel index values. int magic = getWidth()/2; Pixel foo, bar; for(int y = 0; y < getHeight(); y++) { int countingDown = getWidth()-1; for(int x = 0; x < magic; x++) { foo = getPixel(x,y); bar = getPixel(countingDown,y); bar.setColor(foo.getColor()); countingDown--; } 1)Solo: (1 min) 2)Discuss: (2 min) 3)Group: (30 sec) x y countingDown foo bar

15 By what variable name do we refer to collage inside makeC in Picture.java? A.collage B.callingObject C.Object D.Picture E.this public class Lab4 { public static void main(String[] args) { Picture collage = new Picture("blank.jpg"); Picture p = new Picture("bird1.jpg"); Picture q = new Picture("bird2.jpg"); collage.makeC(p,q); } public class Picture { public void makeC(Picture p1, Picture p2) { Pixel[] targetPixels = _______.getPixels(); // … more code }

16 Match the scenario to the constructor call (we’ll vote for each scenario) Scenario 1)Create a picture from a specific file 2)Create a picture that is a copy of another picture 3)Create a picture of a given width and height 4)Create a picture of the same width and height as another picture Call A.Picture p = new Picture(); B.Picture p = new Picture("filename.jpg"); C.Picture p = new Picture(other); D.Picture p = new Picture(aNum,bNum);

17 What does this code do? Makes red box of width height 1)Solo: (1 min) 2)Discuss: (2 min) 3)Group: (30 sec) Pixel foo; for(int y = 40; y < 50; y++) { for(int x = 1 ; x < 5; x++) { foo = getPixel(x,y); foo.setColor(Color.RED); }

18 What does this code do? Makes red box of width height 1)Solo: (1 min) 2)Discuss: (2 min) 3)Group: (30 sec) Pixel foo; for(int y = 40; y < 50; y++) { for(int x = 1 ; x <= 5; x++) { foo = getPixel(x,y); foo.setColor(Color.RED); }

19 What are correct loops to make a black box of width x and height y? public void foo(int x, int y) { Pixel foo; >>> { foo = getPixel(w,h); foo.setColor(Color.BLACK); } 1)Solo: (1 min) 2)Discuss: (2 min) 3)Group: (30 sec) A) for (int w = 0; w <= x; w++) for (int h = 0; h <= y; h++) B) for (int w = 10; w < x+10; w++) for (int h = 20; h < y+20; h++) C) for (int w = 0; w < y; w++) for (int h = 0; h < x; h++) D) for(int w = 10; w <= x+10; w++) for(int h = 20; h <= y+20; h++)

20 From the book: Cropping A Picture (page 147-148) – we’ll change a bit Example of: –Working with both the calling object and a parameter object in a method Extra information is passed to methods through parameters. The calling object is something like an extra parameter, named this –Doing something to a subset of the possible pixels in a picture

21 What part of Katie are we copying? (slight mod from the book) public void copyKatiesXXX(Picture sourcePic) { Pixel sPixel = null, tPixel = null; for (int sX = 40, tX = 100; sX < 110; sX++, tX++) { for (int sY = 350, tY = 100; sY < 400; sy++, tY++) { sPixel = sourcePic.getPixel(sX,sY); tPixel = this.getPixel(tX,tY); tPixel.setColor(sPixel.getColor(); } 1)Solo: (1 min) 2)Discuss: (2 min) 3)Group: (30 sec) A.Feet B.Part of dress C.Hands D.Part of Couch E.Face

22 Parameters: getting information in to methods It’s nice to have code that is “user controllable”… We have been hard-coding constants (40, 3, 100, for example) a lot, but we can write more flexible code using PARAMETERS This lets us write code to do things like “cropping and pasting into a blank canvas”, but letting the user specify what part of the source picture to crop, and where to place it in the canvas.

23 Underline the values you would change into parameters and write a new method header public void copyKatiesXXX( ) { Pixel sPixel, tPixel = null; for (int sX = 40, tX = 100; sX < 110; sX++, tX++) { for (int sY = 350, tY = 100; sY < 400; sy++, tY++) { sPixel = sourcePic.getPixel(sX,sY); tPixel = this.getPixel(tX,tY); tPixel.setColor(sPixel.getColor(); }

24 TODO Study for In-term Exam 2 Start PSA4: Collage and Picture Flip Don’t forget your PSA3 interview!


Download ppt "CSE 8A Lecture 8 Reading for next class: None Prepare for In-term exam 2 PSA4: Collage and Picture Flip, DON’T WAIT (it’s longer than the previous PSAs)"

Similar presentations


Ads by Google