import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


/**
 *  This program opens a window on the screen that shows a different
 *  random "artwork" every five seconds.
 *
 *  The animation is driven by a "Timer" that "ticks" every 5000 milliseconds
 *  (that is, every five seconds).  Each time the timer ticks, the window is 
 *  repainted, and the paintComponent() method is called.  The paintComponent()
 *  method draws the random art.
 */
public class RandomArt extends JPanel implements ActionListener {

   /**
    *  The main routine creates the window and shows it on the screen.  It also
    *  sets up the timer that will drive the animation.
    */
   public static void main(String[] args) {
      JFrame window = new JFrame("Art!");
      RandomArt canvas = new RandomArt();
      canvas.setPreferredSize(new Dimension(500,400)); // Sets size of the display area.
      window.setContentPane(canvas);
      window.pack();
      window.setLocation(200,100);
      window.setResizable(false);
      window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      window.setVisible(true);
      Timer timer = new Timer(5000, canvas);  // Create the timer that will drive the animation.
                                              //   (The "5000" is the number of milliseconds between ticks.)
      timer.start(); // Starts the timer running.
   }
   
   
   /**
    *  This method is called by the system when the timer "ticks".
    *  it simply causes the display to be repainted.
    */
   public void actionPerformed(ActionEvent evt) {
      repaint();
   }
   
   /**
    *  This function creates a color with random amounts of red, green, and blue.
    */
   private Color randomColor() {
      int r, g, b;  // Amounts of red, green, and blue (which must be in the range 0 to 255).
      Color color;  // The random color.
      r = (int)(256*Math.random());
      g = (int)(256*Math.random());
      b = (int)(256*Math.random());
      color = new Color(r,g,b);
      return color;
   }
   
   /**
    *  Creates an "HSB" color, defined in terms of hue, saturation, and brightness.
    *  The hue is selected at random.  The saturation and brightness are given as
    *  parameters.  Note that the saturation and brightness must be in the range
    *  0.0 to 1.0.
    */
   private Color randomHue(double saturation, double brightness) {
       double hue;   // The randomly selected hue.
       Color color;  // The random color.
       hue = Math.random();
       color = Color.getHSBColor( (float)hue, (float)saturation, (float)brightness );
       return color;
   }
   
   /**
    *  This is the method that draws the display.  It will be called over and over
    *  as long as the window is open.  Its job is to draw a random work of "art".
    */
   protected void paintComponent(Graphics g) {
   
       // Fill the entire drawing area with a random-colored background.
       
       g.setColor( randomColor() );
       g.fillRect(0,0,500,400);
   
       // Draw four colored bars across the display.  The first bar is brightly
       // colored, and the brightness decreases from on bar to the next.
       
       g.setColor( randomHue(1,1) );
       g.fillRect(15,15,470,70);
       
       g.setColor( randomHue(1,0.75) );
       g.fillRect(15,115,470,70);
       
       g.setColor( randomHue(1,0.5) );
       g.fillRect(15,215,470,70);
       
       g.setColor( randomHue(1,0.25) );
       g.fillRect(15,315,470,70);

   }
   
}
