//      Programer:  Greg Murray
//
//      Draw Various Objects Using a UI

import java.awt.*;

public class drawit2 extends java.applet.Applet {

   String yourchoice = "Rectangle";
   String theColor = "black";
   Point anchor;  // start of current object
   Point anchor2; // end of current object
   Point anchor3;
   Point anchor4;
   Point tempPoint1;
   Point tempPoint2;
   Point currentpoint; // current end of line
   boolean fillit;
   Choice c = new Choice();
   Choice co = new Choice();
   Button clr;

public void init() {

    Checkbox checkme;
    c.addItem("Rectangle");
    c.addItem("Circle");
    c.addItem("Line");
    c.addItem("Triangle");
    add(c);
    checkme = new Checkbox("Fill in");
    add(checkme);
    co.addItem("black");
    co.addItem("blue");
    co.addItem("red");
    co.addItem("green");
    co.addItem("white");
    add(co);
    clr= new Button("Clear"); 
    add(clr);
   }

public boolean mouseDown(Event evt, int x, int y) {
    anchor = new Point(x,y);  // anchor the new point to 1st anchor  
    anchor3= new Point(x,y);
    return true;
  }

public boolean mouseUp(Event evt, int x, int y) 
  {   
    anchor2 = new Point(x,y);
    anchor4 = anchor2;
    additem(x,y);
    return true;
  }

public boolean mouseDrag(Event evt, int x, int y)
 {
    currentpoint = new Point(x,y);
      // if its a rectangle or circle we need to convert the points to
      // coincide with width and heighth instead of points
     additem(x,y);
     return true;
  }

void additem(int x, int y)  //allows multiple items to be selected (will)
   {
    anchor2 = new Point(x,y);
    tempPoint1= anchor3;
  
      // if its a rectangle or circle we need to convert the points to
      // coincide with width and heighth instead of points

    if ((yourchoice.equals("Rectangle")) || (yourchoice.equals("Circle")))
     {
      if (anchor2.x < anchor3.x)
        {

         tempPoint1.x = currentpoint.x;
         tempPoint2.x = anchor3.x;
         tempPoint2.y= currentpoint.y;
         tempPoint1.y=anchor3.y;
         anchor2 = tempPoint2;
         anchor = tempPoint1;
        System.out.println("(A)anchor.x " + anchor.x + " anchor2.x" + anchor2.x);
        System.out.println("(A)anchor.y " + anchor.y + " anchor2.y" + anchor2.y);

        }
      anchor2.x = (anchor2.x - anchor.x); // width
      anchor2.y = (anchor2.y - anchor.y); // heigth
     }
  //  anchor2 = tempPoint2;
 //   anchor = tempPoint1;

    currentpoint = null;
    repaint();
    currentpoint= null;

   }

public boolean action(Event evt, Object arg)
  {
    String temp;
    System.out.println(arg);
    if (evt.target.equals(c)) yourchoice = (String)arg;
    if (evt.target.equals(co)) theColor = (String)arg;    
    if (evt.target instanceof Checkbox) // turn fill on/off 
       if (fillit) fillit = false;      // depends on whether on or off
          else fillit = true;
    if (evt.target.equals(clr))
       {
        temp = yourchoice;
        yourchoice = "";
        anchor = null;
        anchor2 = null;
        currentpoint = null;
        repaint();
        yourchoice = temp;
       }
    return true;
   }

public void paint(Graphics g) {

    // draw current object

    // set the color
    g.setColor(Color.black); // Black will be the default
    if (theColor.equals("black")) g.setColor(Color.black);
    if (theColor.equals("green")) g.setColor(Color.green);
    if (theColor.equals("blue")) g.setColor(Color.blue);
    if (theColor.equals("red")) g.setColor(Color.red);
    if (theColor.equals("white")) g.setColor(Color.white);

   // Draw Stuff
   // Line

   if ((currentpoint != null)  && (yourchoice.equals("Line")))
      g.drawLine(anchor.x,anchor.y,currentpoint.x,currentpoint.y);
   else if (yourchoice.equals("Line")) 
         g.drawLine(anchor.x,anchor.y,anchor2.x,anchor2.y);

   // Rectangle
        // A stretching filled rectangle
   if ((currentpoint != null) && (yourchoice.equals("Rectangle"))
         && (fillit)) 
      g.fillRect(anchor.x,anchor.y,anchor2.x,anchor2.y); 
        // A stretching circle frame 
    else if ((currentpoint != null) && (yourchoice.equals("Rectangle")))
      g.drawRect(anchor.x,anchor.y,anchor2.x,anchor2.y);
        // A stationary filled rectangle
    else if ((yourchoice.equals("Rectangle")) && (fillit))
      g.fillRect(anchor.x,anchor.y,anchor2.x,anchor2.y);
        // A stationary rectangle frame
    else if (yourchoice.equals("Rectangle")){
      g.drawRect(anchor.x,anchor.y,anchor2.x, anchor2.y);}

   // Circle
        // a filled stretching circle
   if ((currentpoint != null) && (yourchoice.equals("Circle"))
         && (fillit))  
         g.fillOval(anchor.x,anchor.y,anchor2.x,anchor2.y);
       // A stretching circle frame
   else if ((currentpoint != null) && (yourchoice.equals("Circle")))
         g.drawOval(anchor.x,anchor.y,anchor2.x,anchor2.y);
       // A stationary filled circle    
   else if ((yourchoice.equals("Circle")) && (fillit))
         g.fillOval(anchor.x,anchor.y,anchor2.x,anchor2.y);
       // A stationary circle frame
   else if (yourchoice.equals("Circle"))
         g.drawOval(anchor.x,anchor.y,anchor2.x,anchor2.y);

    // Triangle - Don't go looking for a pre-defined method
    //            I created this one from scratch

   if ((currentpoint != null)&& (yourchoice.equals("Triangle"))
         && (fillit))
    // This is a streching filled triangle - created using a polygon
        {
         int exes[] = {anchor.x,currentpoint.x,
               (anchor.x-(currentpoint.x-anchor.x)),
               (anchor.x-(currentpoint.x-anchor.x))};
         int whys[] = {anchor.y,currentpoint.y,currentpoint.y,currentpoint.y};
         int pts = exes.length;
         g.fillPolygon(exes,whys,pts);
        }
   else if ((currentpoint != null) && (yourchoice.equals("Triangle")))
    // This is a streching frame of a triangle
       {
        g.drawLine(anchor.x,anchor.y,currentpoint.x,currentpoint.y);
        g.drawLine(anchor.x,anchor.y,(anchor.x-(currentpoint.x-anchor.x)),
            currentpoint.y);
        g.drawLine(currentpoint.x,currentpoint.y,
            (anchor.x-(currentpoint.x-anchor.x)),currentpoint.y);
       } 
   else if ((yourchoice.equals("Triangle")) && (fillit))
    //  This is a filled stationary triangle
        {
         int exes[] = {anchor.x,anchor2.x,(anchor.x-(anchor2.x-anchor.x)),
               (anchor.x-(anchor2.x-anchor.x))};
         int whys[] = {anchor.y,anchor2.y,anchor2.y,anchor2.y};
         int pts = exes.length;
         g.fillPolygon(exes,whys,pts);
        }
   else if (yourchoice.equals("Triangle"))
    // This is a stationary frame of a triangle
       {
        g.drawLine(anchor.x,anchor.y,anchor2.x,anchor2.y);
        g.drawLine(anchor.x,anchor.y,(anchor.x-(anchor2.x-anchor.x)),
            anchor2.y);
         g.drawLine(anchor2.x,anchor2.y,
            (anchor.x-(anchor2.x-anchor.x)),anchor2.y);
       }

    


    }
}






