import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * Assignment - Typewriter
 * @author Amanda Gorski
 * @date July 23, 2002
 * Simulates a typewriter
 * Shows how to use an array of buttons
 */


public class Typewriter extends JPanel implements ActionListener
{
    String words = "";
    JButton alpha [] = new JButton [26];
    JLabel word;
    JButton clear, space;


    public Typewriter ()
    {
	super (new BorderLayout ());
	setBackground (Color.white);
	//sets up the output label
	word = new JLabel ("                                                   ");
	word.setFont (new Font ("Courier", Font.BOLD, 18));
	word.setForeground (Color.blue);

	//sets up the buttons
	clear = new JButton ("Clear");
	clear.setActionCommand ("clear");
	clear.addActionListener (this);
	space = new JButton ("Space");
	space.setActionCommand (" ");
	space.addActionListener (this);

	//the alphabet set up
	JPanel alp = new JPanel (new FlowLayout ());
	JPanel alp2 = new JPanel (new FlowLayout ());
	JPanel alpwrap = new JPanel (new GridLayout (2, 1));
	//Create the alphabet
	for (int i = 0 ; i < 26 ; i++)
	{
	    char a [] = new char [1];
	    a [0] = (char) (i + 97);
	    String x = new String (a);
	    alpha [i] = new JButton (x);
	    alpha [i].setActionCommand (x);
	    alpha [i].addActionListener (this);
	    if (i < 14)
		alp.add (alpha [i]);
	    else
		alp2.add (alpha [i]);
	}
	alp2.add (space);
	alp2.add (clear);
	alp.setBackground (Color.white);
	alp2.setBackground (Color.white);
	alpwrap.add (alp);
	alpwrap.add (alp2);

	//make the layout
	add (word, "North");
	add (alpwrap, "South");

	//set up a space around the outside of the screen
	setBorder (BorderFactory.createEmptyBorder (20, 20, 20, 20));

    }


    /** Listens to the radio buttons and buttons.
    */
    public void actionPerformed (ActionEvent e)
    {

	if (e.getActionCommand ().equals ("clear"))
	{ //user chooses clear - reset display and holder variable
	    words = "";
	    word.setText (words);

	}
	else
	{ //user clicked on a button
	    //find the letter
	    String x = e.getActionCommand ();
	    //add it to the holder variable
	    words += x;
	    word.setText (words);

	}

    }


    /**
    Directly from: http://java.sun.com/docs/books/tutorial/uiswing/components/example-1dot4/index.html#RadioButtonDemo
     */
    public static void main (String [] args)
    {
	//Make sure we have nice window decorations.
	JFrame.setDefaultLookAndFeelDecorated (true);

	//Create and set up the window.
	JFrame frame = new JFrame ("Typewriter");
	frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

	//Create and set up the content pane.
	JComponent newContentPane = new Typewriter ();
	newContentPane.setOpaque (true); //content panes must be opaque
	frame.setContentPane (newContentPane);

	//Display the window.
	frame.pack ();
	frame.setVisible (true);
    }
}



