Animated Smiley

Lab Assignment 2
to prepare for Lab Exam 2

This assignment gives you some more practice in basic Java programming, including practice with if statments and simple loops, by enhancing the previous Smiley program to handle animation of a smiley face as it interacts with the walls (boundaries) of the graphics window.


Program behavior and general requirements

In general, you are to complete the Java class SmileyAnimation, which we’ve added to the Smiley program so that smiley faces can be animated.

When Smiley is run, a graphics window appears, with a smiley face in it; the exact shape and color of the face are up to you, within the bounds that the face must be small enough that it can ’move around“ within the graphics window, and be recognizable as a smiley face (you will use the previously written SmileyFace class to make it). That smiley heads off towards a wall, an edge of the graphics window. When it hits the wall it should

  1. Change the color of its face to the color of the wall, and the wall color should become the color of the (background of) the smiley face; in other words, the colors swap.

  2. Rebound from the wall, either directly (going back the way it came), or at 45 degrees up or down (if it hit the left or right wall) or 45 degrees left or right (if it hit the top or bottom wall); the direction is randomly chosen.

This behavior continues for a number of milliseconds, set as a constant in the program; every time the smiley hits a wall, it and the wall swap colors and the smiley rebounds, heading to another wall.

The user clicks on the close box to close the window and end the program; if the close box is clicked while the smiley is still moving, the program still shuts down.


Technical Details

For this assignment, we’ve again helped you get started by providing some code and lots of comments and documentation. All your work is done in the SmileyAmination class. We provide several already-written class files that provide functions you will likely need to use in your program. Two of these, SmileyFace and SmileyDisplay, have public methods or constants you may need to employ, so we included text files, SmileyFace.txt and SmileyDisplay.txt, describing those public items. You may be surprised that SmileyFace is not provided in source code form, since you wrote that class in the last execise. But that is just the point: having written and thoroughly tested it, we can now use its methods to make and manipulate smiley face characterisics without paying attention to the class’ details; we don’t need the source to use the class’ capabilities.

These files are stored together in the archive file Lab2.zip. Download Lab2.zip to your computer and unzip it into its own folder. Replace all spaces marked // Your code goes here with your own Java code to obtain a complete, correctly working program.

As before, when compiling the program, use the command javac *.java to ensure all parts of the program are recompiled. (For more on this, see the Orientation to the Lab section of the manual.) Run the program with the command java Smiley.

Do remember that a significant failure to follow class style standards on a lab exam can result in a “not pass” for that exam.


Testing Tips

It’s typically fastest to implement and debug a program when it is done in stages; when something goes wrong, you know it was because of changes made at the current stage, thus reducing the amount of code you have to search to find the mistake. Testing in stages can take many forms. For this assignment, since all the methods you are writing are in one class, testing in stages boils down to adding and testing a method or two at a time, judiciously choosing them so that, as you add them, you are incrementally heading to a completed program.

For example, here’s an outline of one way to incrementally implement and test your program: