CompSci 6- Classwork 12 - March 2, 2010
10 pts


NOTE: THIS CLASSWORK CAN BE TURNED IN BY MARCH 16.

Today's classwork focuses on using graphics, inheritance, and lists to draw a number of random colored circles, going different directions and speeds, bouncing around within the canvas. This basic functionality is similar to what is required for basic simulation of molecular dynamics.

Snarf the 12-moreAnimation-cps006-spring10 to get started. You can see the code here.

  1. Currently there are two balls of the same size. Add a size attribute to the BouncingBall object of type Dimension. Demonstrate how you can create and display two BouncingBalls of different sizes.
  2. Increase the number of bouncing balls you can display.
    1. Modify the Canvas class to use a collection (i.e., an ArrayList) to hold the BouncingBall objects it displays and updates.
    2. Verify that you can display 10 or more RED bouncing balls of the same size. What happens if they all have the same initial position and velocity? Include your answer in your README.

    3. Create a new Canvas method, addBouncers,that add the given number of BouncingBall objects to the object's ArrayList of BouncingBalls with the following additional random attributes:
      • size: both the height and width should be the same random value between 16 and 48, inclusive (i.e., the shape should remain a circle)
      • center: the x-coordinate should be a random value between the circle's radius and width of the window minus the circle's radius; the y-coordinate should be a random value between the circle's radius and height of the window minus the circle's radius (remember the radius is half the randomly generated size)
      • velocity: the x- and y-components of the velocity should never be more than one-tenth the size of the canvas, but they should vary between both positive and negative values. Finally, how can you guarantee that every BouncingBall created moves, i.e., does not have a velocity of (0, 0)? Include your answer in your README.

  3. The origin of the bright yellow smiley face is not completely clear. The earliest known examples of the graphic are attributed to Harvey Ball, who devised the face in 1963 for a Worcester, Massachusetts, USA-based insurance firm, State Mutual Life Assurance. Ball never attempted to use, promote or trademark the image and it fell into the public domain in the United States before that could be accomplished. It rose to popularily in the seventies and was recently given the rare honor of appearing on a postage stamp, becoming the spokesperson for Wal-mart, being one of the first structures modeled with DNA, and, of course, a representation of our emotions during text-based conversations as emoticons.

    This problem asks you to create a class that represents a bouncing smiley face. Since that is similar to a bouncing ball, we will create a BouncingSmiley class that extends BouncingBall. Get that working first for a different color, and then add the face.

    1. Create a class BouncingSmileythat represents a smiley face that can bounce off the walls like a BouncingBall. Your BouncingSmiley and BouncingBall classes likely share a lot of code. Make BouncingSmiley a subclass of BouncingBall, so that they share some state and velocity and color.

    2. For now create a BouncingSmiley paint method that just calls the BouncingBall paint method. We will add the eyes and mouth later.

    3. Verify your code by adding code to the Canvas constructor so that it creates two BouncingSmiley objects of different positions, sizes and colors in addition to the original BouncingBall objects, so you should have two bouncing balls and two bouncing smileys.

    4. Now put the face on the smiley by modifying the paint method in BouncingSmiley to draw the eyes and mouth on.

      Your smiley face should have at least a head, two eyes, and mouth that are correctly positioned and sized proportionally to the canter and size given when the class is created. Note that you will need to change the pen color before drawing the eyes and mouth. Feel free to be creative about adding extra features to your smiley face (e.g., a hat, nose, teeth, headband, etc.).

      Note: to draw a shape, you specify the top-left point in the bounding box, the height and width.

    5. Create a new Canvas method, addSmileys,that adds the given number of BouncingSmileys with random initial position and velocity to the list of BouncingBalls.

Submit

Create a README file with your name, any persons you received help from, resources used, and answers to questions.

Submit via Eclipse your Java code and README file to the ClassworkMar2 folder.