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


/**
 * The RandomArt1 class is a combined applet/application that shows random
 * art.  The art changes every five seconds.  This is an applet class, so it
 * can be used for an applet on a web page.  The class contains a main program
 * and so can be run as an application -- the main program simply creates a
 * window that shows the applet.
 * <p>Note that when this file is compiled, it produces TWO class files,
 * RandomArt1.class and RandomArt1$1.class.  Both of these class files are
 * necessary to run the applet or application.  For an applet, both class
 * files must be in the same diretory that contains the HTML file that shows
 * the applet.
 */
public class RandomArt1 extends JApplet {
	

	/**
	 * This paint method is called whenever the applet needs to be redrawn.
	 * Later in this class, a "timer" is set up that will cause the applet
	 * to be redrawn every five second.  Since this method uses random numbers,
	 * each time the paint method is called,the picture will be different.
	 * Note that this method is also called when a part of the applet that
	 * was covered up is uncovered; in that case, only the part that was
	 * covered is actually drawn.
	 */
	public void paint(Graphics g) {

		int width, height;    // The actual width and height of the drawing area.
		width = getWidth();
		height = getHeight();

		g.setColor(Color.BLACK);
		g.fillRect(0,0,width,height);    // Fill the entire drawing area with black.

		int x1, y1;  // One endpoint of the line.
		int x2, y2;  // The other endpoint of the line.
		x1 = (int)(width*Math.random());  // x coords are random numbers between 0 and width-1
		x2 = (int)(width*Math.random());
		y1 = (int)(height*Math.random()); // y coords are random numbers between 0 and height-1
		y2 = (int)(height*Math.random());
		g.setColor(randomColor(1,1));  // Use a random bright, saturated color.
		g.drawLine(x1,y1,x2,y2);

	}
	
	/**
	 * Creates a color with a randomly selected hue and with a specified satruation
	 * and brightness.  The satruation of a color is a number between 0 and 1 that 
	 * specifies how "pure" the color is.   A saturation of 1 gives a pure color; 
	 * a satuation of 0 gives a grayscale "color"  that is not really a color at all.  
	 * The brightness is a number between 0 and 1 that says how bright the color is.
	 * For "spectral" colors that are as bright and saturated as possible, use 
	 * brightness = saturation = 1.
	 * @param saturation the saturation of the color.  This must be in the range 0.0 to 1.0;
	 * if not, the value will be truncated to fit in this range.
	 * @param brightness the brightness of the color.  This must be in the range 0.0 to 1.0;
	 * if not, the value will be truncated to fit in this range.
	 * @return the random color
	 */
	public Color randomColor(double saturation, double brightness) {
		float hue = (float)Math.random();
		if (saturation > 1)
			saturation = 1;
		else if (saturation < 0)
			saturation = 0;
		if (brightness > 1)
			brightness = 1;
		else if (brightness < 0)
			brightness = 0;
		return Color.getHSBColor(hue, (float)saturation, (float)brightness);
	}


	/**
	 * This "constructor" is called by the saystem when the applet is created.
	 * Its job in this class is to set up a "timer" that will cause the applet
	 * to be redrawn every five seconds.
	 */
	public RandomArt1() {
		Timer timer = new Timer(5000, new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				repaint();
			}
		});
		timer.setInitialDelay(5000);
		timer.start();
	}
	
	/**
	 * Specifies the size that this applet would like to be.  This size is
	 * respected by the main program, below, but when the applet is shown
	 * on a web page, the size can be anything.
	 */
	public Dimension getPreferredSize() {
		return new Dimension(500,500);
	}
	
	/**
	 * This main routine allows this class to be run as an application.
	 * When the application is run, it simply shows a window that contains
	 * the applet that is defined by this class.
	 * @param args
	 */
	public static void main(String[] args) {
		RandomArt1 applet = new RandomArt1();
		JFrame frame = new JFrame("Is it Art?");
		frame.setContentPane(applet);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setResizable(false);
		frame.setLocation(100,50);
		applet.init();
		frame.setVisible(true);
		frame.show();
		applet.start();
	}


}

