/*
 * Example code for CS211, Fall 2000.
 *
 * Demonstrates various capabilities of the Java/Swing GUI.
 *
 * Each of the classes below has its own main program that must be
 * run in order to demonstrate that class.
 */
 
import javax.swing.*;
import java.awt.Color;
import java.awt.event.*;
import java.awt.GridLayout;
import java.awt.BorderLayout;


/**
 * Example: a column of buttons.
 */
class GUITest {

public static void main (String[ ] args) {
	JFrame frame = new JFrame();
	JPanel panel = new JPanel();
	panel.setLayout(new GridLayout(0,1));
	panel.add(new JButton("One"));
	panel.add(new JButton("Two"));
	panel.add(new JButton("Three"));
	panel.add(new JButton("Four"));
	panel.add(new JButton("Five"));
	frame.getContentPane().add(panel,BorderLayout.WEST);
	frame.setSize(250,300);
	frame.setVisible(true);
	}
}


/**
 * Example: color buttons.  Each button click changes the color.
 * Uses an ActionListener.
 */
class ColorWindow extends JFrame {

static String[ ] name = {"red","blue","green","magenta","cyan","yellow"};
static Color[ ] color = {Color.red,Color.blue,Color.green,Color.magenta,Color.cyan,Color.yellow};

public ColorWindow () {
	JPanel panel = new JPanel();
	for (int i = 0; i < name.length; i++) {
		JButton button = new JButton(name[i]);
		panel.add(button);
		button.addActionListener(new MyListener(panel,color[i]));
		}
	this.getContentPane().add(panel);
	}
	
public static void main (String[] args) {
	JFrame frame = new ColorWindow();
	frame.pack();	// Makes frame fit tight around its contents
	frame.setVisible(true);
	}

static class MyListener implements ActionListener {
	Color myColor;
	JPanel myPanel;
	
	public MyListener (JPanel panel, Color color) {
		myColor = color;
		myPanel = panel;
		}

	public void actionPerformed (ActionEvent event) {
		myPanel.setBackground(myColor);
		}
	}
}


/**
 * Example: counts button clicks.
 * Uses an ActionListener that is an anonymous inner class.
 */
class CountWindow extends JFrame {

private final String labelPrefix = "Number of clicks: ";
private JLabel label;
private int count = 0;

public CountWindow () {
	JPanel panel = new JPanel();
	panel.setLayout(new GridLayout(0,1));
	JButton button = new JButton("Click here!");
	panel.add(button);
	panel.add(label = new JLabel(labelPrefix + count));
	button.addActionListener(
			new ActionListener() {
				public void actionPerformed (ActionEvent e) {
					label.setText(labelPrefix + (++count));
					}
				});
	this.getContentPane().add(panel);
	}
	
public static void main (String[] args) {
	JFrame frame = new CountWindow();
	frame.setSize(150,100);
	frame.setVisible(true);
	}
}


/**
 * Example: Does random (but bright) colors when window is clicked.
 * Uses a MouseAdapter to build a MouseListener.
 * Uses a WindowAdapter to build a WindowListener.
 */
class CloseableWindow extends JFrame {

private JPanel panel;

public CloseableWindow () {
	panel = new JPanel();
	this.getContentPane().add(panel);
	this.addMouseListener(new MouseAdapter() {
		public void mouseClicked (MouseEvent e) {
			panel.setBackground(Color.getHSBColor((float)Math.random(),1,1));
		}
	});
	this.addWindowListener(new WindowAdapter() {
		public void windowClosing(WindowEvent e) {
			System.exit(0);
		}
	});
}
	
public static void main (String[ ] args) {
	JFrame frame = new CloseableWindow();
	frame.setSize(150,150);
	frame.setVisible(true);
	}
}

    
				