Picture Lab A8: Creating a Collage
From CS Awesome 2CC BY-NC-SA 4.0Original page
Java1 class period3 exercises6 graded tests
Files
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.
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
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.
Exercise 1
Picture Lab A8: Run to see createCollage() working.
Open the
01-picture-lab-A8-createCollagefolder and runPicture.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

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-copyPartialfolder and editPicture.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
- Create a
myCollagemethod 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-myCollagefolder and editPicture.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.
For teachers
Picture Lab A8: Creating a Collage
Students read CSAwesome2's “Picture Lab A8: Creating a Collage” in CS Room and work through its 3 Java coding exercises (2 graded, 1 example).
- Time: 1 class period
- Tests: 6 in 2 graded exercises, set up for you when you import the lesson
Goals
- Practice for Unit 4: Data Collections.
Before class
- Import the lesson into your class from the Lessons page, or with New assignment → Use a lesson. It arrives as a draft with its tests already set up.
- Publish it.
- The original page is on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/pictureLabA8.html
- CS Awesome's own teacher materials: the CS Awesome teacher site
In class
- Students read
README.md. - Students do exercises 01–03, each in its own folder, pressing Run to try their code.
- Run Tests checks every graded exercise at once; scores appear in the Gradebook. Grading uses your copy of the tests, so a student who edits theirs does not change the grade.
Exercises
| Folder | File | Grading | Tests |
|---|---|---|---|
01-picture-lab-A8-createCollage | Picture.java | Example, not graded (draws pictures) | 0 |
02-picture-lab-A8-createCollage-copyPartial | Picture.java | Checks code text only (draws pictures; graded without the drawing) | 3 |
03-picture-lab-A8-myCollage | Picture.java | Checks code text only (draws pictures; graded without the drawing) | 3 |
Teachers can download a reference solution after signing in.