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

/**
 * This panel represents a panel (i.e. a rectangular area on the screen) where
 * random art is displayed.  The drawArt method draws a random art work.  This
 * method is called every 5 seconds, and a new piece of random art is generated
 * each time it is called.
 */
public class RandomArtPanel extends JPanel implements ActionListener {
	
	/**
	 * This method draws one random artwork.  The size of the drawing area is 
	 * supposed to be 500-by-400 pixels (but this can also be changed in the
	 * next subroutine).  If you want to know the size, you can call the
	 * methods getWidth() and getHeight(), which return int values giving
	 * the actual width and height of the panel, measuered in pixels.
	 * You can use the functions randomColor() and randomHue(), defined below,
	 * to select random colors for drawing, as in "g.setColor(randomHue(1.0, 0.5))".
	 *    For drawing, you can use the subroutines g.setColor(), g.drawString(),
	 * g.drawLine(), g.drawRect(), g.fillRect(), g.drawOval(), and g.fillOval(),
	 * g.draw3DRect(), g.fill3DRect(), g.drawRoundRect(), and g.fillRoundRect().
	 * These subroutines are part of the object g of type Graphics, which is a 
	 * parameter to this subroutine. 
	 */
	private void createArt(Graphics g) {
		
		int width, height;   // Actual widht and height of the panel.
		width = getWidth();
		height = getHeight();

		if (Math.random() < 0.5) {
			g.setColor( Color.BLACK );  // fill with black
			g.fillRect(0,0,width,height);
			int x1,y1,x2,y2;  // coordinates of endpoints of line, to be selected randomly
			x1 = (int)(width * Math.random());
			y1 = (int)(height * Math.random());
			x2 = (int)(width * Math.random());
			y2 = (int)(height * Math.random());
			g.setColor( randomHue(1,1) );  // Select a random saturated, bright color
			g.drawLine(x1,y1,x2,y2);
			x1 = (int)(width * Math.random());
			y1 = (int)(height * Math.random());
			x2 = (int)(width * Math.random());
			y2 = (int)(height * Math.random());
			g.setColor( randomHue(1,1) );  // Select a random saturated, bright color
			g.drawLine(x1,y1,x2,y2);
		}
		else {
			g.setColor( randomHue(1.0,0.4) );  // fill with a random color, not very bright
			g.fillRect(0,0,width,height);
			g.setColor( randomColor() );  // Random color
			g.fill3DRect(10,10, width/2 - 20, height/2 - 20, true);
			g.fill3DRect(width/2 + 10, height/2 + 10, width/2 - 20, height/2 - 20, true);
			g.setColor( randomColor() );
			g.fillRoundRect(width/2 + 10, 10, width/2 - 20, height/2-20, 32, 32);
			g.fillRoundRect(10, height/2 + 10, width/2 - 20, height/2 - 20, 32, 32);
		}
		
	}
	
	/**
	 * When this subroutine is called, the return value is a random color,
	 * constructed with randomly selected amounts of red, green, and blue.
	 */
	private Color randomColor() {
		int r,g,b;  // Red, blue, and green components of color.
		r = (int)(256*Math.random());
		g = (int)(256*Math.random());
		b = (int)(256*Math.random());
		return new Color(r,g,b);
	}
	
	/**
	 * The return value of this subroutine is a color that has a random
	 * "hue" and has a saturation and a brightness that are specified as
	 * parameters to the subroutine.  A color's hue is its basic spectral
	 * color.  The saturation is a value between 0 and 1 that indicates
	 * how "pure" the color is; 1 gives a pure color while smaller values
	 * are equivalent to mixing some white into the color.  For example,
	 * a basic red color with a saturation of 0.5 would be pink.  The
	 * brightness is a value between 0 and 1, where 1 gives a color that
	 * is as bright as possible and values close to 0 will give a color
	 * that is almost black.  A brightness value of 0 will give black,
	 * no matter what the value of hue or saturation.
	 */
	private Color randomHue(double saturation, double brightness) {
		double hue = Math.random();
		return Color.getHSBColor((float)hue, (float)saturation, (float)brightness);
	}
	
	/**
	 * This is the "constructor" that is called to create an object of type
	 * BasicAnimationPanel.  It sets up all the initial properties of the object.
	 * You can change some of the statements here, as noted, to modifiy the
	 * behavior of the animation.  Note in particular setPreferredSize,
	 * setBackground, and frameCount.
	 *
	 */
	public RandomArtPanel() {
		setPreferredSize( new Dimension(500,400) );  // The size should be 50o-by-500.
		setBackground(Color.LIGHT_GRAY);   // Background color for the window.
		setFont(new Font("Serif",Font.PLAIN,12));  // Initial font size is 12.
		Timer timer = new Timer(5000,this);  // 5000 milliseconds (5 seconds) between artworks
		timer.setInitialDelay(1000);         // one second before first artwork appears
		timer.start();
	}
	
	
	//---- implementation details -- no need to read or understand past this point ----
	
	private boolean timerFired = false;  // Used to prevent drawing any art before timer fires.
	
	public void actionPerformed(ActionEvent evt) {
		timerFired = true;
		repaint();
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		if (timerFired)
			createArt(g);
	}
	
	
} // end BasicAnimation
