//************************************
//
// Alex Apostol
//
// Java applet illustrating the use of an array
// class for selection sorting an array.
//
//************************************

import java.applet.*;// used for applets
import java.awt.*;// if you use any kind of graphics
import java.io.*;// if you use any kind of I/O
import java.awt.event.*;

//************************************
//
// myApplet
//
//************************************

public class myApplet extends Applet implements ActionListener
{

    cookieVector v;
    Button clear, sort, add1;
    Button sizes[];
    Button flavors[];

    //************************************
    //
    // init
    //    Initialising, Allocating, and invoking listeners!
    //
    //************************************
    /**
     * Allocates and Inializes cookieVector and related buttons.
     * Adds buttons to canvas
     */
public void init()
{
    setBackground(Color.black);
    int i;

    v = new cookieVector();
    sizes= new Button[10];
    flavors = new Button[10];

     clear = new Button("New");
     sort = new Button("Sort");
     add1 = new Button ("Add");


     for (i=0; i<10; i++)
	 {
	     sizes[i]=new Button("Size "+i);
	     sizes[i].setBackground(Color.lightGray);
	     add(sizes[i]);
	     sizes[i].addActionListener(this);
	 }
     for (i=0; i<10; i++)
	 {
 	     flavors[i]=new Button("Type "+i);
	     flavors[i].setBackground(Color.orange);
	     add(flavors[i]);
	     flavors[i].addActionListener(this);
	 }

     clear.setBackground(Color.green);
     sort.setBackground(Color.green);
     add1.setBackground(Color.green);
     add(clear);
     add(sort);
     add(add1);

     clear.addActionListener(this);
     sort.addActionListener(this);
     add1.addActionListener(this);


} // End of Init

//************************************
//
// actionPerformed
//    Manifesting the User's will!
//
//************************************
/**
 * Executes Button clicks
 */
public void actionPerformed (ActionEvent e)
{
    Cookie c= new Cookie (4, 0, 0);
    int i;

    if(e.getSource()==clear)
	{
	    v.clear();
        }
    else if (e.getSource()==sort)
	{
	    v.sort();
	}
    else if (e.getSource()==add1)
	{
	    i=v.size();
	    c.setFlavor(i);
	    c.setID(i+1);
	    v.addElement(c);
	}
    for(i=0; i<10; i++)
	{
	    if (e.getSource()==sizes[i])
		v.incElement(i);
	}
    for(i=0; i<10; i++)
	{
	    if (e.getSource()==flavors[i])
		v.changeFlavor(i);
	}
    repaint();
} // End of actionPerformed


//************************************
//
// paint
//    Making good things be seen!
//
//************************************
/**
 * Calls print and draw for the cookieVector.
 */
    public void paint(Graphics g)
    {
	v.print();
       	v.draw(g);
    }//End of paint

}//End Class myApplet
