import java.applet.*;
import java.awt.*;
public class Geom1 extends Applet {
   private int angl=360;
   private int last_x=0;
   private int last_y=0;
   private Color current_color=Color.black;
   private Button clear_button;
   private Choice color_choices;
   private Choice arc_choices;
   private Button square_button;
   private Button circ_button;
   private Button choose_button;


    public class point implements Cloneable{
                 public int x;
                 public int y;

                  public Object clone(){ //begin clone
                    try{ //begin try
                        return super.clone();
                        } //end try
                         
                    catch(CloneNotSupportedException e){ // begin catch
                        return null;  //if there is a catch
                         } //end catch
                    } //end cloning structure for point
             
                 }  //end point def


     public class Vcirc implements Cloneable{
                 
                 public point c=new point();
                 public point v=new point();
                 public int s;

                  public Object clone(){ //begin clone
                    try{ //begin try
                        Vcirc vc=(Vcirc)super.clone();
                        vc.c=(point)c.clone();
                        vc.v=(point)v.clone();
                        return vc;
                        } catch(CloneNotSupportedException e){
                           return null;
                          }//end catch

                    } //end cloning structure for virtual circle c is center, v is vector
  
   
           } //end virtual circle def

      public Vcirc circ1=new Vcirc(); 
      public boolean choose=false;

//init applet
   public void init(){

//set backgd color
    this.setBackground(Color.white);

//create a restart button
    clear_button=new Button("Clear");
    clear_button.setForeground(Color.black);
    clear_button.setBackground(Color.lightGray);
    this.add(clear_button);

//create a choose button
    choose_button=new Button("Choose");
    choose_button.setForeground(Color.black);
    choose_button.setBackground(Color.lightGray);
    this.add(choose_button);

//create square button
     square_button=new Button("Square");
     square_button.setForeground(Color.black);
     square_button.setBackground(Color.lightGray);
     this.add(square_button);

//create circle button
     circ_button=new Button("Circle");
     circ_button.setForeground(Color.black);
     circ_button.setBackground(Color.lightGray);
     this.add(circ_button);

//create menu of colors
    color_choices=new Choice();
    color_choices.addItem("black");
    color_choices.addItem("red");
    color_choices.addItem("blue");
    color_choices.addItem("green");
    color_choices.addItem("yellow");

    color_choices.setForeground(Color.black);
    color_choices.setBackground(Color.lightGray);
    this.add(new Label("Color:"));
    this.add(color_choices);


   }
//end init applet


//called when mouse click
    public boolean mouseDown(Event e, int x, int y){
        last_x=x; last_y=y;
          if (choose){
            if((circ1.c.x-3<last_x)  &  (last_x<circ1.c.x+3)
             & (circ1.c.y-3<last_y) & (last_y<circ1.c.y+3)){
             Graphics g=this.getGraphics();
             g.setColor(current_color);
             g.drawArc(circ1.c.x-2,circ1.c.y-2,8,8,0,360);
             choose=false;
             }
          }
        return true;
       }


//called when mouse moves
    public boolean mouseDrag(Event e, int x, int y){
          Graphics g=this.getGraphics();
          g.setColor(current_color);
          if((circ1.c.x-3<last_x)  &  (last_x<circ1.c.x+3)
             & (circ1.c.y-3<last_y) & (last_y<circ1.c.y+3)){
              g.setColor(Color.white);
                g.drawArc(circ1.c.x-circ1.s,circ1.c.y -circ1.s,2*circ1.s,2*circ1.s,0,360);
                //draw center
                g.drawArc(circ1.c.x,circ1.c.y,4,4,0,360);
                // draw radial point
                g.drawArc(circ1.c.x+ circ1.v.x-2,circ1.c.y+circ1.v.y-2,4,4,0,360);
          g.setColor(current_color);
          circ1.c.x=x;
          circ1.c.y=y;
                g.drawArc(circ1.c.x-circ1.s,circ1.c.y -circ1.s,2*circ1.s,2*circ1.s,0,360);
                //draw center
                g.drawArc(circ1.c.x,circ1.c.y,4,4,0,360);
                // draw radial point
                g.drawArc(circ1.c.x +circ1.v.x-2,circ1.c.y+circ1.v.y-2,4,4,0,360);
          
          }


          if((circ1.c.x +circ1.v.x-3<last_x)  &  (last_x<circ1.c.x+circ1.v.x+3)
             & (circ1.c.y+circ1.v.y-3<last_y) & (last_y<circ1.c.y+circ1.v.y+3)){
              g.setColor(Color.white);
                g.drawArc(circ1.c.x-circ1.s,circ1.c.y -circ1.s,2*circ1.s,2*circ1.s,0,360);
                //draw center
                g.drawArc(circ1.c.x,circ1.c.y,4,4,0,360);
                // draw radial point
                g.drawArc(circ1.c.x+circ1.v.x-2,circ1.c.y+circ1.v.y-2,4,4,0,360);
          g.setColor(current_color);
          circ1.v.y=circ1.v.y-last_y+y;
          circ1.v.x=circ1.v.x-last_x+x;
          circ1.s=(int)Math.sqrt(circ1.v.x*circ1.v.x + circ1.v.y*circ1.v.y);
                g.drawArc(circ1.c.x-circ1.s,circ1.c.y -circ1.s,2*circ1.s,2*circ1.s,0,360);
                //draw center
                g.drawArc(circ1.c.x,circ1.c.y,4,4,0,360);
                // draw radial point
                g.drawArc(circ1.c.x+circ1.v.x-2,circ1.c.y+circ1.v.y-2,4,4,0,360);
          }
          last_x=x;
          last_y=y;
          return true;
      }


//user clicks button or chooses a color
     public boolean action(Event event, Object arg){
          // in case of click
          if(event.target==clear_button){
               Graphics g=this.getGraphics();
               Rectangle r=this.bounds();
               g.setColor(this.getBackground());
               g.fillRect(r.x,r.y,r.width,r.height);
               return true;
              }
            
           // else do for choose
           else if(event.target==choose_button){
                  choose=true;
                  return true;
               }


           // else do for color choices
           else if(event.target==color_choices){
                 if(arg.equals("black")) current_color=Color.black;
                 else if(arg.equals("red")) current_color=Color.red;
                 else if(arg.equals("blue")) current_color=Color.blue;
                 else if(arg.equals("green")) current_color=Color.green;
                 else if(arg.equals("yellow")) current_color=Color.yellow;

                 return true;
                }
          
           // else do
           else if(event.target==square_button){
                Graphics g=this.getGraphics();
                g.setColor(current_color);
                g.drawRect(10,10,40,40);
                return true;    
               }
            // else do
           else if(event.target==circ_button){
                Graphics g=this.getGraphics();
                g.setColor(current_color);
                circ1.c.x=190;
                circ1.c.y=190;
                circ1.v.x=0;
                circ1.v.y=90;
                circ1.s=90;
                // draw circle
                g.drawArc(circ1.c.x-circ1.s,circ1.c.y -circ1.s,2*circ1.s,2*circ1.s,0,360);
                //draw center
                g.drawArc(circ1.c.x,circ1.c.y,4,4,0,360);
                // draw radial point
                g.drawArc(circ1.c.x+circ1.v.x-2,circ1.c.y+circ1.v.y-2,4,4,0,360);
                return true;    
               }
           // note that the last two args in drawArc 
           // tell the angle to start and the angular length.


           // Otherwise let the superclass handle it.
           else return super.action(event, arg);
     }
}

