CSC 171
Homework 2
Animations

Assigned Feb 15, due Feb 25

The first few problems below are ImageMaps (see lecture notes from Feb. 15). For each such problem, you'll write a class that implements ImageMap. One method in this class, pixelColor, is prescribed: its name must be exactly pixelColor, it must take be public and not static, it must take in two ints, a Color, and an Object in that order, and it must return a Color. You may want to write other methods of your own to help pixelColor do its job.

You should write an appropriate number of test cases (using checkExpect) for each such method, preferably in a separate test class. Once all your methods have passed all their test cases, write some test cases that call map on various images and an instance of your ImageMap class; for the latter, don't even try to do a checkExpect, but just show the results.

Each problem below is an animation or game. In most cases, you'll write a class (using the “Animation Class” template in BlueJ) for each animation, with an associated test class that contains test cases for the event-handling methods and any other helper methods you decide to write. If you want the animation run in a particular way (e.g. with a particular initial world or particular arguments to bigBang), write a testAnimation method that calls bigBang with the arguments you had in mind. Note: bigBang doesn't return anything useful, so don't bother trying to use checkExpect on it.

For each animation, be sure to follow the design recipe for animations.

For each method, be sure to follow the design recipe for methods.

Be sure to choose meaningful names for methods and parameters, and watch for opportunities to re-use methods you, I, or Dr. Stemkoski have already written.

Also turn in a log of how many errors of different kinds you encountered in the assignment, with brief comments describing each one ("mismatched parentheses" is self-explanatory, but more complex errors might need more description). You may do this using the PSP forms, or simply by keeping track in a text file or on paper and turning it in.

How to turn this in

You are encouraged to do this in teams of two students; turn in one copy of the project with both names on it (in the README file or in comments in the Java source files). Even if you do the assignment without a partner, make sure your name is on it.

If the “Submit” button works on your installation of BlueJ, you can use that to package up the entire project and send it to me and Dr. Stemkoski. Or you can use “Create JAR File…” and either e-mail us the JAR file or upload it to Moodle.

Pixel manipulation

  1. Develop an ImageMap (call the class DeRedder) that removes all the red from a given image, leaving the green and blue components of every pixel unchanged. (Done in class.)
  2. Develop an ImageMap (call the class BGSwapper) that swaps the green and blue components of a given image, leaving the red component unchanged.
  3. Develop an ImageMap (call the class RedDistance) that makes the red component of each pixel of an image approximately equal to that pixel's distance from (0,0); leave the blue and green components unchanged. (You'll need to make sure this number doesn't go above 255 somehow, and you may need to use the (int) casting technique described in animation 2, below.)
  4. Develop an ImageMap (call the class RedCone) that makes the red component of each pixel of an image approximately equal to that pixel's distance from the center of the image. (You'll need to pass in the whole image, or its center location, as the "extra" parameter to map.)
  5. Develop an ImageMap that does something else interesting with a given image, preferably location-dependent. Go wild.

Animations

  1. Develop an animation (name the class HW2_1) with an image of your choice (I suggest a fairly small one, like a stick-figure) rotating at a particular place on a fixed background image, with another copy rotating in the opposite direction in another place on that background. Something like this.

  2. Develop an animation (class HW2_2) in which a dot moves around the screen according to a formula: after t time steps, it should be at location
    (100 + 50 cos(t/10), 100 + 30 sin (t/10)).
    It should look something like this (although that program was written in a different language, it's the same exercise).

    Note: You'll need to use the Math.sin and Math.cos methods, and you'll need to convert doubles to ints using casting syntax. For example, (int)3.2 evaluates to the int 3; (int)(3.2+4.6) evaluates to the int 7; and so on. Note that (int)3.2+4.6 evaluates to the double 7.6; do you understand why?

    Replace the "magic numbers" 100, 50, 30, and 10 in the above formula with appropriately-named variables. Experiment with different values.

  3. Develop an animation (class HW2_3) in which a dot alternates between locations (20, 50) and (60, 50) once per second, against a fixed background. You shouldn't need an if statement to do this.

  4. Develop an animation (class HW2_4) in which a dot moves randomly in the x dimension: at every clock tick, it moves left one pixel, or right one pixel, or not at all, each with probability 1/3.

  5. Develop an animation (class HW2_5) in which an initial background image is gradually covered with dots: at every clock tick, a dot is added at a random location.

  6. Develop an animation (class HW2_6) of a digital counter: the user should see the number of clock ticks so far as a decimal numeral, in 18-point blue font in the animation window..

  7. Develop an animation (class HW2_7) that shows the current mouse coordinates as a parenthesized ordered pair, in 18-point blue font in the animation window. It should look something like this (again, that program was written in a different language, but it's the same exercise).

  8. Develop an animation (class HW2_8) in which a dot moves randomly in both dimensions: at every clock tick, it moves left, right, or not at all in the x dimension, and also moves up, down, or not at all in the y dimension.

  9. Develop an animation (class HW2_9) of a solid colored disk whose color changes with time and the mouse location: the mouse's x coordinate should control the amount of red, the y coordinate should control the amount of green, and the number of clock ticks so far should control the amount of blue.

  10. Develop an animation (class HW2_10) that starts with an image of your choice, and at every clock tick increases the green component of every pixel by 1 (but not going beyond 255).

  11. Develop an interesting animation of your own, using the techniques we've seen so far. Go wild.

Grading standards

Error log:       /10
(I'm not grading on how many or how few errors you encountered, only on whether you recorded them adequately.)

Programming: To save time and get you feedback more quickly, I probably won't grade all of the problems, but a selection of them. Each ImageMap requires writing a class with one or two methods, and each animation requires writing a class with two or more methods.

General skills:

Following directions /10
Writing contracts from word problems /10
Choosing test cases /10
Choosing names; readability /10
Coding /10
Code re-use and function composition /10

Last modified:
Stephen Bloch / sbloch@adelphi.edu