/*
	Draw applet
	Scot Hamilton
	CSCI 15a
	10/30/1998
*/

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.EventListener.*;

public class prog4 extends Applet 
                           implements MouseListener {
    // establish checkbox groups                       
       
       CheckboxGroup shape,      // The shape of the object
                  colorList,  // The color of the object
                  fillOrNot;  // The fill property of the object
    
    // establish Checkboxes
    // shape
       
       Checkbox cline,    // line
                crect,    // rectangle
                ccircle,  // circle
                coval,    // oval    
    
    // colorList
                cred,     // red 
                cblue,    // blue
                cgreen,   // green
                cblack,   // black
                cyellow,  // yellow
    // fillOrNot               
                cfilled,  // shaded in
                chollow;  // hollow
    // Establish labels
       Label    lspace1,
                lspace2;
                
    // Boolean Variables
       
       boolean  filled = true;
       
    // Integer Variables
       int      x1,y1,  // first coordinates form Mouse clicked
                x2,y2,  // Second coordinates form Mouse released
                temp;   // for swaping variables to correct mouse input
    
    // User Defined objects
       Line l;
       Rectangle r;
       Circle circle;
       Oval o;
                   
    // color and graphics            
       Color    c;     //color object       
                                 
                 
	public void init() {
	
	// Allocate Components
	
	// labels  
	   lspace1 = new Label("  ");
	   lspace2 = new Label("  ");
	// Checkbox Groups   
	   shape = new CheckboxGroup();
	   colorList = new CheckboxGroup();
	   fillOrNot = new CheckboxGroup();
	// shape
	   cline = new Checkbox("Line", shape, true);
	   crect = new Checkbox("Rectangle", shape, false);
	   ccircle = new Checkbox("Circle", shape, false);
	   coval = new Checkbox("Oval", shape, false);
	// colorList   	
	   cred = new Checkbox("Red", colorList, true);
	   cblue = new Checkbox("Blue", colorList, false);
	   cgreen = new Checkbox("Green", colorList, false);
	   cblack = new Checkbox("Black", colorList, false);
	   cyellow = new Checkbox("yellow", colorList, false);
	// fillOrNot
	   cfilled = new Checkbox("Filled", fillOrNot, true);   
	   chollow = new Checkbox("Hollow", fillOrNot, false);
	// Lay out Components  
	   add(cline);
	   add(crect);
	   add(ccircle);
	   add(coval);
	   add(lspace1);
	   add(cblack);
	   add(cred);
	   add(cblue);
	   add(cgreen);
	   add(cyellow);
	   add(lspace2);
	   add(cfilled);
	   add(chollow);
	// register MouseListener
	   addMouseListener(this);   
	  }
	// retrieve first set of coordinates 
    public void mousePressed(MouseEvent m){
	     x1 = m.getX();
	     y1 = m.getY();
	     }
	   
	public void mouseReleased(MouseEvent m){
	// retrieve second set of coordinatetes
	Graphics g = getGraphics();
	     x2 = m.getX();
	     y2 = m.getY();     
    
    // correct point possistion x1y1 = UL x2y2 = LR
    if(! (shape.getSelectedCheckbox() == cline)){
    
    // LR to UL
       {if(x1 > x2 && y1 > y2)
          {temp = x1;
           x1 = x2;
           x2 = temp;
           
           temp = y1;
           y1 = y2;
           y2 = temp;}
    
    // UR to LL
        if(x1 > x2 && y1 < y2)
          {temp = x1;
           x1 = x2;
           x2 = temp;}
    
    // LL to UR       
        if(x1 < x2 && y1 > y2)
          {temp = y1;
           y1 = y2;
           y2 = temp;}
           }
          }
    
     //     set Color    
     if(colorList.getSelectedCheckbox() == cred)
        c = Color.red;
        else if(colorList.getSelectedCheckbox() == cblue)
               c = Color.blue;
                else if(colorList.getSelectedCheckbox() == cgreen)
                c = Color.green;            
                     else if(colorList.getSelectedCheckbox() == cblack)
                             c = Color.black;
                             else c = Color.yellow; 
     // check fill properties
     if(fillOrNot.getSelectedCheckbox() == cfilled)
        filled = true;
        else filled = false;
     
     // construct appropriate objects
     if(shape.getSelectedCheckbox() == cline)
        l = new Line(x1, y1, x2, y2, c);
        else if((shape.getSelectedCheckbox() == crect))
                r = new Rectangle(x1, y1, x2, y2, c, filled);
                else if((shape.getSelectedCheckbox() == ccircle))
                     circle = new Circle(x1, y1, x2, y2, c, filled);
                      else o = new Oval(x1, y1, x2, y2, c, filled);
                
        if(shape.getSelectedCheckbox() == cline)
                 l.draw(g);
        else if(shape.getSelectedCheckbox() == crect)
                r.draw(g);
                else if(shape.getSelectedCheckbox() == ccircle)
                       circle.draw(g);
                        else o.draw(g); 

               
   }
   public void mouseEntered(MouseEvent m){}                        
   public void mouseExited(MouseEvent m){}
   public void mouseClicked(MouseEvent m){}
      
   public void paint( Graphics g ) {
		
}
}
