Summary: Learn how to mirror images both horizontally and vertically.
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 mirror images, both horizontally and vertically.
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 file named Prob02a.jpg (shown in Image 1 ) to produce the three graphic output images shown in Image 2 , Image 3 , and Image 4 .
| Image 1: Input file named Prob02a.jpg. |
|---|
![]() |
| Image 2: First output image. |
|---|
![]() |
| Image 3: Second output image. |
|---|
![]() |
| Image 4: Third output image. |
|---|
![]() |
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 shown in Listing 1 .
Rotate and mirror
The image from the file named Prob02a.jpg is rotated by 35 degrees. It is not scaled. Then the top-left quadrant of the picture containing the rotated image is mirrored into the top-right quadrant. Following this, the top half of the picture is mirrored into the bottom half.
Required output text
In addition to the three output images mentioned above, your program must display your name and the other line of text shown in Image 5 on the command-line screen
| Image 5: Required output text. |
|---|
|
Will discuss in fragments
I will discuss and explain this program in fragments. A complete listing of the program is provided in Listing 6 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. |
|---|
|
Instantiate a new object and call its run method
The code in the main method instantiates a new object of the class named Prob02Runner and calls the run method on that object.
When the run method returns, the main method terminates causing the program to terminate.
Beginning of the class named Prob02Runner
The beginning of the class named Prob02Runner , including the constructor, is shown in Listing 2 .
| Listing 2: Beginning of the class named Prob02Runner. |
|---|
|
The constructor displays the student's name, producing the first line of output text shown in Image 5 .
The run method
The run method that is called in Listing 1 is shown in its entirety in Listing 3 .
| Listing 3: The run method. |
|---|
|
Very familiar code
Except for the calls to the methods named mirrorUpperQuads and mirrorHoriz in Listing 3 , you should already be familiar with all of the code in Listing 3.
The rotatePicture method
For example, the call to the method named rotatePicture is essentially the same as code that I explained in an earlier module. Therefore, I won't explain that method again in this module. You will find the code for the method named rotatePicture in Listing 6 near the end of the module.
Operate on the picture with the rotated image
The original picture is replaced by a picture containing the rotated image shown in Image 3 . From this point forward, all operations are performed on the Picture object containing the rotated image.
The method named mirrorUpperQuads
The method named mirrorUpperQuads that is called in the run method in Listing 3 is shown in Listing 4 .
Behavior of the method named mirrorUpperQuads
This method mirrors the upper-left quadrant of a picture into the upper-right quadrant as shown in Image 6 .
| Listing 4: The method named mirrorUpperQuads. |
|---|
|
Declare four working variables
Listing 4 begins by declaring and initializing four working variables. The purpose of these variables should be obvious on the basis of their names and their initialization expressions.
Copy the pixel colors
Then Listing 4 uses a double nested for loop to copy the colors from the pixels in the upper-left quadrant into the pixels in the upper-right quadrant. This is done in such a way as to form a mirror image about the center point as shown in Image 6 .
The outer loop
The outer loop iterates on the rows of pixels in the top half of the image. Only the top half of the image is processed in this method because the top half will be mirrored into the bottom half later on.
The inner loop
The inner loop iterates on the columns in the left half of the image, copying pixel colors from the left half into the pixels in the right half.
Destruction of pixel colors
The colors in the pixels in the upper-right quadrant are overwritten by this method.
In effect, this method and the one following it destroys all of the pixel colors originally in the right half of the picture of the rotated image and all of the pixel colors originally in the bottom half of the picture.
The final picture shown in Listing 4 contains only pixel from the upper-left quadrant of the picture with the rotated image.
Return a modified Picture object
Finally, the code in Listing 4 returns the modified Picture object to the run method in Listing 3 .
At this point, the picture with the rotated image is replaced by the version of the picture returned by the mirrorUpperQuads method.
Picture output from the mirrorUpperQuads method
If you were to display the picture at that point, you would see the image shown in Image 6 .
| Image 6: Picture output from the mirrorUpperQuads method. |
|---|
![]() |
The upper-left quadrant has been mirrored
As you can see from Image 6 , at this point in the process, the upper-left quadrant has been mirrored into the upper-right quadrant, but the bottom half of the picture is undisturbed. It's time to do something about that.
Call the mirrorHoriz method
The next statement in the run method in Listing 3 is a call to the mirrorHoriz method passing the picture shown in Image 6 as a parameter.
The method named mirrorHoriz
The method named mirrorHoriz is shown in Listing 5 . This method mirrors the top half of a picture into the bottom half of the same picture. It will be used to mirror the top half of the picture in Image 6 into the bottom half.
| Listing 5: The method named mirrorHoriz. |
|---|
|
Very similar to the mirrorUpperQuads method
This method is very similar to the previous method named mirrorUpperQuads .
Four working variables and a nested for loop
As before, Listing 5 declares and initializes four working variables. These variables are used in a nested for loop to copy pixel colors from the top half of the picture into the pixels in the bottom half.
The outer and inner loops
In this case, the outer loop iterates on all of the columns going from left to right.
The inner loop iterates on rows, from the top row to the vertical midpoint, copying the colors from the pixels from the top half into the pixels in the bottom half.
The end of the class
Listing 5 also signals the end of the class named Prob02Runner and the end of the program.
I encourage you to copy the code from Listing 6 . 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 Prob02a.jpg to download the required input image file for this program.
You learned how to mirror images both horizontally and vertically.
In the next module, you will learn to use a variety of Java2D classes including GradientPaint.
Select the following link to view an online video lecture 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 6 below.
| Listing 6: Complete program listing. |
|---|
|
-end-