// Program to draw shapes
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Draw extends JApplet implements ActionListener, ListSelectionListener, MouseListener 
{

    JList colorList, shapeList;
    JLabel colorLabel, shapeLabel;
    JButton clearButton, undoButton, lastPointButton;
    String [] colors = { "Red", "Green", "Blue", "Yellow", "Purple" };
    String [] shapes = { "Circle", "Square", "Rectangle", "Line", "Ploygon" };


    // setup the graphical user interface components
    // and initialize variables
    public void init()
    {
        int i;
         
        DefaultListModel colorListModel = new DefaultListModel();
        DefaultListModel shapeListModel = new DefaultListModel();

        colorLabel = new JLabel( "Color: ");
        shapeLabel = new JLabel( "Shape: ");

        clearButton = new JButton( "Clear" );
        undoButton = new JButton( "Undo" );
        lastPointButton = new JButton( "Last Point" );


        for ( i = 0; i < colors.length; i++ )
        {
            colorListModel.addElement( colors[i] );
        }

        for ( i = 0; i < shapes.length; i++ )
        {
            shapeListModel.addElement( shapes[i] );
        }
     
        colorList = new JList( colorListModel );
        shapeList = new JList( shapeListModel );
     
        Container c = getContentPane();
        c.setLayout( new FlowLayout() );

        c.add(colorLabel);
        c.add(colorList);
        c.add(shapeLabel);
        c.add(shapeList);
        c.add(clearButton);
        c.add(undoButton);
        c.add(lastPointButton);
     
        colorList.addListSelectionListener( this );
        shapeList.addListSelectionListener( this );
        clearButton.addActionListener( this );
        undoButton.addActionListener( this );
        lastPointButton.addActionListener( this );

        addMouseListener( this );
    }


    // process user's action on the inputVal text field
    public void actionPerformed( ActionEvent e )
    {
        if (e.getSource() == clearButton)
        {
           showStatus("Pressed Clear");
        }
        else if (e.getSource() == undoButton)
        {
           showStatus("Undo Button");
        }
        else if (e.getSource() == lastPointButton)
        {
           showStatus("Last Point Button");
        }
    }
   
    // process user's action on the list selections
    public void valueChanged(ListSelectionEvent e) 
    {
        int index;
    
        if (e.getSource() == colorList)
        {
            
            index = colorList.getSelectedIndex();

            if ( index >= 0 )
            {
                showStatus("New color is " + colors[index]);
            }
            else
            {
                showStatus("No color has been selected");
            }

        }
        else if (e.getSource() == shapeList)
        {

            index = shapeList.getSelectedIndex();

            if ( index >= 0 )
            {
                showStatus("New shape is " + shapes[index]);
            }
            else
            {
                showStatus("No shape has been selected");
            }
        }
    }
   
    // process mouse clicks
    public void mouseClicked( MouseEvent e )
    {
        int x, y;
        x = e.getX();
        y = e.getY();
        
        showStatus( "Mouse clicked on (" + x + "," + y + ")" );
    }
    
    public void mouseEntered( MouseEvent e )
    {
    }
    public void mouseExited( MouseEvent e )
    {
    }
    public void mousePressed( MouseEvent e )
    {
    }
    public void mouseReleased( MouseEvent e )
    {
    }

    //draw all shapes and dots
    public void paint( Graphics g )
    {
        super.paint(g);
    }

}
