nestedloops-mod7-part31 two-dimensional arrays and nested loops – part 3 bugs in the garden...
TRANSCRIPT
NestedLoops-Mod7-part3 1
Two-Dimensional Arrays and Nested Loops – part 3
Bugs in the garden
Originally by Barb EricsonGeorgia Institute of Technology
NestedLoops-Mod7-part3 2
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
NestedLoops-Mod7-part3 3
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 any picture – As the target picture
• Several blank pictures are available
– 640x480.jpg– 7inX95in.jpg
1 2
3 4
1 2
3 4
source
target
NestedLoops-Mod7-part3 4
For loops
• A for loop can have multiple statements in each section with semicolons in betweenfor(intialization; condition; increment){
// do something
}
• For example:for(int a=0, b=0; a<5; a++,b++){
System.out.println(“(a,b)=”+a+”, “+b);
}
(a,b)=0,0
(a,b)=1,1
(a,b)=2,2
(a,b)=3,3
(a,b)=4,4
NestedLoops-Mod7-part3 5
Copy Picture Algorithm
• Copy a picture to another picture– Create the target picture object– 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
NestedLoops-Mod7-part3 6
CopyOver Method – in a NEW class.public class Duplicate {
public static void copyOver () { String sourceFile = “name your source file here”; String targetFile = “name the target file here”; Picture sourcePicture = new Picture(sourceFile); Picture targetPicture = new Picture(targetFile);
Pixel sourcePixel = null; Pixel targetPixel = null;
Convert the Algorithm into code
//Create variables to hold the pixels while you // transfer the color over
Create the target picture objectCreate the source picture object
NestedLoops-Mod7-part3 7
Convert the Algorithm to Code
• Loop through the source picture pixels
NestedLoops-Mod7-part3 8
CopyOver Method - Continued
What goes here? Where do you get the target pixel from?
This code goes inside the nested loops so every pixel in the source file gets copied over into the target file.
Be sure your source file is smaller than your target file!
Get the source and target pixelsSet the target pixel color to the source pixel color
NestedLoops-Mod7-part3 9
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?– Use the same for loops
going through all pixels in the source picture but add startX and startY to the target x,y position
1 2
3 4
1 2
3 4
0
1
0
1
0 1 2 3
0
1
2
3
source
target
NestedLoops-Mod7-part3 10
What if you want to eliminate the background of the source picture?
• Blue backgrounds were made to eliminate
• Before changing the color of the target pixel, ask if the color of the source picture is blue
• Only change the target picture if the source is NOT blue.
Color src = sourcePixel.getColor( ):
if(!src.equals(Color.blue))
/// change the color of the target pixel
to src which is the color of the source pixel
NOTE: you must import the Color class to use Colors
import java.awt.Color;
NestedLoops-Mod7-part3 11
Copy to Position Exercise• First add a ladybug to
position 0,0 in the garden without any changes.
• Next: • Add code to eliminate any
blue in the source picture
• Last:• Add ladybugs to 3
different locations in the garden by with 3 different x and y values eliminating the blue background.
Use these pictures: ladybug.gif and garden.jpg