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

/**
 * An ArtSubroutinesPanel draws randomly created artworks of several types.  The user selects
 * the type of artwork by clicking on buttons at the bottom of the panel.
 */
public class ArtSubroutinesPanel extends JPanel implements ActionListener {
	

	private ArtPanel display;  // The display area where the art is actually drawn.
	

	private String artType;    // Specifies which type of art to draw.  (See the createArt() subroutine.)

	
	/**
	 * This subroutine will be called from elsewhere in this class when the art display area
	 * needs to be redrawn.  The widht and height parameters give the actual widht and height
	 * of the drawing area (which should be 500-by-400, but doensn't necessarily have to be,
	 * depending on how this class is actually used).  The "g" parameter is a graphics context
	 * that can be used to do the drawing.
	 * 
	 * Note that the value of the "artType" member variable is a string that tells what type
	 * of art to draw.  Initially, the value of this string is "None" indicating that the user
	 * has not yet pressed any of the buttons.  When the user clicks a button, the value of
	 * the "artType" variable is set to the text displayed on that button.  This createArt()
	 * subroutine will then be called to draw the art.  The button texts are set up in the
	 * constructor to be "Pollock?", "Kandinsky?", and "Mondrian?"
	 */
	private void createArt(Graphics g, int width, int height) {
		g.setColor(Color.LIGHT_GRAY);
		g.fillRect(0,0,width,height);
		g.setColor(Color.RED);
		g.drawString(artType,20,30); // INCOMPLETE -- this just shows the value of the artType variable
	}


	/**
	 * This constructor is called when the panel is first created.  Its job is to set up the
	 * contents of the panel and its initial state.  An ArtSubroutinePanel contains a large
	 * display area, which is actually itself a panel belonging to the nested class named ArtPanel.
	 * Beneath the panel is a row of buttons representing different types of art.  When the user
	 * clicks one of the buttons, a random art work of that type is created.  (More exactly, 
	 * event-handling for the buttons is set up so that when the user clicks a button, the
	 * "Action" event that is generated results in a call to the actionPerformed() subroutine in
	 * this class.  This subroutine, in turn, sets the value of the artType variable and causes
	 * the display area to be repainted.  When the display area is repainted, the createArt()
	 * subroutine will be called.)
	 */
	public ArtSubroutinesPanel() {
		
		artType = "None";  // At the beginning, no type of art has been selected.
		
		display = new ArtPanel(); // ArtPanel is a nested class.  It is defined below.
		display.setPreferredSize(new Dimension(500,400));
		
		JPanel buttonBar;   // A panel to hold the buttons, to be placed at the bottom of the main panel.
		buttonBar = new JPanel();
		buttonBar.setLayout( new GridLayout(1,0) );  // Buttons will be in one row, all with same width.
		
		// Create the buttons, add them to the buttonBar, and set up listening for events.
		// Note that the text displayed on the buttons is "Pollock?", "Kandinsky?", "Mondrian?"
		
		JButton PollockButton, kandinskyButton, mondrianButton;  // The buttons.

		PollockButton = new JButton("Pollock?");
		buttonBar.add(PollockButton);
		PollockButton.addActionListener(this);

		kandinskyButton = new JButton("Kandinsky?");
		buttonBar.add(kandinskyButton);
		kandinskyButton.addActionListener(this);
		
		mondrianButton = new JButton("Mondrian?");
		buttonBar.add(mondrianButton);
		mondrianButton.addActionListener(this);
		
		// Set up the overall layout and appearance of the main panel.
		
		setLayout( new BorderLayout(5,5) );
		add(buttonBar, BorderLayout.SOUTH);
		add(display,BorderLayout.CENTER);
		setBackground(Color.DARK_GRAY);
		buttonBar.setBackground(Color.LIGHT_GRAY);
		setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY,5));
		
	}
	

	/**
	 * This is the method that is called when the user clicks one of the buttons.  It simply stores
	 * the text from the button in the artType member variable and then causes the display to be
	 * repainted.
	 */
	public void actionPerformed(ActionEvent e) {
		artType = e.getActionCommand();  // This is the text from the button.
		display.repaint();  // Tells the sytem to repaint the display.
	}


	/**
	 *  This nested class is used to represent the drawing area where the artworks are displayed.
	 *  It is a plain panel, except that its paintComponent() method calles createArt() to draw
	 *  an artwork in the panel.
	 */
	private class ArtPanel extends JPanel {
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			int width, height;  // The  width and height of this display panel, where the art is drawn.
			width = getWidth();
			height = getHeight();
			createArt(g, width, height);  // draws the random work of art!
		}
	}
	
}
