CompSci 6
Spring 2009
Program Design and Analysis

Screen Savers

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, which, arguably, can be considered an art form.

For each problem below, you will need to update at least two classes: a Mover subclass that animates in the described fashion and a Factory subclass that creates the proper subclass in the proper configuration. In your Mover subclass, complete the paint and update methods to draw and move the shape as specified. In your Factory subclass, complete the createMovers method to create the given number of shapes within the given canvas in a specific pattern as described below. This means you will need to modify at least eight different class files. The current Canvas class will work without modification because it operates on generic Mover objects rather than the specific subclasses you are writing, so it has been moved to the package ignorethis.

Here are some example solutions to these problems.

  1. Smileys
    Modify the paint and update methods of the class BouncingSmiley such that it draws itself like a smiley face and animates itself by bouncing around the screen as if affected by a gravitational force. More details are given in this classwork.

    Modify the createMovers method of the class BouncerFactory to create the given number of BouncingSmileys such that they all start at the center of the canvas, with a random size and color, and launch upwards in a random direction (i.e., the velocity's y-component must be negative, but the x-component can be any value).

  2. Racers in a Line
    Modify the paint and update methods of the class Racer 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. More details are given in this classwork.

    Modify the createMovers method of the class RacerFactory to create the given number of Racers such that they are all the same size and are positioned in a line along the left side of the canvas.

  3. Walkers on a Diagonal
    Modify the paint and update methods of the class Walker 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 update 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 velocity's x and y distances travelled in a given direction, you will need to multiply the speed of the walker by Math.cos(randomAngleInRadians) and Math.sin(randomAngleInRadians), respectively and cast the result to an int value. Each Walker should have its own speed instance variable that is set to a random value when it is constructed, but not modified in the Walker's update method. You may choose the initial color to be anything you like.

    Modify the createMovers method of the class WalkerFactory to create the given number of Walkers 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.

  4. Attractors on a Circle
    Modify the paint and update methods of the class Attractor such that it draws itself like a circle and animates itself by moving toward the center of the canvas (i.e., each time update is called, its velocity should change by one one-hundredth, 0.01, of the distance between the center of the shape and the center of the canvas). In a sense, the shape is attracted to the center as if by gravity; it will not simply move directly to the center and stay there, but speed up as it gets closer to the center and slow down as it gets farther away. You may choose the initial velocity and color to be anything you like.

    Modify the createMovers method of the class AttractorFactory to create the given number of Attractors such that they are all the same size and are positioned in a circular pattern with each touching the one beside it (i.e., tangent to each other) like the marks on a clock (if those marks were circles). In this case, their position and size should be set so that they are spread evenly around the perimeter of a circle whose diameter is the minimum of the width and height of the canvas. Recall that the formula for computing the circumference, or perimeter, of a circle is 2 * Math.PI * radius. To calculate the the x and y position, you will need to multiply the radius of the circle by Math.cos(angleInRadians) and Math.sin(angleInRadians), respectively and cast the result to an int value. For example, given 4 attractors to create and a canvas whose size is 100x200 pixels, you should create 4 attractors that are 100x100 pixels in size and centered at (50, 0), (100, 50), (50, 100), and (0, 50), respectively.

    When run, it should appear like the circles are orbiting the center of the canvas.

  5.  

  6. Your Choice
    Modify the paint and update methods of the classes Student such that it draws itself however you like and animates itself as you wish. You may use a "buggy" animation you created while trying to do one of the other problems. You may try to do a coordinated animation like fireworks, a waterfall, balls bouncing off each other, or a particle system. You may try to do a function like a spirograph. You may try to do something abstract that changes colors and sizes rather than simply moving. In any case, you must document what you tried to do so that it is not simply random chance.

    Modify the createMovers method of the class StudentFactory to create our movers in a pattern that supports your animation.

    In class, we will display the animations and may vote on the best ones using a variety of criteria.

  7. Leaving a Trail (optional extra credit)
    Change the super class, Mover, such that it paints itself as a collection of connected line segments and animates itself by adding the current center of the shape to a collection of points (i.e., each time update is called, the center point is added to a collection). When drawn as a series of lines from the first point to the second point in the collection, from the second point to the third point, from the third point to the fourth point, and on and on until the last point in the collection, it will appear that the shape is leaving a trail behind marking where it has moved. Additionally, the Mover class will need to declare and initialize an instance variable that is a collection,i.e., ArrayList, of all the past points.

    Since this code is in the superclass, it is inherited by all the sub-classes and it will mark a trail for any of the shapes written above without having to know the exact type of sub-class or movement being made.

Submitting Your Work

When you are satisfied you have completed the problems above, you should electronically submit your project through Eclipse. A submission is not considered complete unless it includes all the Java code for the project (both what you have written and the code provided when you downloaded the project) and a README file as described here.