1 / 84

Modifying Pixels in a Matrix

Learning Goals. Understand at a conceptual and practical levelNested loops and using nested loops to process data in a matrix (or two dimensional array)More advanced topics with methodsMore advanced ways of manipulating pictures in Java programs. More Advanced Working with Pictures. We can only g

artaxiad
Download Presentation

Modifying Pixels in a Matrix

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. Modifying Pixels in a Matrix

    2. Learning Goals Understand at a conceptual and practical level Nested loops and using nested loops to process data in a matrix (or two dimensional array) More advanced topics with methods More advanced ways of manipulating pictures in Java programs

    3. More Advanced Working with Pictures We can only go so far in processing pictures without knowing where certain pixels are in an image For example Cropping a picture, copying parts of a picture, performing reflections and rotations, and so on Remember that a picture can be thought of as a two dimensional array or a matrix Pixels are organized into rows and columns, with each pixel having an (x,y) position in the picture

    4. Using Two Loops for Processing Pictures We cannot easily use a single loop to keep track of the x and y position values for a pixel Instead, we need to use two loops One to move horizontally along rows Another to move vertically along columns The method getPixels() did this inside itself to make it easier for us to write simple methods for manipulating pictures If we need to access pixels individually by position, however, we must do this ourselves

    5. Nested Loops How would you get all the pixels in a picture using their x and y values From left to right and top to bottom? x=0 and y=0, x=1 and y=0, x=2 and y=0, … x=0 and y=1, x=1 and y=1, x=2 and y=1, … x=0 and y=2, x=1 and y=2, x=2 and y=2, … We need to have one loop inside another The outer loop counts y from 0 to height - 1 The inner loop counts x from 0 to width – 1 We would say that the inner loop is nested inside the outer loop

    6. Nested Loop Template // Loop through the rows for (int y = 0; y < this.getHeight(); y++) { // Loop through the columns for (int x = 0; x < this.getWidth(); x++) { // Get the current pixel pixelObj = this.getPixel(x,y); // Do something to the color // Set the new color pixelObj.setColor(colorObj); } }

    7. Alternative Nested Loop How would you get all the pixels in a picture using their x and y values This time, from top to bottom and left to right? x=0 and y=0, x=0 and y=1, x=0 and y=2, … x=1 and y=0, x=1 and y=1, x=1 and y=2, … x=2 and y=0, x=2 and y=1, x=2 and y=2, … We would again need to have one loop inside another The outer loop counts x to width - 1 The inner loop counts y from 0 to height - 1

    8. Alternative Nested Loop Template // Loop through the columns (x direction) for (int x = 0; x < this.getWidth(); x++) { // Loop through the rows (y direction) for (int y = 0; y < this.getHeight(); y++) { // Get the current pixel pixelObj = this.getPixel(x,y); // Do something to the color // Set the new color pixelObj.setColor(colorObj); } }

    9. Lighten the Color Algorithm Earlier, we saw how to lighten an image by accessing pixels through getPixels() This time, we use a nested array … Start x at 0 and loop while x < the picture width (add 1 to x at the end of each loop) Start y at 0 and loop while y < the picture height (add 1 to y at the end of each loop) Get the pixel at this location Get the color at the pixel Lighten (brighten) the color Set the color for the pixel to the lighter color

    10. Lighten the Color with a Nested Loop public void lighten2() { Pixel pixelObj = null; Color colorObj = null; // loop through the columns (x direction) for (int x = 0; x < this.getWidth(); x++) { // loop through the rows (y direction) for (int y = 0; y < this.getHeight(); y++) {

    11. Lighten - Continued // get pixel at the x and y location pixelObj = this.getPixel(x,y); // get the current color colorObj = pixelObj.getColor(); // get a lighter color colorObj = colorObj.brighter(); // set the pixel color to the lighter color pixelObj.setColor(colorObj); } } }

    12. Trying the Lighten Method In the interactions pane: String file = FileChooser.pickAFile(); Picture p1 = new Picture(file); p1.explore(); p1.lighten2(); p1.explore();

    13. Changing to Nested Loop Exercise Change the method clearBlue() to use a nested for loop to loop through all the pixels Run the method again to check that it still works Check that the blue values are all 0 using pictureObj.explore()

    14. Vertical Mirroring What if we want to pretend to place a mirror in the middle of the picture We would see the left side of the picture mirrored on the right side

    15. Mirror Vertical Algorithm Loop through all the rows (y starts at 0, increments by 1, and is less than the picture height) Loop with x starting at 0 and x less than the midpoint (mirror point) value Get the left pixel at x and y Get the right pixel at width – 1 - x Set the color for the right pixel to be the color of the left pixel

    16. Mirror Vertical Algorithm to Code We are going to need the midpoint int midpoint = this.getWidth() / 2; Loop through the rows (y values) for (int y = 0; y < this.getHeight(); y++) { Loop through x values (starting at 1) for (int x = 0; x < midpoint; x++) { Set right pixel color to left pixel color Pixel leftPixel = this.getPixel(x, y); Pixel rightPixel = this.getPixel(this.getWidth() - 1 - x, y); rightPixel.setColor(leftPixel.getColor()); We don’t need to calculate the midpoint every time through the loop so calculate it once before the loop.We don’t need to calculate the midpoint every time through the loop so calculate it once before the loop.

    17. Mirror Vertical Method public void mirrorVertical() { int mirrorPoint = this.getWidth() / 2; Pixel leftPixel = null; Pixel rightPixel = null; // loop through the rows for (int y = 0; y < this.getHeight(); y++) { // loop from 0 to just before the mirror point for (int x = 0; x < mirrorPoint; x++) {

    18. Mirror Vertical Method - Continued leftPixel = this.getPixel(x, y); rightPixel = this.getPixel(this.getWidth() – 1 – x, y); rightPixel.setColor(leftPixel.getColor()); } } }

    19. Using FileChooser.getMediaPath(fName) Gets the full path name for the file with the passed short name redMotorcycle.jpg Defaults to using the directory c:\intro-prog-java\mediasources\ Set it to a directory using FileChooser.setMediaPath("c:/book/media/"); This will write out the directory name to a file and remember it till you change it

    20. Trying Mirror Vertical Create the picture Picture p1 = new Picture( FileChooser.getMediaPath("caterpillar.jpg"); Invoke the method on the picture p1.mirrorVertical(); Show the picture p1.show();

    21. Mirror Horizontal What about mirroring around a mirror held horizontally in the vertical center of the picture? Like a reflection in a lake?

    22. Mirror Horizontal Algorithm Get the vertical midpoint Picture height / 2 Loop through all the x values Loop from y=0 to y < vertical midpoint Get the top pixel At x and y Get the bottom pixel Height - 1 - y Set the bottom pixel’s color to the top pixel color

    23. Mirror Horizontal Exercise Write the method to mirror the top half of the picture to the bottom half. This is a motorcycle redMotorcycle.jpg How about mirroring bottom to top?

    24. Copying and Transforming Pictures We can copy pictures by taking pixels from a source image and setting pixels in a target image Actually, we don’t copy the pixels, but rather simply make the pixels in the target image the same color as the source If we do things carefully, not only can we copy an image, but we can also transform it in the process Cropping it, scaling it, rotating it, and so on

    25. Copying Pixels to a New Picture We need to track the source picture x and y And the target picture x and y We can use a blank picture as the target picture Several blank pictures are available 640x480.jpg 7inX95in.jpg

    26. Copy Picture Example We will start by copying a particular picture to the 7 by 9.5 inch blank picture Create the target picture object Invoke the new copy method we will create on the target picture Inside this method, we create the source picture object and loop through the source picture pixels Get the source and target pixels Set the color of the target pixel to the color of the source pixel

    27. Copy Algorithm to Code Loop through the source pixels // loop through the columns for (int sourceX = 0, targetX = 0; sourceX < sourcePicture.getWidth(); sourceX++, targetX++) { // loop through the rows for (int sourceY = 0, targetY = 0; sourceY < sourcePicture.getHeight(); sourceY++, targetY++) { Notice we did multiple tasks in the for loops!

    28. Copy Algorithm to Code – Cont Get the source and target pixels sourcePixel = sourcePicture.getPixel(sourceX,sourceY); targetPixel = this.getPixel(targetX,targetY); Set the color of the target pixel to the color of the source pixel targetPixel.setColor(sourcePixel.getColor());

    29. Copy Method to Copy KatieFancy.jpg public void copyKatie() { String sourceFile = FileChooser.getMediaPath("KatieFancy.jpg"); Picture sourcePicture = new Picture(sourceFile); Pixel sourcePixel = null; Pixel targetPixel = null; // loop through the columns for (int sourceX = 0, targetX = 0; sourceX < sourcePicture.getWidth(); sourceX++, targetX++) {

    30. Copy Method - Continued // loop through the rows for (int sourceY = 0, targetY = 0; sourceY < sourcePicture.getHeight(); sourceY++, targetY++) { // set the target pixel color to the source pixel color sourcePixel = sourcePicture.getPixel(sourceX,sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } } }

    31. Trying the copyKatie Method Create a picture object using the 7inX95in.jpg file in the mediasources directory Picture p1 = new Picture(FileChooser.getMediaPath("7inX95in.jpg")); Show the picture p1.show(); Invoke the method on this picture object p1.copyKatie(); Repaint the picture p1.repaint();

    32. Result of copyKatie Method

    33. Copy to an Upper Left Location How would you copy a picture to a location in another picture (like 100, 100)? Specified as the upper left corner You still copy all the source pixels But the target x and y start at the specified location

    34. Copy to Position Exercise Write a method copyRobot to copy robot.jpg To location 100, 100 in 7inx95in.jpg Test with String file = FileChooser.getMediaPath("7inx95in.jpg"); Picture p = new Picture(file); p.copyRobot(); p.show();

    35. Copy Method to Copy robot.jpg public void copyRobot() { String sourceFile = FileChooser.getMediaPath("robot.jpg"); Picture sourcePicture = new Picture(sourceFile); Pixel sourcePixel = null; Pixel targetPixel = null; // loop through the columns for (int sourceX = 0, targetX = 100; sourceX < sourcePicture.getWidth(); sourceX++, targetX++) {

    36. Copy Method - Continued // loop through the rows for (int sourceY = 0, targetY = 100; sourceY < sourcePicture.getHeight(); sourceY++, targetY++) { // set the target pixel color to the source pixel color sourcePixel = sourcePicture.getPixel(sourceX,sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } } }

    37. Cropping We can copy just part of a picture to a new picture instead of the entire picture Just change the start and end source x and y values to the desired values How do you find the right values? Well, you can use pictureObj.explore() to find the x and y values inside the picture referred to by the pictureObj reference variable What are the x and y values to get the face of the girl in KatieFancy.jpg?

    38. Copy Cropped Face Method public void copyKatiesFace() { String sourceFile = FileChooser.getMediaPath("KatieFancy.jpg"); Picture sourcePicture = new Picture(sourceFile); Pixel sourcePixel = null; Pixel targetPixel = null; // loop through the columns for (int sourceX = 70, targetX = 100; sourceX < 135; sourceX++, targetX++) { // loop through the rows for (int sourceY = 3, targetY = 100; sourceY < 80; sourceY++, targetY++) {

    39. Copy Cropped Face Method - Continued // set the target pixel color to the source pixel color sourcePixel = sourcePicture.getPixel(sourceX,sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } } }

    40. Testing the copyKatiesFace Method Create a picture object Picture p1 = new Picture(FileChooser.getMediaPath( "7inX95in.jpg")); Show the picture p1.show(); Invoke the method p1.copyKatiesFace(); Repaint the picture p1.repaint();

    41. What Makes a Good Method? A method should do one and only one thing Accomplish some task The name should tell you what it does A method can call other methods to do some of the work Procedural decomposition We shouldn’t copy code between methods We should make general methods that are reusable A method should ideally be in the class that has the data the method is working on

    42. Were the Last Few Methods General? We specified the file to copy from inside the method Meaning we would need to change the method or make another method to copy a different picture We also specified the portion of the images to copy inside the method Again, we would have to change the method or introduced a new one to have things work differently So, no, they were not general!

    43. General Copy Method First, let’s create a general copy method that copies pixels from a passed source picture For now, we will do this without worrying about the portion of the image to copy This will be very similar to our earlier copy methods, except now we are passing in a picture instead of creating one inside our method This makes it more general, because it can be used to copy from arbitrary sources

    44. General Copy Method public void copy (Picture sourcePicture) { Pixel sourcePixel = null; Pixel targetPixel = null; // loop through the columns for (int sourceX = 0, targetX = 0; sourceX < sourcePicture.getWidth(); sourceX++, targetX++) {

    45. General Copy Method - Continued // loop through the rows for (int sourceY = 0, targetY = 0; sourceY < sourcePicture.getHeight(); sourceY++, targetY++) { // set the target pixel color to the source pixel color sourcePixel = sourcePicture.getPixel(sourceX,sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } } }

    46. More General Copy Algorithm Now, we create a more general method that copies pixels from a passed source picture Giving a start x and y and end x and y for the source picture If the start x and y and end x and y cover the entire picture then the whole picture will be copied If the start x and y and end x and y are part of the picture then cropping will occur This is copied to the picture object the method is invoked on, with a target start x and target start y If the start x and y are 0 then it copies to the upper left corner

    47. More General Copy Algorithm Loop through the x values between xStart and xEnd Loop through the y values between yStart and yEnd Get the pixel from the source picture for the current x and y values Get the pixel from the target picture for the targetStartX + x and targetStartY + y values Set the color in the target pixel to the color in the source pixel

    48. More General Copy Method public void copy(Picture sourcePicture, int startX, int startY, int endX, int endY, int targetStartX, int targetStartY) { Pixel sourcePixel = null; Pixel targetPixel = null; // loop through the x values for (int x = startX, tx = targetStartX; x < endX && x < sourcePicture.getWidth() && tx < this.getWidth(); x++, tx++) { // loop through the y values for (int y = startY, ty = targetStartY; y < endY && y < sourcePicture.getHeight() && ty < this.getHeight(); y++, ty++) {

    49. More General Copy Method - Continued // copy the source color to the target color sourcePixel = sourcePicture.getPixel(x,y); targetPixel = this.getPixel(tx,ty); targetPixel.setColor(sourcePixel.getColor()); } } }

    50. Rewrite Methods Exercise Type the copy methods into Picture.java Rewrite copyKatie() and copyKatiesFace() methods to use one of the new copy methods Run the methods to make sure they still work

    51. Method Overloading Notice that you had two methods named copy() in Picture.java and Java did not complain about this. Java allows you to have methods with the same name as long as they take different parameters Either a different number of parameters or different types of parameters This is called overloading in Java

    52. Method Overloading It is important to note that the return type of a method does not count towards being able to overload a method You cannot have two methods with the same names and parameters, with the only difference being the return type

    53. Left Rotation To rotate an image left 90 degrees we can still copy all the pixels But they go to different locations in the target Column values become row values target x = source y target y = source width -1 – source x

    54. Left Rotation Algorithm Create the target picture object Invoke the method on the target picture Create the source picture object Loop through the source x Loop through the source y Get the source pixel at the x and y values Get the target pixel with the x equal the source y value and the y equal the source picture width – 1 minus the source x Copy the color from the source pixel to the target pixel

    55. Left Rotation Method public void copyKatieLeftRotation() { String sourceFile = FileChooser.getMediaPath("KatieFancy.jpg"); Picture sourcePicture = new Picture(sourceFile); Pixel sourcePixel = null; Pixel targetPixel = null; int targetX, targetY = 0; // loop through the columns for (int sourceX = 0; sourceX < sourcePicture.getWidth(); sourceX++) {

    56. Copy Katie Left Rotation // loop through the rows for (int sourceY = 0; sourceY < sourcePicture.getHeight(); sourceY++) { // set the target pixel color to the source pixel color sourcePixel = sourcePicture.getPixel(sourceX,sourceY); targetX = sourceY; targetY = sourcePicture.getWidth() – 1 – sourceX; targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } } }

    57. Testing Left Rotation String file = FileChooser.getMediaPath( "7inX95in.jpg"); Picture p = new Picture(file); p.show(); p.copyKatieLeftRotation(); p.repaint();

    58. Right Rotation To rotate an image right 90 degrees still copy all the pixels But, like left rotation, they go to different locations in the target Column values become row values target y = source x target x = source height – 1 – source y

    59. Right Rotation Exercise Write the method to rotate the picture of Katie to the right instead of to the left Try out the method String file = FileChooser.getMediaPath( "7inX95in.jpg"); Picture p = new Picture(file); p.show(); p.copyKatieRIghtRotation(); p.repaint(); Can you make the method more general? To work on any picture?

    60. Scaling You can make a picture smaller Faster to download on the web Increment the source x and y by a number larger than 1 Don’t use all the source pixels in target You can also make a picture larger Show more detail Copy the same source x and y to more than one target x and y Use source pixels more than once in target

    61. Scaling Down a Picture passionFlower.jpg is 640 pixels wide and 480 pixels high If we copy every other pixel we will have a new picture with width (640 / 2 = 320) and height (480 / 2 = 240)

    62. Scaling Down Algorithm Create the target picture Invoke the method on the target picture Create the source picture Loop with source x starting at 0 and target x starting at 0 as long as < source width Increment the source x by 2 each time through the loop, increment the target x by 1 Loop with source y starting at 0 and target y starting at 0 as long as < source height Increment the source y by 2 each time through the loop, increment the target y by 1 Copy the color from the source to target pixel

    63. Scaling Down Method public void copyFlowerSmaller() { Picture flowerPicture = new Picture( FileChooser.getMediaPath("passionFlower.jpg")); Pixel sourcePixel = null; Pixel targetPixel = null; // loop through the columns for (int sourceX = 0, targetX=0; sourceX < flowerPicture.getWidth(); sourceX+=2, targetX++) {

    64. Scaling Down Method - Continued // loop through the rows for (int sourceY=0, targetY=0; sourceY < flowerPicture.getHeight(); sourceY+=2, targetY++) { sourcePixel = flowerPicture.getPixel(sourceX,sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } } } sourceY+= 2 means the same as sourceY = sourceY + 2;sourceY+= 2 means the same as sourceY = sourceY + 2;

    65. Trying the copyFlowerSmaller Method Create a new picture half the size of the original picture (+ 1 if odd size) Picture p1 = new Picture(320,240); Copy the flower to the new picture p1.copyFlowerSmaller(); Show the result p1.show();

    66. Something Interesting Notice on the previous slide that we created a new Picture object of the exact size we needed, instead of using one of the larger empty pictures We could modify our scaling method to create a new Picture object inside itself using that approach, and we could return this new Picture as the result of the method For that matter, we could do the same with our copying and rotation methods too, so they create the new Picture object for us …

    67. Thinking Through Scaling Up Copy each pixel in the source multiple times to the target Source (0,0) Target (0,0) Source (0,0) Target(1,0) Source (1,0) Target(2,0) Source (1,0) Target(3,0) Source (2,0) Target(4,0) Source (2,0) Target(5,0) Source (0,0) Target(0,1) Source (0,0) Target(1,1) What could we add to source x and source y so that it is 0 twice, then 1 twice, then 2 twice, etc. Remember that 0.5 is 0 when you cast to integer. What could we add to source x and source y so that it is 0 twice, then 1 twice, then 2 twice, etc. Remember that 0.5 is 0 when you cast to integer.

    68. Scaling Up Algorithm Create the target picture Invoke the method on the target picture Create the source picture Loop with source x starting at 0 and target x starting at 0 as long as < source width Increment the source x by 0.5 each time through the loop, increment the target x by 1 Loop with source y starting at 0 and target y starting at 0 as long as < source height Increment the source y by 0.5 each time through the loop, increment the target y by 1 Copy the color from the source to target pixel The source x and y will need to be double variables. Cast them to int to lose the fractional part to get the pixel x and y.The source x and y will need to be double variables. Cast them to int to lose the fractional part to get the pixel x and y.

    69. Scaling Up Method public void copyFlowerLarger() { Picture flowerPicture = new Picture( FileChooser.getMediaPath("flower1.jpg")); Pixel sourcePixel = null; Pixel targetPixel = null; // loop through the columns for (double sourceX = 0, targetX=0; sourceX < flowerPicture.getWidth(); sourceX += 0.5, targetX++) {

    70. Scaling Up Method - Continued // loop through the rows for (double sourceY=0, targetY=0; sourceY < flowerPicture.getHeight(); sourceY += 0.5, targetY++) { sourcePixel = flowerPicture.getPixel((int)sourceX,(int)sourceY); targetPixel = this.getPixel((int)targetX,(int)targetY); targetPixel.setColor(sourcePixel.getColor()); } } } sourceY+= 2 means the same as sourceY = sourceY + 2;sourceY+= 2 means the same as sourceY = sourceY + 2;

    71. Trying the copyFlowerLarger Method Create a new picture twice the size of the original Picture p1 = new Picture(200,200); Copy the flower to the new picture p1.copyFlowerLarger(); Show the result p1.show(); Remember we can save the result to a file using p1.write("file");

    72. More General Scaling Up Scaling up a picture by a factor other than 2 can’t be reliably done following the approach we just used Rounding and other errors creep in, and results just don’t work out very well So we need a different approach to do it in general for other scaling factors While we’re at it, we’ll modify our method as discussed earlier to have it create and return a new Picture object as its result

    73. More General Scaling Up To do our scaling, we will introduce extra loops that will copy pixels multiple times The number of copies required to scale the image will be passed into our new method as a parameter Instead of being called on the target Picture object, as we’ve been doing so far, we will invoke this method on the source Picture When done, this method will return a new Picture object, which is the target created inside the new method

    74. More General Scaling Up Method public Picture scaleUp(int numTimes) { Picture targetPicture = new Picture(this.getWidth() * numTimes, this.getHeight() * numTimes); Pixel sourcePixel = null; Pixel targetPixel = null; int targetX = 0; int targetY = 0; // loop through the source picture columns for (int sourceX = 0; sourceX < this.getWidth(); sourceX ++) { // loop through the source picture rows for (int sourceY=0; sourceY < this.getHeight(); sourceY ++) { // get the source pixel sourcePixel = this.getPixel(sourceX,sourceY);

    75. More General Scaling Up Method - Cont // loop copying to the target y for (int indexY = 0; indexY < numTimes; indexY++) { // loop copying to the target x for (int indexX = 0; indexX < numTimes; indexX++) { targetX = sourceX * numTimes + indexX; targetY = sourceY * numTimes + indexY; targetPixel = targetPicture.getPixel(targetX, targetY); targetPixel.setColor(sourcePixel.getColor()); } } } } // return the created target Picture out of the method return targetPicture; }

    76. Trying the scaleUp Method For example, let’s use this on flower1.jpg String fileName = FileChooser.getMediaPath( "flower1.jpg"); Picture origPicture = new Picture(fileName); Picture scaledPicture = origPicture.scaleUp(2); origPicture.show(); scaledPicture.show();

    77. Create a Collage One of the things that you can do with pictures is create a collage There are two pictures of flowers flower1.jpg flower2.jpg Both pictures are 100 pixels wide The target picture is created from file 640x480.jpg

    78. Create Collage Algorithm Create the target picture object Using the 640x480 file Invoke the method on the target picture Create the flower picture objects using flower1.jpg as source1Picture using flower2.jpg as source2Picture Set targetBottomY to the targetPicture height – 5 5 pixels from bottom of picture

    79. Create Collage Algorithm - Cont Copy from source1Picture to the targetPicture starting at (0,0) ending at (width-1,height-1) to targetPicture (targetStartX,targetBottomY - height) Add the width of source1Picture to targetStartX Copy from source2Picture to the targetPicture starting at (0,0) ending at (width-1,height-1) to targetPicture(targetStartX,targetBottomY - height) Add the width of source2Picture to targetStartX Negate source1Picture Copy from source1Picture to the targetPicture starting at (0,0) ending at (width-1,height-1) to targetPicture(targetStartX,targetBottomY - height) Add the width of source2Picture to targetStartX Clear the blue from source2Picture Copy from source2Picture to the targetPicture starting at (0,0) ending at (width-1,height-1) to targetPicture(targetStartX,targetBottomY - height) Add the width of source2Picture to targetStartX Copy from source1Picture to the targetPicture starting at (0,0) ending at (width-1, height-1) to targetPicture(targetStartX,targetBottomY - height)

    80. Create Flower Collage Method public void createFlowerCollage() { Picture source1Picture = new Picture(FileChooser.getMediaPath("flower1.jpg")); Picture source2Picture = new Picture(FileChooser.getMediaPath("flower2.jpg")); int targetBottomY = this.getHeight() - 5; // copy source1Picture to 0, targetBottomY - height this.copy(source1Picture,0,0, source1Picture.getWidth(), source1Picture.getHeight(), 0,targetBottomY - source1Picture.getHeight()); // copy source2Picture to 100, targetBottomY - height this.copy(source2Picture,0,0, source2Picture.getWidth(), source2Picture.getHeight(), 100,targetBottomY - source2Picture.getHeight());

    81. Create Flower Collage Method - Cont // negate the source1Picture source1Picture.negate(); // copy negated source1Picture to 200 this.copy(source1Picture,0,0, source1Picture.getWidth(), source1Picture.getHeight(), 200,targetBottomY - source1Picture.getHeight()); // remove blue from source2Picture source2Picture.clearBlue();

    82. Testing createFlowerCollage String file = FileChooser.getMediaPath("640x480.jpg"); Picture p = new Picture(file); p.show(); p.createFlowerCollage(); p.repaint();

    83. Create Collage Exercise Try creating a collage At least 4 copies of an image in it The original image and 3 changes to the original image Scale, rotate, crop, change colors Then mirror the whole picture horizontally Save your collage using pictureObj.write(fileName); See http://coweb.cc.gatech.edu/cs1315/916 for some examples from Georgia Tech students.See http://coweb.cc.gatech.edu/cs1315/916 for some examples from Georgia Tech students.

    84. Summary A two-dimensional array or matrix has rows and columns, and is a good way of working with Pictures Use nested loops to work with these arrays One loop inside of another’s block Write methods so that they can be reused Do one and only one thing Take parameters to make them more reusable Methods can be overloaded Same names but different parameter lists

More Related