Lab 5 Post-Lab Assignment: Drawing Random Hats
Due Friday, October 8, 2004
Write a RandomHat class that creates a graphical hat object (a top hat formed
by a rectangle and a line for the brim) with random size, position, and
color. The class should store
the following information as
Your RandomHat class should provide the following
- height of the hat
- width of the hat
- x coord of upper left-hand corner
- y coord of upper left-hand corner
- A parameterless constructor that generates random values for the
height, x, y, and color. Make the height a random value
from 75 up to 150 (but not
including), x from 0 up to 550, y from 0 up to 300. Assign the width of the
hat to be 75% of the height.
To generate the color generate a random value (between 0 and 255, inclusive)
for each color component (amount of red, amount of green, and amount of
blue), then use the Color constructor with three parameters (as we did in
Lab 3) to get a random color. You should declare local variables
inside the constructor to store each of the color components then use
those variables as parameters to the Color constructor.
NOTE: This constructor is different from the constructor in the balloon
class. That constructor had 4 parameters specifying the height, position,
and color of the balloon. In this case the constructor should have no
parameters - initial values of the instance data are generated randomly
rather than being passed in by the client program.
IMPORTANT: Your random number generator must be declared at the class
level (not inside the constructor), and must be declared static. So
its declaration and initialization
should appear with the declarations of height, width, color, x, and y, and
should look like this:
private static Random generator = new Random();
- A draw method that draws the hat at its x,y coordinate in its
color. The hat should be drawn as a rectangle with a line for the brim. The
brim should be 1.5 times the width of the hat (so the amount it sticks out
on each side of the hat is one-quarter the width).
Note that your draw method
will need to take a Graphics object as a parameter.
When your RandomHat class compiles, write an applet DrawHats that
uses your RandomHat class to create and draw 5 hats. Of course,
you also need an html file so you can run the applet. In the html
file make the applet width 600 and height 400.
Be sure your program is appropriately documented.
- Hard copy of your RandomHat class and your DrawHats applet.
- Tar your post lab directory that contains all of your files (.java,
.class, and .html) and email it to your instructor with
cpsc120 post5 in the subject line.