Example 10
Sunshine
Create a new application to draw a sun and the rays it emits. Rays should be drawn and erased repeatedly to make the illusion of the sun that shines.
DESIGN and ANALYSIS: SunShine class should be a Java applet that implements the Runnable interface in order to use a Thread in order to allow the applet to be redrawn while the thread creates a delay during the creation of the sun's "rays". Use an array of colors and randomly select a color each time the screen is updated. Put the code to draw the sun and its rays in the update method and have the paint method call the update method as the only action in the paint method. In the run method, use modulo division so a class-wide variable holds the number of rays to draw each time the update method runs a for loop. This number is incremented each time the thread wakes from sleep, and is modulo divided by a constant which equals the number of rays you wish to draw the last time through the draw loop.