/* illustrate the use of events and event listeners */
/* by Neng-Fa Zhou 2003 */
import java.awt.*;
import java.awt.event.*;

public class TestEvent {
    public static void main(String[] args){
	Frame f = new TestEventFrame();
	f.setSize(500,500);
	f.show();
    }
}

class TestEventFrame extends Frame 
    implements KeyListener
{
    List list_2; /* generated automatically from a B-Prolog program */
    Choice choice_3;
    Checkbox checkbx_4;
    Checkbox checkbx_5;
    Checkbox checkbx_6;
    TextField tf_7;
    Button bu_8;
    Button bu_9;
    Button bu_10;
    Button bu_11;
    Color color = Color.yellow;
    int last_x,last_y;

    Menu m;
    MenuItem m_yellow, m_blue, m_red;

    public TestEventFrame(){
	super("Test Event Frame");
	setLayout(null);
	
	MenuBar mb = new MenuBar();
	setMenuBar(mb);

	m = new Menu("Color");
	mb.add(m);

	m_yellow = new MenuItem("Yellow");
	m_blue = new MenuItem("Blue");
	m_red = new MenuItem("Red");	
	m.add(m_yellow); m.add(m_blue); m.add(m_red);
	
	m_yellow.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e){
		    color = Color.yellow;
		}
	    });

	m_blue.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e){
		    color = Color.blue;
		}
	    });

	m_red.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e){
		    color = Color.red;
		}
	    });


	list_2 = new List();
	list_2.add("Yellow");
	list_2.add("Blue");
	list_2.add("Red");
	add(list_2);
	list_2.setSize(65,20);
	list_2.setLocation(0,60);
	list_2.addItemListener(new ItemListener(){
		public void itemStateChanged(ItemEvent e){
		    Integer item = (Integer)e.getItem();
		    switch (item.intValue()){
		    case 0: color = Color.yellow; break;
		    case 1: color = Color.blue; break;
		    case 2: color = Color.red; break;
		    }
		}
	    });

	choice_3 = new Choice();
	add(choice_3);
	choice_3.add("Yellow");
	choice_3.add("Blue");
	choice_3.add("Red");
	choice_3.setSize(65,20);
	choice_3.setLocation(0,100);
	choice_3.addItemListener(new ItemListener(){
		public void itemStateChanged(ItemEvent e){
		    int itemIndex = choice_3.getSelectedIndex();
		    System.out.println("item selected"+itemIndex);
		    switch (itemIndex){
		    case 0: color = Color.yellow; break;
		    case 1: color = Color.blue; break;
		    case 2: color = Color.red; break;
		    }
		}
	    });

	bu_9 = new Button("Yellow");
	add(bu_9);
	bu_9.setSize(65,15);
	bu_9.setLocation(0,140);
	bu_9.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e){
		    color = Color.yellow;
		}
	    });

	bu_10 = new Button("Blue");
	add(bu_10);
	bu_10.setSize(65,15);
	bu_10.setLocation(0,175);
	bu_10.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e){
		    color = Color.blue;
		}
	    });

	bu_11 = new Button("Red");
	add(bu_11);
	bu_11.setSize(65,15);
	bu_11.setLocation(0,210);
	bu_11.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e){
		    color = Color.red;
		}
	    });
	

	CheckboxGroup cbg = new CheckboxGroup();
	checkbx_4 = new Checkbox("Yellow",cbg,false);
	add(checkbx_4);
	checkbx_4.setSize(65,15);
	checkbx_4.setLocation(0,245);
	checkbx_4.addItemListener(new ItemListener(){
		public void itemStateChanged(ItemEvent e){
		    if (checkbx_4.getState()){
			color = Color.yellow; 
		    }
		}
	    });
 

	checkbx_5 = new Checkbox("Blue",cbg,false);
	add(checkbx_5);
	checkbx_5.setSize(65,15);
	checkbx_5.setLocation(0,280);
	checkbx_5.addItemListener(new ItemListener(){
		public void itemStateChanged(ItemEvent e){
		    if (checkbx_5.getState()){
			color = Color.blue;
		    }
		}
	    });

	checkbx_6 = new Checkbox("Red",cbg,false);
	add(checkbx_6);
	checkbx_6.setSize(65,15);
	checkbx_6.setLocation(0,315);
	checkbx_6.addItemListener(new ItemListener(){
		public void itemStateChanged(ItemEvent e){
		    if (checkbx_6.getState()){
			color = Color.red; 
		    }
		}
	    });


	tf_7 = new TextField("",1);
	add(tf_7);
	tf_7.setSize(65,20);
	tf_7.setLocation(0,350);
	tf_7.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e){
		    String cont = tf_7.getText();
		    if (cont.equalsIgnoreCase("yellow")){
			color = Color.yellow;
		    } else if (cont.equalsIgnoreCase("blue")){
			color = Color.blue;
		    } else if (cont.equalsIgnoreCase("red")){
			color = Color.red;
		    }
		}
	    });

	bu_8 = new Button("Clean");
	add(bu_8);
	bu_8.setSize(65,15);
	bu_8.setLocation(0,390);
	bu_8.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e){
		    Graphics g = getGraphics();
		    g.setColor(TestEventFrame.this.getBackground());
		    g.fillRect(0,0,TestEventFrame.this.getWidth(),TestEventFrame.this.getHeight());
		}
	    });
	
	registerKeyListerner(new Component[]{list_2,choice_3,checkbx_4,checkbx_5,checkbx_6,tf_7,bu_8,bu_9,bu_10,bu_11},this);

	addMouseListener(new MouseAdapter(){
		public void mousePressed(MouseEvent e){
		    last_x = e.getX();
		    last_y = e.getY();
		}
	    });
    
	addMouseMotionListener(new MouseMotionAdapter(){
		public void mouseDragged(MouseEvent e){
		    Graphics g = getGraphics();
		    int x = e.getX(),
			y = e.getY();
		    g.setColor(color);
		    g.drawLine(last_x,last_y,x,y);
		    last_x = x;
		    last_y = y;
		}
	    });
    }

    /* implements KeyListener */
    public void keyTyped(KeyEvent e){
	char c = e.getKeyChar();
	switch (c){
	case 'y': color = Color.yellow; break;
	case 'b': color = Color.blue; break;
	case 'r': color = Color.red; break;
	}
    }
    
    public void keyPressed(KeyEvent e){}
    public void keyReleased(KeyEvent e){}

    private void registerKeyListerner(Component[] comps, KeyListener lis){
	for (int i=0;i<comps.length;i++){
	    comps[i].addKeyListener(lis);
	}
    }
}

