Hello, if you have any need, please feel free to consult us, this is my wechat: wx91due
ICS 21 / CSE 21: Introduction to Computer Science I
Lab 2: Animated SmileyPurpose
This assignment gives you some more practice in basic Java programming, including practice with "if" statements and simple loops, by enhancing the previous Smiley program to handle animation of a smiley face as it moves and interacts with the walls (boundaries) of the graphics window.
Reminder: Find yourself a new partner!
We're requiring you to work with a different partner on each assignment, so don't forget to find a new partner before you proceed with this assignment.
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.
A completed version of this program will work in the following way:
- When you execute the program (i.e., run the main method in the Smiley class), a graphics window appears, with a smiley face in it and colored walls around the edges.
- The exact shape and color of the smiley face and its parts are up to you, within the bounds that the face must be small enough that it can move around within the graphics window, and that it be recognizable as a smiley face.
-
The smiley face heads off in some direction toward one of the walls. When it hits one of the walls, it should:
- Change the color of its face to the color of the wall, and the wall color should become the color of the smiley face; in other words, the colors swap.
- 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 that is specified by a constant in the program. Every time the smiley hits a wall, the smiley and the wall swap colors and the smiley rebounds, heading to another wall.
- Closing the window ends the program, even if the window is closed while the smiley is still moving.
Technical details
For this assignment, we've again helped you get started by providing some code and lots of comments and documentation. All of your work is to be done in the SmileyAnimation class. We've provided several already-written classes (in compiled form) that provide functions that you will likely need to use in your program. Three of these in particular — SmileyFace, SmileyFacePart, and SmileyDisplay — have public methods you may need to employ, so we've included text files — SmileyFace.txt, SmileyFacePart.txt, and SmileyDisplay.txt — describing those public methods. You may be surprised that SmileyFace and SmileyFacePart are not provided in source code form, since you wrote them in the last assignment. But that is exactly the point: having written and thoroughly tested them, we can now use their methods to make and manipulate smiley face characteristics without paying attention to the classes' details; we don't need the source to use the classes' capabilities.
These files have been collected into an Eclipse project and zipped into an archive called 21Lab2.zip. Using the same procedure that you followed in the last assignment, import this project into your Eclipse workspace.
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 of 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 an example, here's an outline of one way to incrementally implement and test your program:
- Start by just constructing and painting the initial smiley face; don't animate it yet. Fill in methods that require a return value (such as those that check whether a wall has been hit) with a "bogus" return value initially, with the understanding that you'll write them correctly later.
- Move the smiley in one, chosen direction (not randomly) and make sure it goes in that direction. Change the chosen direction a few times and re-run the program, making sure each time that the smiley face goes where it's supposed to go.
- Using the chosen direction, make the smiley stop when it reaches a wall; repeat for all four walls.
- When the smiley hits a wall, swap the color of the wall with the face's color; repeat for all four walls.
- Have the smiley hit the wall "dead on," swap colors and have the smiley return back the way it came; repeat for all four walls.
- Have the smiley rebound from a wall at the three angles that can (later) randomly occur; repeat for each angle and each of the four walls.
- Choose the "bounce direction" randomly, as described above.
- Test the whole program with various starting positions, sizes, and colors of smiley faces.
About Lab Exam 2
Lab Exam 2 will be very similar, but perhaps not identical, to the program you are being asked to write for this assignment. The lab exam will ask you to implement one or more of the following methods from the SmileyAnimation class:
- The SmileyAnimation constructor
- animate
- hitSomething
- adjustColorAndDirection
- adjustColor
- adjustDirection
- hitLeftWall
- hitRightWall
- hitTopWall
- hitBottomWall