# Picture Lab A8: Creating a Collage

Each coding exercise has its own folder. Open its `.java` file and press **Run** to try it. **Run Tests** checks every graded exercise in this lesson at once.

You can copy one picture to another by copying the color from the pixels in one picture to the pixels in the other picture. To do this you will need to keep track of the row and column information for both the picture you are copying from and the picture you are copying to, as shown in the following copy method. The easiest way to do this is to declare and initialize both a `fromRow` and `toRow` in the outer for loop and increment them both at the end of the loop. A for loop can have more than one variable declaration and initialization and/or modification. Just separate the items with commas. The inner loop in this code uses that to create two loop variables, `fromCol` and a `toCol` which are both declared, initialized, and incremented.

```java
public void copy(Picture fromPic,int startRow, int startCol)
{
     Pixel fromPixel = null;
     Pixel toPixel = null;
     Pixel[][] toPixels = this.getPixels2D();
     Pixel[][] fromPixels = fromPic.getPixels2D();
     for (int fromRow = 0, toRow = startRow;
         fromRow < fromPixels.length &&
         toRow < toPixels.length;
         fromRow++, toRow++)
     {
          for (int fromCol = 0, toCol = startCol;
              fromCol < fromPixels[0].length &&
              toCol < toPixels[0].length;
              fromCol++, toCol++)
          {
               fromPixel = fromPixels[fromRow][fromCol];
               toPixel = toPixels[toRow][toCol];
               toPixel.setColor(fromPixel.getColor());
          }
    }
}
```

You can create a collage by copying several small pictures onto a larger picture. You can do some picture manipulations like zero blue before you copy the picture as well. You can even mirror the result to get a nice artistic effect (Figure 1).

![Collage with vertical mirror](images/picturelabcollage.png)

*Collage with vertical mirror*

The following method shows how to create a simple collage using the copy method.

```java
public void createCollage()
{
     Picture flower1 = new Picture("flower1.jpg");
     Picture flower2 = new Picture("flower2.jpg");
     this.copy(flower1,0,0);
     this.copy(flower2,100,0);
     this.copy(flower1,200,0);
     Picture flowerNoBlue = new Picture(flower2);
     flowerNoBlue.zeroBlue();
     this.copy(flowerNoBlue,300,0);
     this.copy(flower1,400,0);
     this.copy(flower2,500,0);
     this.mirrorVertical();
     this.show();
}
```

You can test this with the `createCollage` method below.

## Exercise 1

Picture Lab A8: Run to see createCollage() working.

> Open the `01-picture-lab-A8-createCollage` folder and run `Picture.java`. It is an example, so it has no tests. It draws pictures: **Run** saves the drawing as an image in its folder.

## More practice

![Copypartial](images/copypartial.png)

## Exercise 2

Picture Lab A8: Create a second copy method called copyPartial that adds parameters to allow you to copy just part of the fromPic. You will need to add parameters that specify the start row, end row, start column, and end column to copy from. Use it in your collage.

> Open the `02-picture-lab-A8-createCollage-copyPartial` folder and edit `Picture.java`. Press **Run** to try it; **Run Tests** checks it (3 tests). Its tests check your code, not what it prints: “copyPartial method”, “Checking that copyPartial contains 6 int parameters”, “Checking that copyPartial() contains 2 for loops”. It draws pictures: **Run** saves the drawing as an image in its folder, and **Run Tests** checks the code without the drawing.

## More practice

2. Create a `myCollage` method that has at least three pictures (can be the same picture) copied three times with three different picture manipulations and at least one mirroring. You can use the pictures flower1.jpg, flower2.jpg, snowflake.jpg, butterfly.jpg in this lesson.

## Exercise 3

Picture Lab A8: Create a myCollage method that has at least three pictures (can be the same picture) copied three times with three different picture manipulations and at least one mirroring.

> Open the `03-picture-lab-A8-myCollage` folder and edit `Picture.java`. Press **Run** to try it; **Run Tests** checks it (3 tests). Its tests check your code, not what it prints: “myCollage method”, “Checking that myCollage contains 3 copy calls”, “Checking that myCollage() calls a mirror method”. It draws pictures: **Run** saves the drawing as an image in its folder, and **Run Tests** checks the code without the drawing.
