//Title    :Archery2
//Name   :Abdulla Al-mutawa
//Date     :09/27/00
//Purpose  :We are going to to add new features to the last program
//          we will add some buttons and text field which mack the           
//          user able to clear the display windows,request the circles 
//          and the checkerboard to be drown and changing the size and
//          position of the circles and the checker
//          there is also a button to throw a drats and also will be 
//          printed on the circles




// Imported libraries

  import java.awt.*;
  import java.applet.Applet;
  import java.awt.event.*;
               

// Main class 
    public class Archery2 extends Applet implements ActionListener
      
    {  
//create list 
     
     private int x, y, radius, z, m, draw, get_score, score;      
     private Button checkerboard_button = new Button ("checkerboard");
     private Button circle_button       = new Button ("circle");
     private Button clear_button        = new Button ("clear");
     private Button hello_button        = new Button ("Hello");
     private TextField size_field       = new TextField (3);
     private TextField x_field          = new TextField (3);
     private TextField y_field          = new TextField (3);
     private Label sizelabel            = new Label ("change size");
     private Label xlabel               = new Label ("change x");
     private Label ylabel               = new Label ("change y");
     private Panel a_panel              = new Panel ();
     private Panel b_panel              = new Panel ();
     private boolean hello              = false;
     
     
     
// init
    public void init()        
     
       {
//add list 
        
        setLayout(new BorderLayout());
        add("South",a_panel);
        a_panel.add("South",checkerboard_button);
        checkerboard_button.addActionListener(this);
        a_panel.add("South",circle_button);
        circle_button.addActionListener(this);
        a_panel.add("South",clear_button);
        clear_button.addActionListener(this);
        a_panel.add("South",hello_button);
        hello_button.addActionListener(this);

        b_panel.setLayout(new GridLayout(1,6));
        add("North",b_panel);
        b_panel.add(sizelabel);
        b_panel.add(size_field);
        size_field.addActionListener(this);
        b_panel.add(xlabel);
        b_panel.add(x_field );
        x_field.addActionListener(this);
        b_panel.add(ylabel);
        b_panel.add(y_field );
        y_field.addActionListener(this);

}//end init

// actionPerformed
    public void actionPerformed(ActionEvent event) 
  
    {
   
      if    (event.getSource() == circle_button)
      
           {                  
               x       = Integer.parseInt(x_field.getText());
               y       = Integer.parseInt(y_field.getText());
               radius  = Integer.parseInt(size_field.getText());
               draw = 1; 
           } 
   
                 
    if      (event.getSource() == checkerboard_button)
       
             {                  
               x       = Integer.parseInt(x_field.getText());
               y       = Integer.parseInt(y_field.getText());
               radius  = Integer.parseInt(size_field.getText());
               draw = 2; 
            } 
                
    if      (event.getSource() == clear_button)  
               
              draw = 0;
   
    if       (event.getSource() == hello_button)
                
                hello=true; 
    
             
                repaint();  
    }  //end actionPerformed
     
 // paint    
public void paint(Graphics window)
  {
    if   (draw == 1)
    if   ((x >= 180 && x <= 240 )&&(y <= 270 && y >= 125)&&(radius <= 20)) 
      
// draw target 
          draw_target (window,x, y, radius);
    
   else
        window.drawString(" Sorry Guys try to put the right #  ",100,200);
      
    if   (draw == 2) 
    if   ((x >= 180 && x <= 250 )&&(y <= 260 && y >= 125)&&(radius <= 20)) 
//draw line 
              {  int row =5;
                draw_line1 (window, row++);
                draw_line2 (window, row++);
                draw_line1 (window, row++);
                draw_line2 (window, row++);
                draw_line1 (window, row++);
              }
    else
        window.drawString(" Sorry try to put the right #  ",100,200);
              
 
   if     (draw == 0) 
          hello=false;   
   if     (hello) 
       {
        z = (int)(Math.random() * x) + 100;
        m = (int)(Math.random() * y) + 100;
        window.setColor(Color.blue);
        window.drawString("Hello",m,z);
       get_score =(int)Math.sqrt(((x-radius)+z)*((x-radius)+m)+((y-radius)+z)*((y-radius)+m));
  if      (get_score>radius)
       score = 0;
  else if (get_score>6*radius/6)
       score =1;
  else if (get_score>4*radius/6) 
       score = 2;
  else if (get_score>2*radius/6)
       score= 3; 
       window.setColor(Color.red); 
       window.drawString(" Hello Position IS "+score,140,40); 
       

       }

}//end paint

//method to draw_circle
private void draw_circle (Graphics window, Color current_color, int radius, int x, int y, int score)
	{
            window.setColor(current_color);
            window.fillOval(y-radius, x-radius, radius*2, radius*2);
            window.setColor(Color.black);
            window.drawString(""+(score),y-2, x-(radius-15));


} //end method draw_circle 

// draw_targe
private void draw_target (Graphics window,  int x, int y, int row)
	   {
	
//declare and initialize colors for circles
               Color color1 = Color.blue;
               Color color2 = Color.green;
               Color color3 = Color.red;
               int score = 3;

//draw three circle alternating color 
               draw_circle (window, color1, radius*6, x, y, score); 
               draw_circle (window, color2, radius*4, x, y, score);
               draw_circle (window, color3, radius*2, x, y, score);

}//end private method to draw_target

//private method to draw line 1 
private void draw_line1 (Graphics window, int row)
	{
	       int column = 4;
	       Color color1 = Color.black;
	       Color color2 = Color.red;
		
               draw_square (window, column++, row, color1);
	       draw_square (window, column++, row, color2);
	       draw_square (window, column++, row, color1);
	       draw_square (window, column++, row, color2);
	       draw_square (window, column++, row, color1);
} //end method draw_line1

//private method to draw line 2
private void draw_line2 (Graphics window, int row)
	{
                int column = 4;
		Color color2 = Color.black;
		Color color1 = Color.red;
		
                draw_square (window, column++, row, color1);
		draw_square (window, column++, row, color2);
		draw_square (window, column++, row, color1);
		draw_square (window, column++, row, color2);
		draw_square (window, column++, row, color1);
        } //end method draw_line2

//private method to draw square 
private void draw_square (Graphics window, int column, int row, Color currentColor)
	  {
		int size = 30;
		window.setColor (currentColor);
		window.fillRect ((column*size), (row*size), size, size);
} //end method draw_square

}//end class Archery2
 
