Presentation is loading. Please wait.

Presentation is loading. Please wait.

NestedLoops-Mod7-part31 Two-Dimensional Arrays and Nested Loops – part 3 Bugs in the garden Originally by Barb Ericson Georgia Institute of Technology.

Similar presentations


Presentation on theme: "NestedLoops-Mod7-part31 Two-Dimensional Arrays and Nested Loops – part 3 Bugs in the garden Originally by Barb Ericson Georgia Institute of Technology."— Presentation transcript:

1 NestedLoops-Mod7-part31 Two-Dimensional Arrays and Nested Loops – part 3 Bugs in the garden Originally by Barb Ericson Georgia Institute of Technology

2 NestedLoops-Mod7-part32 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

3 NestedLoops-Mod7-part33 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 12 34 12 34 source target

4 NestedLoops-Mod7-part34 For loops A for loop can have multiple statements in each section with semicolons in between for(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

5 NestedLoops-Mod7-part35 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

6 NestedLoops-Mod7-part36 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 object Create the source picture object

7 NestedLoops-Mod7-part37 Convert the Algorithm to Code Loop through the source picture pixels

8 NestedLoops-Mod7-part38 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 pixels Set the target pixel color to the source pixel color

9 NestedLoops-Mod7-part39 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 12 34 12 34 0 1 0 1 0123 0 1 2 3 source target

10 NestedLoops-Mod7-part310 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;

11 NestedLoops-Mod7-part311 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


Download ppt "NestedLoops-Mod7-part31 Two-Dimensional Arrays and Nested Loops – part 3 Bugs in the garden Originally by Barb Ericson Georgia Institute of Technology."

Similar presentations


Ads by Google