/*******************************************************************

  Class:	CS6600, Graphics
  Author:	Yury Izrailevsky
	
  Assignment 1 - Paint program


  This file contains the implementation of Y_Paint class.
*******************************************************************/

import java.awt.*;
import java.awt.event.*;
import java.util.Vector;

public class Y_Paint extends java.applet.Applet 
      implements ActionListener, ItemListener
{
	
    // ----------**** DATA MEMBERS ***-------------------------
    
    
    // -----------**** WIDGETS ***-----------------------
    // panels and menu
    Panel fControlPanel = new Panel();
    Y_Canvas fCanvas = new Y_Canvas();
    
    // selects current figure
    Choice fFigureChoice = new Choice();	
    
    // selects current color
    Choice fColorChoice = new Choice();
    
    // if shapes are filled in
    Checkbox fFillInCheckbox = new Checkbox("Fill In Color"); 
    
    // scale choice box
    Choice fScaleChoice = new Choice();

    // clears all shapes
    Button fClearButton = new Button("Clear"); 

    // completes polygon
    Button fCompletePolygonButton = new Button("Complete Polygon");
    
    // ----------**** GET/SET Functions ***--------------------
    
    
    // ---------**** APPLET FUNCTIONS ***----------------------
    
    //~~~~~~~~~~~~~~~~~~~~~~ INIT() ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    public void init() 
    {
	
	// -------------- Initialize widgets ------------------
	
	setLayout(new BorderLayout());
	//fControlPanel.setLayout( new GridLayout(1,5));
	
	// Add the figure choice listbox
	fFigureChoice.addItem("Polygon");
	fFigureChoice.addItem("Free Hand");
	fFigureChoice.addItem("Rectangle");
	fFigureChoice.addItem("Circle");
	fFigureChoice.addItem("Line");
	fFigureChoice.addItem("Ellipse");
	
	fControlPanel.add(fFigureChoice);
	
	fColorChoice.addItem("black");
	fColorChoice.addItem("red");
	fColorChoice.addItem("green");
	fColorChoice.addItem("blue");
	fColorChoice.addItem("orange");
	fColorChoice.addItem("white");

	fControlPanel.add(fColorChoice);
	
	fScaleChoice.addItem("50%");
	fScaleChoice.addItem("75%");
	fScaleChoice.addItem("100%");
	fScaleChoice.addItem("150%");
	fScaleChoice.addItem("200%");
	fScaleChoice.addItem("300%");
	
	fControlPanel.add(fScaleChoice);
	fScaleChoice.select(2);
	fCanvas.setCurScale(1.0);

	fControlPanel.add(fFillInCheckbox);
	
	fControlPanel.add(fClearButton);

	fControlPanel.add(fCompletePolygonButton);
	removeCompletePolygon();//hide it right away
	
	add("North", fControlPanel);
	add("Center", fCanvas);
	
	// Add listeners
	fFigureChoice.addItemListener(this);
	fFillInCheckbox.addItemListener(this);
	fColorChoice.addItemListener(this);
	fClearButton.addActionListener(this);
	fCompletePolygonButton.addActionListener(this);
	fScaleChoice.addItemListener(this);
	
	
	//------------------- Set up members -------------------------
	
	fCanvas.setCurFigureCode(fFigureChoice.getSelectedIndex());
	fCanvas.setFillIn(fFillInCheckbox.getState());
	
	
    }//~~~~~~~~~~~~~~~ end of init() ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    

    //~~~~~~~~~~~~~~~~ removes select widgets from the control panel ~~~~~~
    public boolean removeSelectWidgets()
    {
	fFigureChoice.setEnabled(false);
	fColorChoice.setEnabled(false);
	fFillInCheckbox.setEnabled(false);
	fClearButton.setEnabled(false);
	fScaleChoice.setEnabled(false);
	return true;
    } 

    //~~~~~~~~~~~~~~~~ adds select widgets to the control panel ~~~~~~
    public boolean addSelectWidgets()
    {
	fFigureChoice.setEnabled(true);
	fColorChoice.setEnabled(true);
	fFillInCheckbox.setEnabled(true);
	fClearButton.setEnabled(true);
	fScaleChoice.setEnabled(true);
	return true;
    } 

    //~~~~~~~~~~~~~~~~ add complete polygon button~~~~~~~~~~~~~~~~~~~~~~~~
    public boolean addCompletePolygon()
    {
	fCompletePolygonButton.setEnabled(true);
	return true;
    }

    //~~~~~~~~~~~~~~~~ remove complete polygon button~~~~~~~~~~~~~~~~~~~~~~~~
    public boolean removeCompletePolygon()
    {
	fCompletePolygonButton.setEnabled(false);
	return true;
    }

    
    //~~~~~~~~~~~~~~~~~ CLEAR FIGURES ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // This function cleans up the canvas and deletes all figures
    public int clearFigures()
    {
	return fCanvas.clearFigures();
    }
    
    
    
    //~~~~~~~~~~~~~~~~~~~~ ACTION PERFORMED ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    public void actionPerformed(ActionEvent evt)
    {
	// if CLEAR button
	if (evt.getSource() == fClearButton){
	    clearFigures();
	}

	if (evt.getSource() == fCompletePolygonButton){
	    fCanvas.completePolygon();
	}

    }//~~~~~~~~~~~~~~~~~~~~~~~ end of ACTION PERFORMED()~~~~~~~~~~~~~~~~~~~ 
    
    
    //~~~~~~~~~~~~~~~~~~~~~ ITEM STATE CHANGED ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    // For choice menus and the check box
    public void itemStateChanged(ItemEvent evt)
    {
	// ------------ see what caused the event ----------------
	
	// if the figure choice event
	if (evt.getSource() == fFigureChoice){
	    fCanvas.setCurFigureCode(fFigureChoice.getSelectedIndex());
	}
	
	// if the fill it choice event
	if (evt.getSource() == fFillInCheckbox){
	    fCanvas.setFillIn(fFillInCheckbox.getState());
	}
	
	// if the change color event
	if (evt.getSource() == fColorChoice){
	    switch (fColorChoice.getSelectedIndex()){
	    case 0:
		fCanvas.setCurColor(Color.black);
		break;
	    case 1:
		fCanvas.setCurColor(Color.red);
		break;
	    case 2:
		fCanvas.setCurColor(Color.green);
		break;
	    case 3:
		fCanvas.setCurColor(Color.blue);
		break;
	    case 4:
		fCanvas.setCurColor(Color.orange);
		break;
	    case 5:
		fCanvas.setCurColor(Color.white);
		break;
	    default:
		fCanvas.setCurColor(Color.black);
		break;
	    }// end switch
	}// end if

	if (evt.getSource() == fScaleChoice){
	    switch (fScaleChoice.getSelectedIndex()){
		// 50% - double
	    case 0:
		fCanvas.scaleFigures(2.0);
		break;

		// 75% - times 1.333
	    case 1:
		fCanvas.scaleFigures(1.333);
		break;

		// 100% - times 1
	    case 2:
		fCanvas.scaleFigures(1.0);
		break;

		// 150% - times .666
	    case 3:
		fCanvas.scaleFigures(0.666);
		break;

		// 200% - times .5
	    case 4:
		fCanvas.scaleFigures(0.5);
		break;

		// 300% - times .333
	    case 5:
		fCanvas.scaleFigures(.333);
		break;

	    default:
		break;
	    }// end switch

	}// end if
	
	
    }// ~~~~~~~~~~~~~~~~~~ END OF ITEM STATE CHANGED ~~~~~~~~~~~~~~~~~~~~~~~~
    
    
}// end of Y_Draw implementation


