Two-Dimensional Arrays and Nested Loops – part 3

Slides:



Advertisements
Similar presentations
Two-Dimensional Arrays Chapter What is a two-dimensional array? A two-dimensional array has “rows” and “columns,” and can be thought of as a series.
Advertisements

Georgia Institute of Technology Movies part 3 Barb Ericson Georgia Institute of Technology April 2006.
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.
NestedLoops-part11 Nested Loops – part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures.
Georgia Institute of Technology Movies part 2 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.
NestedLoops-Mod7-part31 Two-Dimensional Arrays and Nested Loops – part 3 Bugs in the garden Originally by Barb Ericson Georgia Institute of Technology.
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.
CPSC1301 Computer Science 1 Chapter 4 Manipulating Pictures, Arrays, and Loops part 5.
Conditionals-Mod8-part41 Conditionals – part 4 Replace background Barb Ericson Georgia Institute of Technology May 2007.
Conditionals-part21 Conditionals – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
NestedLoops-part21 Nested Loops – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
CSC 112Introduction to Media Computation 1 Rotating Images.
ManipulatingPictures-part31 Manipulating Pictures, Arrays, and Loops part 3 Barb Ericson Georgia Institute of Technology Nov 2009.
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.
Test 2 on Wed, 11/9 On image processing
Barbara Ericson Georgia Tech Sept 2005
Topic 9 Modifying Pixels in a Matrix: Copying, Cropping
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Georgia Institute of Technology
Manipulating Pictures, Arrays, and Loops part 3
Georgia Institute of Technology
Georgia Institute of Technology
Barb Ericson Georgia Institute of Technology August 2005
Workshop for Programming And Systems Management Teachers
Manipulating Pictures
Barb Ericson Georgia Institute of Technology August 2005
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
Barb Ericson Georgia Institute of Technology August 2005
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
Georgia Institute of Technology
Arrays versus ArrayList
Manipulating Pictures, Arrays, and Loops part 4
Manipulating Pictures
Two-Dimensional Arrays and Nested Loops – part 4
Two-Dimensional Arrays and Nested Loops – part 6
Two-Dimensional Arrays and Nested Loops – part 2
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops
Two-Dimensional Arrays and Nested Loops – part 6
Georgia Institute of Technology
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops part 6
Chapter 6 Conditionals - part 4
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
Manipulating Pictures, Arrays, and Loops part 6
Presentation transcript:

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

Georgia Institute of Technology Learning Goals Understand at a conceptual and practical level How to copy pixels from one picture to another How to declare, initialize and change several variables in a for loop How to copy pixels from one picture to a specified location in another picture Georgia Institute of Technology

Copying Pixels to a New Picture 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 1 2 3 4 1 2 3 4 Georgia Institute of Technology

Copy Picture Algorithm Copy a picture to the 7 by 9.5 inch blank picture Create the target picture object Invoke the method on the target picture Create the source picture object 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 Georgia Institute of Technology

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

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

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

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

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

Result of copyKatie Method Georgia Institute of Technology

Copying Pixels to a New Picture What if we want to copy the target to a different location in the source Than 0,0 Say startX and startY What is an algorithm that will do this? 1 1 2 3 4 1 1 2 3 1 2 3 4 1 2 3 Georgia Institute of Technology

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

Georgia Institute of Technology Summary To copy pixels from one picture to another Keep track of the sourceX, sourceY and targetX and targetY You can declare, initialize, and change more than one variable in a for loop for (int sourceX = 0, targetX = 0; sourceX < sourcePicture.getWidth(); sourceX++, targetX++) To copy one picture to a location in another Just change the start values for targetX and targetY Georgia Institute of Technology