//package drawpad;

/**
 * <p>Title: Draw Pad</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author Sridhar Narayan
 * @version 1.0
 */
 import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DrawPad extends JFrame implements ActionListener, MouseListener {
  JPanel topPanel,bottomPanel;
  Graphics bottomPanelGraphics;

  boolean draw = false;
  public DrawPad()  {
    setTitle("Draw Pad");
    setSize(300,400);

    Container cp = getContentPane();

    cp.setLayout(new GridLayout(2,1));
    topPanel = new JPanel();
    topPanel.setLayout(new FlowLayout());

    JButton b = new JButton("Exit");
    b.addActionListener(this);
    JButton b1 = new JButton("Draw");
    b1.addActionListener(this);

    JLabel l1 = new JLabel("Width");
    JLabel l2 = new JLabel("Height");

    JTextField t1 = new JTextField("0", 4);
    JTextField t2 = new JTextField("0", 4);
    topPanel.add(l1);
    topPanel.add(t1);
    topPanel.add(l2);
    topPanel.add(t2);

    topPanel.add(b1);
    topPanel.add(b);

    bottomPanel = new JPanel();
    bottomPanel.addMouseListener(this);
    bottomPanel.setBackground(Color.blue);
    bottomPanel.setForeground(Color.red);
    bottomPanel.setSize(100,100);

    cp.add(topPanel);
    cp.add(bottomPanel);

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
  }
  public void mouseClicked(MouseEvent e) {
    Graphics g = bottomPanel.getGraphics();

    if ( draw )  { // draw is true
      g.drawRect(e.getX(), e.getY(), 20, 20);
      draw = false;
    }

    g.dispose();
    //repaint();
  }
  public void mousePressed(MouseEvent e) {

  }
  public void mouseReleased(MouseEvent e) {

  }
  public void mouseEntered(MouseEvent e) {

  }
  public void mouseExited(MouseEvent e) {

  }
  public void actionPerformed(ActionEvent e) {
    String label = e.getActionCommand();
    if (label.equals("Exit"))
      System.exit(0);
    else if (label.equals("Draw"))
      draw =true;

  }

  public static void main(String[] args) {
    new DrawPad();
  }
}
