You will do this exercise by yourself, but be quick to ask questions of your instructor, student assistants and classmates as desired.
Combine and apply concepts that you have practiced in previous exercise, especially how to:
Do this project using documented stubs throughout.
Use good style throughout.
For any points deducted, the grader will put a CONSIDER tag saying how many points were deducted and why.
“Earn back” may be available for this assignment; ask your instructor.
Here is a link to the General Instructions for Grading Programs.
You will implement this project in stages, testing at each stage to see if the project works correctly through that stage.
Before beginning work, read over the titles of the seven stages below. Those titles are the iterative enhancement plan for this program and will give you a sense of what you're up against. You should also look at the figures in each stage.
Once you have an idea of what’s expected, begin working at Stage 0 and proceed through the stages in order, getting help as needed.
Don't forget to commit your changes to SVN after each stage!
Here is a specification of this project:
A Face can draw, translate and rotate itself. It provides the following methods to encapsulate that behavior:
/** * Draws the Face onto the given Graphics2D object. * * @param graphics2 * Graphics object onto which to draw */ public void drawOn(Graphics2D graphics2) { // ... code elided ... } /** * Translates the Face by the given amount. * * @param translateX * Amount to translate in the x direction * @param translateY * Amount to translate in the y direction */ public void translate(double translateX, double translateY) { // ... code elided ... } /** * Rotates the Face by the given number of degrees. * * @param degreesToRotate * Number of degrees to rotate the Face (e.g. 180 to turn the * Face upside down) */ public void rotate(double degreesToRotate) { // ... code elided ... }
A FacesComponent class tests the drawing of Face objects
By producing something like the picture below
Here is the UML class diagram for Faces that we developed together in class.
From your individual repository, checkout the Java project called Faces
SVN Repositories
view to check out this project. Create a new class called FacesViewer with the usual main method.
Write documented stubs for FacesViewer and its main method.
Constructs and makes visible a 500 by 400 JFrame that has on it a JComponent that draws Face objects.
Implement and test your documented stubs.
Commit your work.
Recall that you don't normally draw directly onto a JFrame. Instead, you:
Proceed as follows:
Displays a circle and a filled circle.since that is all that FacesComponent should do at this stage.
Implement FacesViewer and FacesComponent per this stage and test your documented stubs.
Write documented stubs for Face, including (at this point) its:
private drawCircle method that (per the UML class diagram for Faces) has the following parameters (and only these parameters so far — the others will be implemented in forthcoming stages):
Also modify your documented stub for FacesComponent to indicate that it (at this stage)
Displays filled faces, but with no outline and no eyes and mouth yet
since that is all that FacesComponent should do at this stage.
Implement Face and FacesComponent per this stage and test your documented stubs.
Suggestion: in coding drawCircle, use the Graphics2D translate method .
For example:
// Change the coordinate system so that (0, 0) // is the center of the circle to be filled. graphics2.translate(centerX, centerY); // Construct and fill the circle. // Ellipse2D.Double needs the upper-left corner, // not the center, as its first two parameters. Ellipse2D.Double circle = new Ellipse2D.Double( -radius, -radius, 2 * radius, 2 * radius); graphics2.fill(circle); // Reset the coordinate system // to what it was before drawing the circle. graphics2.translate(-centerX, -centerY);
Augment/modify your documented stubs for Face to include (at this point):
Modify your documented stub for FacesComponent to indicate that it (at this stage)
Displays filled faces with outlines and eyes, but no mouth yet
since that is all that FacesComponent should do at this stage.
Also augment your documented stub for Face's drawCircle method to indicate that it has a parameter that determines whether to draw the circle filled or not.
Get the outlines working first. That should be easy.
You might want to make the outline with a thicker-than-default line, like this:
// Make lines 3 times their default thickness graphics.setStroke(new BasicStroke(3.0f)); ... // Draw (using the thicker lines) // Reset line thickness to its default graphics.setStroke(new BasicStroke(1.0f));
To draw the eyes:
Note that translating the Graphics2 object to the center of the Face and then rotating the Graphics2 object by -45 degrees makes it easier to draw the left eye (using your drawCircle method).
The rotate method of the Graphics2D class takes the degrees to rotate in radians:
graphics2.rotate(-eyeAngle * Math.PI / 180);
Augment your program to draw the mouths of your Faces, as in the picture.
Use an Arc2D.Double object to draw the mouth. Look up its documentation as needed, but briefly:
Implement a translate(double x, double y) method that translates the Face's position by the amounts given in the arguments. (See the specification in Stage 0 for code you can copy to create the documented stub.)
Implement a rotate(double angleToRotate) method that rotates the Face by the number of degrees given in the argument. (See the specification in Stage 0 for code you can copy to create the documented stub.)
GIGANTIC HINT!!!! The face itself and each eye is currently a circle created using Ellipse2D.Double
. It’s difficult to see whether you’re rotating a circle correctly! So, you may wish to temporarily change your code to draw squares instead. The constructor for Rectangle2D.Double
takes the same arguments as the one for Ellipse2D.Double
, so you can just switch to squares until you get rotation working. Be sure to switch back to circles before your final commit!
An Applet is Java code that can be run from a browser. As you will see, it is VERY easy to make an Application (which you have written) into an Applet.
Examine the FacesApplet code that we supplied. Note that it is trivial to make an Application into an Applet — just a few lines of boilerplate per the example that we supplied, plus a call to the JComponent class that your Application adds to a JFrame.
Outside of Eclipse, find the Faces.htm file in your bin subfolder of your Faces project.
Open the Faces.htm file in your bin subfolder (that is, double-click on the file), to bring it up in a browser. It should look very similar to what you just saw in the Applet Viewer.
Commit your project to your individual repository when you are done.
FacesComponent
class are OK, but you should otherwise use good style. So, consider doing a Control-shift-F on each of your files before your final commit.