Two-Dimensional Arrays and Nested Loops – part 4

Slides:



Advertisements
Similar presentations
A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.
Advertisements

Conditionals-part11 Barb Ericson Georgia Institute of Technology Nov 2009.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 6 Barb Ericson Georgia Institute of Technology August 2005.
TOPIC 9 MODIFYING PIXELS IN A MATRIX: COPYING, CROPPING 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
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)
NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009.
How to use the Java class libraries Brief documentation of how to do this all with Java.
TOPIC 7 MODIFYING PIXELS IN A MATRIX NESTED FOR LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by.
NestedLoops-part11 Nested Loops – part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
CSC1401 Viewing a picture as a 2D image - 1. Review from the last week We used a getPixels() to get all of the pixels of a picture But this has been somewhat.
02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures.
Georgia Institute of Technology Movies part 5 Barb Ericson Georgia Institute of Technology April 2006.
TOPIC 11 RETURNING VALUES FROM METHODS PICTURE TRANSFORMATIONS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
CSE 8A Lecture 8 Reading for next class: PSA4: Collage and Picture Flip, DON’T WAIT (it’s longer than the previous PSAs)
NestedLoops-Mod7-part31 Two-Dimensional Arrays and Nested Loops – part 3 Bugs in the garden Originally by Barb Ericson Georgia Institute of Technology.
ManipulatingPictures-Mod6-part61 Manipulating Pictures, Arrays, and Loops: Eliminating color, Inversion, grey scale and adjusting for luminance Barb Ericson.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 5 Barb Ericson Georgia Institute of Technology August 2005.
NestedLoops-part41 Nested Loops – part 4 Barb Ericson Georgia Institute of Technology Nov 2009.
NestedLoops-Mody7-part51 Two-Dimensional Arrays and Nested Loops – part 5 Rotations Barb Ericson Georgia Institute of Technology May 2007.
Conditionals-Mod8-part41 Conditionals – part 4 Replace background Barb Ericson Georgia Institute of Technology May 2007.
CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.
Conditionals-part21 Conditionals – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
Georgia Institute of Technology Movies part 4 Barb Ericson Georgia Institute of Technology April 2006.
NestedLoops-part21 Nested Loops – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
CSC 112Introduction to Media Computation 1 Rotating Images.
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 2 Barb Ericson Georgia Institute of Technology August 2005.
NestedLoops-Mod7-part61 Two-Dimensional Arrays and Nested Loops – part 6 Enlarge Barb Ericson Georgia Institute of Technology August 2005.
Georgia Institute of Technology Drawing in Java – part 2 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology North Jeddah.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 4 Barb Ericson Georgia Institute of Technology August 2005.
Topic 9 Modifying Pixels in a Matrix: Copying, Cropping
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Barb Ericson Georgia Institute of Technology Dec 2009
Manipulating Pictures, Arrays, and Loops part 3
Georgia Institute of Technology
Barb Ericson Georgia Institute of Technology August 2005
Workshop for Programming And Systems Management Teachers
Manipulating Pictures, Arrays, and Loops part 2
Georgia Institute of Technology
Working with ranges in pictures
Manipulating Pictures, Arrays, and Loops
Barb Ericson Georgia Institute of Technology August 2005
Two-Dimensional Arrays and Nested Loops – part 1
Manipulating Pictures, Arrays, and Loops part 5
Two-Dimensional Arrays and Nested Loops – part 2
Two-Dimensional Arrays and Nested Loops – part 5
Two-Dimensional Arrays and Nested Loops – part 1
Arrays versus ArrayList
Manipulating Pictures, Arrays, and Loops part 4
Manipulating Pictures
Manipulating Pictures, Arrays, and Loops part 3
Introduction to Processing Digital Sounds part 3
Two-Dimensional Arrays and Nested Loops – part 3
Two-Dimensional Arrays and Nested Loops – part 6
Two-Dimensional Arrays and Nested Loops – part 2
Workshop for Programming And Systems Management Teachers
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops
Two-Dimensional Arrays and Nested Loops – part 6
Georgia Institute of Technology
Multidimensional Arrays
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops part 6
CSC1401 Viewing a picture as a 2D image - 2
Two-Dimensional Arrays and Nested Loops – part 6
Barb Ericson Georgia Institute of Technology May 2006
Manipulating Pictures, Arrays, and Loops
Processing Sound Ranges part 3
Manipulating Pictures, Arrays, and Loops part 6
Presentation transcript:

Two-Dimensional Arrays and Nested Loops – part 4 Georgia Institute of Technology

Georgia Institute of Technology Learning Goals Understand at a conceptual and practical level How to copy part of one picture to another? What makes a good method? How to write more general methods? By adding parameters to the method How to rewrite methods? To reduce copied code Georgia Institute of Technology

Georgia Institute of Technology Cropping We can copy just part of a picture to a new picture Just change the start and end source x and y values to the desired values Use pictureObj.explore() to find the x and y values What are the x and y values to get the face of the girl in KatieFancy.jpg? Georgia Institute of Technology

Georgia Institute of Technology Copy 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++) Georgia Institute of Technology

Copy 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()); } Georgia Institute of Technology

Testing Copy Katie’s Face 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(); Georgia Institute of Technology

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 be in the class that has the data the method is working on Georgia Institute of Technology

Where the last two methods general? We specified the file to copy from in the method Meaning we would need to change the method or make another method to copy a different picture Georgia Institute of Technology

General Copy Algorithm Create a 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 To the current picture object 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 Georgia Institute of Technology

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 Georgia Institute of Technology

Georgia Institute of Technology 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++, tx++) // loop through the y values for (int y = startY, ty = targetStartY; y < endY; y++, ty++) Georgia Institute of Technology

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()); } Georgia Institute of Technology

Rewrite Methods Exercise Type the copy method in Picture.java Rewrite copyKatie() and copyKatiesFace() methods to use the new copy method Run the methods to make sure they still work Georgia Institute of Technology

Georgia Institute of Technology Summary To copy part of one picture to another Change the start and end conditions in the for loop A good method should do one and only one thing Use parameters to make methods more reusable Don’t copy code from one method to another Create a general method instead And call the general method from other methods Georgia Institute of Technology