import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SquareExercise {
	
	// these must be declared here to be seen in any Action classes
	Graphics2D thePen;
	JTextField lengthField;
	JPanel drawPanel;
	int width;
	int height;
	
	public SquareExercise() {
		
		// The (window) frame
		JFrame frame = new JFrame("Generic Frame");
		setUpFrame(frame);
		
		// The main panel
		JPanel thePanel = new JPanel();
		width = 500;
		height = 300;
		thePanel.setPreferredSize(new Dimension(width,height));
		thePanel.setBackground(Color.YELLOW);
		
		// Other componenets are added to the panel here
		
		// the input label and input field will be added to an input panel
		JPanel inputPanel = new JPanel();
		inputPanel.setBackground(Color.YELLOW);
		
		JLabel lengthLabel = new JLabel("Enter the size of the square");
		lengthField = new JTextField(10);
		lengthField.addActionListener(new LengthFieldAction() );
		inputPanel.add(lengthLabel);
		inputPanel.add(lengthField);		
		thePanel.add(inputPanel);
		
		drawPanel = new JPanel();
		drawPanel.setPreferredSize(new Dimension(width,height-50));
		drawPanel.setBackground(Color.BLUE);
		thePanel.add(drawPanel);
		
		// To see the panel it must be "added" to the frame
		// and set visible
		frame.getContentPane().add(thePanel);
		frame.pack();
		frame.setVisible(true);
		
		// This line gets the "pen" (Graphics2D object) to draw 
		// on the draw panel. It must be 'gotten" after the frame
		// is set visible
		thePen = (Graphics2D)drawPanel.getGraphics();		
		thePen.setFont(new Font("monospaced",Font.BOLD,20));
		
	} // end constructor
	
	class LengthFieldAction implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			// these are the statments that are executed when aButton is clicked
			String input = lengthField.getText();
			int length = Integer.parseInt(input);
			lengthField.setText("");
			for( int i = 0; i < 30; i++) {
				try{Thread.sleep(100);}catch(Exception exp){};
				drawSquare(thePen,20*i,10*i,length);
			}
			// clear the length field
		} // end actionPerformed
	} // end ButtonAction
	
	
	void drawSquare(Graphics2D thePen, int x, int y, int length) {
		thePen.setColor(Color.BLUE);
		thePen.fill(new Rectangle(0,0,drawPanel.getWidth(),drawPanel.getHeight()));
		thePen.setColor(Color.RED);
		thePen.fill(new Rectangle(x,y,length,length));
	} // end drawRectangle
	
	void setUpFrame(JFrame aFrame) {
		aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		int xPosition = 200;
		int yPosition = 200;
		aFrame.setLocation(xPosition,yPosition);
	} // end setUpFrame
	
	public static void main(String[] args) {
		new SquareExercise();		
	} // end main
	
} // end SquareExercise
