Often it is possible to create a number of shapes in a specific pattern algorithmically (i.e., using a loop and calculating the position, size, velocity, etc.). When combined with movement, this has the possibility of producing a very interesting effect, with very little effort. In fact, this is the basis of most screen savers, arguably, an art form.
You will start with the code screensaver.jar
After compiling and running the code to see what it does, you should and refactor it into an appropriately named "super" package (lastname.assign5).
For each of the problems below, you will complete the
createMovers
method of the appropriate factory subclass
that, given a Canvas
and an integer parameter, returns
nothing but creates the given number of shapes within the given
canvas in a specific pattern as described below. The current
Canvas
class will work without modification because, for
each problem below, you will modify the paint
and
move
methods of a separate class that extends
Mover
. To test your solutions, run the Main class, which will
cause a Java program to appear that allows you to call each
createMovers
method by pressing the appropriate button.
Racer
class such that it draws itself like a
rectangle and animates itself by moving straight across the screen
from left to right at a random rate (i.e., each time move
is called, its x-velocity should be randomly set between 1 and
10). When it reaches the right side, it should stop (much like those
racing games you see at the local carnival).
Modify the createMovers
method of the class
RacerFactory
to create the given number of
Racer
s such that they are all the same size and are
positioned in a line along the left side of the canvas and exactly
fill its height. All Racer
s, no matter how many will be
created, should start at the same center x-coordinate, 0, and have the
same width, 20. The rest of their attributes should be set so that
the rectangles are spread evenly across the edge of the canvas. For
example, given 10 racers to create and a canvas that is 100 pixels
tall, they should each be 10 pixels high and positioned with their
center y-coordinates at 5, 15, 25, 35, 45, 55, 65, 75, 85, and 95,
respectively.
When run, it should appear like the rectangles are racing to the right side of the window.
Extra credit: Make your race cars fancier: add tires, numbers, or cool designs (up to 5 points).
Walker
class such that it draws itself like a
rectangle and animates itself by moving at a constant rate in a random
direction (i.e., each time move
is called, its velocity
should be set to move at a random angle in radians, chosen between 0
and 2 * Math.PI
).To calculate the the x and y distances
travelled in a given direction, you will need to multiply the speed of
the walker by Math.cos(randomAngle)
and
Math.sin(randomAngle)
, respectively. Each
Walker
should have its own speed that is set to a random
value when it is constructed, then remains constant for the life of
the Walker
.
Modify the createMovers
method of the class
WalkerFactory
to create the given number of
Walker
s such that they are all the same size and are
positioned in a line diagonally across the canvas with each touching
the two adjacent rectangles at their corners. Their position and size
should be set so that they are spread evenly across the canvas. For
example, given 10 walkers to create and a canvas whose size is 100x200
pixels, you should create 10 walkers that are 10x20 pixels in size
and centered at (5, 10), (15, 30), (25, 50), (35, 70), (45, 90),
(55, 110), (65, 130), (75, 150), (85, 170), and (95, 190),
respectively.
When run, it should appear like the rectangles taking a random (i.e., drunken) walk. In other words, at each step, your rectangle will choose a direction to move.
See Assignment 4 for instructions on creating screen shots.
If possible, put multiple screen shots on each sheet of paper. Label each screen shot with some context about what's happening in the picture. (You can label screen shots either in pen--if neatly written--or in print.)
Submit a printed version of your assignment--including the screen shots--at the beginning of class on Thursday, July 27.
Email a gzipped tar file of your assignment directory (named
lastname
) to Ke (kli at cis.udel.edu) before next
Thursday (July 27) at 11:59:59 p.m.
Please do not submit your code from earlier assignments. You may need to create a temporary location that contains your submission so that you do not submit code from earlier assignments.
If you have any questions about submission, ask early!
This problem is based on a homework assignment from Robert Duvall.