/** * Class SunShine draws a sun, and the rays it emits. * SunShine implements the Runnable interface in order * to use a thread to delay time between each new ray * being drawn. Using a thread allows the applet to * redraw the screen while waiting for the delay time * to pass. * * @author Dr. Pevac, modified by David Thibodeau * @version 30-July-2002 */ import java.applet.Applet; import java.awt.*; import java.util.Random; public class SunShine extends Applet implements Runnable { int frame; int delay = 20; // # of msecs between each paint method private final static int NUMRAYS = 60; // Number of sunshine rays to emit Thread animator; // Thread object to control delay /** * start method - This method automatically called * when applet becomes visible on the screen. This * method creates and starts the Thread object. * @param void - no data required * @return void - no data returned */ public void start() { animator = new Thread(this); animator.start(); // create & start new Thread object } /** * run method - This method called by the animator Thread * object. * @param void - no data required * @return void - no data returned */ public void run() { long tm = System.currentTimeMillis(); // get current time while (Thread.currentThread() == animator) { repaint(); // show next frame of animation // Delay the next frame for the proper amount of time try { tm += delay; Thread.sleep(Math.max(0, tm - System.currentTimeMillis() ) ); } catch (InterruptedException e) { break; } frame++; frame = frame % (NUMRAYS + 1); // mod math to create 60 rays } // end while } // end run method /** * stop method - This method called when the applet is no * longer visible. The method sets animator to null so the * Thread will exit before displaying the next frame. * @param void - no data required * @return void - no data returned */ public void stop() { animator = null; } public void paint(Graphics g) { update(g); } public void update(Graphics g) { double pi, theta, xx, yy; int x, y,x1,y1; Color color[] = new Color[11]; color[0] = Color.black; color[1] = Color.white; color[2] = Color.red; color[3] = Color.blue; color[4] = Color.green; color[5] = Color.cyan; color[6] = Color.magenta; color[7] = Color.orange; color[8] = Color.pink; color[9] = Color.lightGray; color[10]= Color.darkGray; int colorNumber = 0; pi = 3.14159; theta=0; setBackground(Color.black); g.setColor(Color.yellow); g.fillOval(100,90,100,100); g.setXORMode(Color.yellow); // These 3 lines randomly select and set // the paint color for the sunshine rays Random number = new Random(); colorNumber = Math.abs(number.nextInt() ) % 11; g.setColor( color[colorNumber] ); for ( int i = 0; i < frame; i++) { theta = i * (2 * pi / NUMRAYS ); xx=120*Math.cos(theta); // 120 = DIST FROM CENTER yy=120*Math.sin(theta); // OF 'SUN' x=(int)xx+150; y=(int)yy+140; x1=(int)(xx*50/120)+150; y1=(int)(yy*50/120)+140; g.drawLine(x1,y1,x,y); g.drawLine(x1+1,y1+1,x+1,y+1); } // for } // update }