Summary: Learn how to use shapes to clip images during the drawing process.
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 use shapes to clip images during the drawing process.
Program specifications
Write a program named Prob04 that uses the class definition shown in Listing 1 and Ericson's media library along with the image file named Prob04a.jpg (see Image 1 ) to produce the graphic output images shown in Image 2 and Image 3 . Don't forget to display your name in the output image as shown.
| Image 1: Input file named Prob04a.jpg. |
|---|
![]() |
| Image 2: First output image. |
|---|
![]() |
| Image 3: Second 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 Prob04 shown in Listing 1 .
Rotate, mirror, and clip
The program rotates a Picture object by 35 degrees with no scaling. Then it does a four-way mirror on the rotated picture. Finally, it clips the image to an elliptical format as shown in Image 3 .
Required output text
In addition to the two output images shown above, your program must display your name and the other line of text shown in Image 4 .
| Image 4: Required text output. |
|---|
|
Will discuss in fragments
I will discuss and explain this program in fragments. A complete listing of the program is provided in Listing 5 near the end of the module.
The driver class named Prob04
The driver class containing the main method is shown in Listing 1 .
| Listing 1: The driver class named Prob04. |
|---|
|
If you have been studying the earlier modules in this collection, no explanation of Listing 1 should be required.
Beginning of the class named Prob04Runner
The class named Prob04Runner begins in Listing 2 .
| Listing 2: Beginning of the class named Prob04Runner. |
|---|
|
Nothing new here
There is nothing new in Listing 2 .
After instantiating a new Picture object from the given image file, Listing 2 calls three methods to rotate, mirror, and display the picture, producing the graphic output shown in Image 2 .
All of the code to accomplish this is essentially the same as code that I have explained in earlier modules.
Clip the picture and display your name
Then Listing 3 calls the clipToEllipse method to clip the picture to an ellipse on a red background as shown in Image 3 . The clipToEllipse method is new to this module, so I will explain it shortly.
| Listing 3: Clip the picture and display your name. |
|---|
|
The remaining code in Listing 3 is a repeat of code that I have explained in earlier modules, so I won't have anything further to say about it.
The method named clipToEllipse
The method named clipToEllipse is shown in its entirety in Listing 4 .
| Listing 4: The method named clipToEllipse. |
|---|
|
Behavior of the clipToEllipse method
The clipToEllipse method receives an incoming parameter that is a reference to an object of the Picture class. Basically, here is what the method does:
The new code
The only code in Listing 4 that is new to this module is the call to the setClip method.
The setClip method is defined in the Graphics class and inherited into the Graphics2D class.
(Among other things, that means that it wasn't necessary for me to cast the Graphics object to type Graphics2D in Listing 4 .)
The setClip method
There are a couple of overloaded versions of the setClip method. The one used in Listing 4 requires an incoming parameter of the interface type Shape .
The Shape interface
Briefly, Sun tells us that the Shape interface "provides definitions for objects that represent some form of geometric shape."
There are several dozen classes that implement the Shape interface, one of which is the class named Ellipse2D.Double . Therefore, the object of that type that is instantiated in Listing 4 satisfies the type requirement for being passed to the setClip method.
Behavior of the setClip method
With regard to the behavior of the setClip method, Sun tells us that the method
"Sets the current clipping area to an arbitrary clip shape."
What is the significance of the clipping area?
The closest answer that I can find for that question is the following statement in Sun's description of the Graphics class:
"All rendering operations modify only pixels which lie within the area bounded by the current clip, which is specified by a Shape in user space and is controlled by the program using the Graphics object."
In other words...
The clipping area is analogous to the current clip . In this case, the position and shape of the current clip is the position and shape of the ellipse.
When the image is later drawn on the red Picture object, only those pixels within the ellipse are modified to show the image. The remaining pixels retain their original color, which was set to red early in Listing 4 .
End of discussion
That concludes my explanation of this program. You will find the methods that I didn't discuss in Listing 5 near the end of the module.
I encourage you to copy the code from Listing 5 . 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 Prob04a.jpg to download the required input image file.
In this module, you learned how to use shapes to clip images during the drawing process.
In the next module, you will learn how to merge pictures.
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 provided in Listing 5 below.
| Listing 5: Complete program listing. |
|---|
|
-end-