Summary: Learn to crop, flip, and combine pictures.
This module is one of a series of modules designed to teach you about Object-Oriented Programming (OOP) using Java.
The program described in this module requires the use of the Guzdial-Ericson multimedia class library. You will find download, installation, and usage instructions for the library at Java OOP: The Guzdial-Ericson Multimedia Class Library .
I recommend that you open another copy of this document in a separate browser window and use the following links to easily find and view the images and listings while you are reading about them.
In this module, you will learn how to:
Program specifications
Write a program named Prob02 that uses the class definition shown in Listing 1 and Ericson's media library along with the image files named Prob02a.jpg and Prob02b.jpg to produce the three graphic output images shown in Image 1 , Image 2 , and Image 3 .
| Image 1: Raw butterfly image. |
|---|
![]() |
| Image 2: Beach scene with student's name added. |
|---|
![]() |
| Image 3: Composite image. |
|---|
![]() |
May define new classes
You may define new classes as necessary to cause your program to behave as required, but you may not modify the class definition for the class named Prob02 given in Listing 1 .
The facing butterfly images
The two facing images of the butterflies in the final output picture are separated by two pixels and those two images as a pair are centered in the picture of the beach.
Required text output
In addition to the three output images mentioned above, your program must display your name and the other three lines of text shown in Image 4 on the command-line screen:
| Image 4: Required text output. |
|---|
|
This program copies a rectangular portion of a picture of a butterfly into a specific location in a picture of a beach.
The program also crops the butterfly picture to the same size as the portion that was copied into the beach picture and flips the cropped version to cause the butterfly to face left instead of facing right.
Then it copies the cropped and flipped image to a location two pixels to the right of the original copy of the butterfly in the beach image.
The two resulting images of the butterfly within the beach image are separated by two pixels, face one another, and are centered in the picture of the beach as shown in Image 3 .
Major evaluation areas
In order to successfully write this program, the student must, as a minimum be able to:
Will discuss in fragments
I will discuss this program in fragments. A complete listing of the program is provided in Listing 10 near the end of the module.
The driver class named Prob02
The driver class containing the main method is shown in Listing 1 .
| Listing 1: The driver class named Prob02. |
|---|
|
A reference to an array object
The call to the run method in Listing 1 may be new to you. This call expects to receive a reference to an array object of type Picture[] as a return value.
Save return value in variable named pictures
The return value from the run method is stored in the local reference variable named pictures .
Extract and print references to Picture objects
Then the reference variable is used to extract references to the individual Picture objects encapsulated in the array. Those references are passed to the println method causing the last three lines of text shown in Image 4 to be displayed on the command line screen.
Beginning of the Prob02Runner class
The class named Prob02Runner begins in Listing 2 , which shows the constructor for the class.
| Listing 2: Beginning of the Prob02Runner class. |
|---|
|
The constructor simply causes the student's name to be displayed on the command line screen, producing the first line of output text shown in Image 4 .
Beginning of the run method
The run method, that was called in Listing 1 begins in Listing 3 .
| Listing 3: Beginning of the run method. |
|---|
|
Listing 3 instantiates two Picture objects from image files and displays them by calling the explore method on each Picture object. In addition, the student's name is added near the upper-left corner of the beach image. This code results in the images shown in Image 1 and Image 2 .
Call the cropAndFlip method
Then Listing 3 calls the cropAndFlip method passing the reference to the butterfly image of Image 1, along with some other information as parameters. The return value is stored in a new local reference variable of type Picture named picC .
Put discussion of the run method on hold
I will put the discussion of the run method on temporary hold at this point and explain the method named cropAndFlip , which begins in Listing 4 .
Beginning of the cropAndFlip method
The cropAndFlip method crops a picture to the specified coordinate values and flips it around a vertical line at its center.
| Listing 4: Beginning of the cropAndFlip method. |
|---|
|
Incoming parameters
In addition to a reference to the picture to be processed, the method receives four incoming integer values as parameters. The parameters named x1 and y1 specify the coordinates of the upper-left corner of a rectangular area of the picture that is to be retained in the output.
The parameters named x2 and y2 specify the coordinates of the lower-right corner of the rectangular area of the picture that is to be retained in the output.
An empty Picture object
Listing 4 begins by creating an empty Picture object of the correct size to hold the cropped image. A reference to the empty picture is saved in the local reference variable named output .
Then Listing 4 gets and saves the width of the output picture.
Following this, Listing 4 declares two local working variables named pixel (of type Pixel ) and color (of type Color ) .
Process using nested loops
Listing 5 uses a pair of nested for loops to cause the output picture to be a cropped version of the picture received as an incoming parameter. The cropped image is flipped around its center.
| Listing 5: Process using nested loops. |
|---|
|
The code in Listing 5 copies the pixel colors of the selected pixels of the incoming image to the pixels of the output image, flipping the image around its center line in the process.
Cropped and flipped version of the butterfly image
If you display the picture referred to by output in Listing 5 , you will get the image shown in Image 5 .
| Image 5: Cropped and flipped version of the butterfly image. |
|---|
![]() |
Compare with the original butterfly picture
If you compare Image 5 with Image 1 , you will see that pixels on the outer edges of Image 1 have been discarded and the resulting image has been flipped around its centerline.
End of the cropAndFlip method
Image 5 returns a reference to the new image and ends the method named cropAndFlip . The returned value is stored in the variable named picC in Listing 3 .
Original image not modified
Note that the code in the cropAndFlip method does not modify the original image of the butterfly. Instead, it extracts pixel data from the original image to produce a new image. When control returns to the run method in Listing 3 , a reference to the new image is stored in the variable named picC .
Call the copyPictureWithCrop method from the run method
Control has now returned to the run method, picking up where Listing 3 left off. The next statement in the run method is shown in Listing 6 .
| Listing 6: Call the copyPictureWithCrop method from the run method. |
|---|
|
Put the run method on hold again
Once again, I will put the run method on hold while I explain the method named copyPictureWithCrop , which begins in Listing 7 .
Beginning of the method named copyPictureWithCrop
The first two incoming parameters named source and dest are references to a source picture and a destination picture.
When the method is called in Listing 6 , the source picture is the original butterfly picture shown in Image 1 and the destination picture is the beach picture shown in Image 2 .
| Listing 7: Beginning of the method named copyPictureWithCrop. |
|---|
|
Copy source to destination
The method named copyPictureWithCrop copies part of the source picture into the destination picture with an offset on both axes after first confirming that the part will fit. The method does nothing if the part won't fit.
The copy process causes selected pixel colors in the destination picture to be replaced by pixel colors from the source picture.
The offset values
The next two parameters named xOff and yOff in Listing 7 specify the location in the destination picture where the upper-left corner of the cropped source picture is to be located.
The statement in Listing 6 passes the values (82,70) for these two values. This is the location of the upper left corner of the left-most butterfly image in Image 3 .
For clarity, I will refer to this as a cropped source picture even though the program doesn't actually save a cropped version of the picture as was the case with the cropAndFlip method.
The program simply copies a rectangular portion of the source picture into the destination picture.
Upper-left cropping corner
The parameters named xCoor and yCoor in Listing 7 specify the upper-left corner of the rectangular area of pixels that is to be preserved when the source image is cropped.
Coordinate values of (4,5) are passed for these two values when the method is called in Listing 6 .
Same values as Listing 3
Note that these are the same two values that were passed for this purpose when the cropAndFlip method was called in Listing 3 .
Two ways to specify a rectangle
There are two commonly used ways to specify a rectangular area in programming. One way is to specify the coordinates of the upper-left and bottom right corners. This is the approach used in the cropAndFlip method in Listing 4 .
The other way is to specify the coordinates of the upper-left corner and then to specify the width and the height. This is the approach used in the copyPictureWithCrop method in Listing 7 .
The width and height parameters
The parameters named width and height in Listing 7 specify the width and height of the rectangular area of pixels that is to be preserved when the source picture is cropped.
If you compare the width and height parameter values passed in Listing 6 with the coordinate values passed in Listing 3 , you will see that the same rectangular area of the butterfly image is being preserved after cropping in both cases.
Confirm that the cropped image will fit
Listing 7 begins by confirming that the cropped rectangular area of the source picture will fit within the destination picture when placed at the specified location. If the conditional clause of the if statement returns true, then the code in the body of the statement will be executed. If not, control bypasses the body of the if statement and the source picture will not be copied into the destination picture.
Process using nested for loops
As was the case in Listing 4 , Listing 7 declares two working variables named pixel and color .
The variables named pixel and color are used along with various parameter values in the pair of nested for loops shown in Listing 8 to crop the source picture and to copy the cropped source picture into the destination picture at the specified location.
| Listing 8: Process using nested loops. |
|---|
|
Not as complicated as it looks
Although the arithmetic operations involved in Listing 8 can be daunting, the code in Listing 8 is doing nothing more than replacing selected pixel colors in the destination picture with selected pixel colors from the source picture.
Partially complete version of the output picture.
If you were to display the destination picture before returning control back to the run method in Listing 8 , you would see the image shown in Image 6 .
| Image 6: Partially complete version of the output picture. |
|---|
![]() |
At this point, only one cropped version of the butterfly image has been copied into the beach image.
Return control to the run method
The copyPictureWithCrop method terminates in Listing 8 and returns control to the run method, picking up where Listing 6 left off.
The remainder of the run method
The remainder of the run method is shown in Listing 9 .
| Listing 9: The remainder of the run method. |
|---|
|
Call the copyPictureWithCrop method again
Listing 9 begins by calling the copyPictureWithCrop method again. This time, however, the picture shown in Image 5 is passed as the source image with the same picture as before being passed as the destination image.
The offset coordinates
In this case, the offset coordinate values specify the upper-left corner of the right-most butterfly image in Image 3 .
The cropping parameters
The final four parameters that are passed in Listing 9 specify that the entire source picture is to be copied into the destination picture.
Display the destination picture
When the copyPictureWithCrop method returns, Listing 9 calls the explore method to display the current state of the destination picture. The result is shown in Image 3 .
A new array object
Finally, Listing 9 instantiates a new array object, populates it with references to three Picture objects, and returns control to the main method code in Listing 1 returning a reference to the array object in the process.
The code in Listing 1 saves the reference to the array object in the variable named pictures .
Pass Picture object references to println method
Then Listing 1 extracts and passes each of the three Picture object references to the println method causing the last three lines of text shown in Image 4 to be displayed on the command-line screen.
The second line of output text ( picA ) describes the raw butterfly image shown in Image 1 .
The third line of output text for ( picB ) describes the beach scene shown in Image 2 and Image 3 .
The last line of output text ( picC ) describes the cropped and flipped version of the butterfly image shown in Image 5 .
I encourage you to copy the code from Listing 10 . Compile the code and execute it. Experiment with the code, making changes, and observing the results of your changes. Make certain that you can explain why your changes behave as they do.
Click here and here to download the two required input image files.
In this module, you learned how to:
You will l earn to write a program to do green-screen processing in the next module.
Select the following links to view online video lectures on the material in this module.
This section contains a variety of miscellaneous information.
Financial : Although the Connexions site makes it possible for you to download a PDF file for this module at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should be aware that some of the HTML elements in this module may not translate well into PDF.
I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. I neither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it was made and published without my prior knowledge.
Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.
A complete listing of the program discussed in this module is shown in Listing 10 below.
| Listing 10: Complete program listing. |
|---|
|
-end-