CSCI 161:
Introduction to Programming I
Fall 2007
Lab 9: Rainbow (Loops)
Due Thursday,
April 19th at beginning of lab period
Implement a program that draws a rainbow consisting of a
user-specified number of arcs. Tightly space the concentric arcs, drawing each
in a random color.
You will need two files for this project: RainbowPanel and
RainbowDriver. RainbowPanel should have
a constructor that takes in the number of arcs from RainbowDriver.
Using the console, prompt the user for the number of arcs.
Allow only integers in the range [1, 10] (inclusive). Alert the user if the
number is invalid, and require him to re-specify until a valid value is
entered.
Generate a rainbow within a panel in a JFrame. Make each arc
width and height constant (experiment with values between 20 and 40
pixels). The bottoms of all of the arcs
should be even. You can create a Color object as follows:
Color c = new Color (r, g, b);
where "r", "g", and "b" are integers in the range [0, 255] and represent the amount of red, green, and blue light, respectively, that the color reflects. For example, a color triple of (0, 0, 0) represents black (no red, green, or blue), while a triple of (255, 255, 0) represents yellow (full red and green, no blue).
public void paintComponent
(Graphics page)
page.fillArc (...); see p. 98
Draw this on
paper before you start coding.
Remember that fillArc takes the upper left
corner of the arc's bounding box. Adjust that corner by repeatedly incrementing
the x and y components (define constants such as XDELTA and YDELTA, which
represent the change in the horizontal and vertical position). The width of the
bounding box also needs to change.
See listings 5.15 and 5.16 in the text.