Time estimate: 90 min.

8.9.6. Picture Lab A8: Creating a Collage

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.

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).

../_images/picturelabcollage.png

Figure 1: Collage with vertical mirror

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

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.

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

coding exercise Coding Exercises

../_images/copypartial.png

1. 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.

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.

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. To use your own images, you can fork this replit.com Swing project or this alternative replit.com project (click output.jpg to see the result) or download the project files form replit to your own IDE.

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.

You can use these images in this lesson:

Data file: flower1.jpg
Data file: flower2.jpg
Data file: snowflake.jpg
Data file: butterfly.jpg
You have attempted of activities on this page