'תרגיל 12 א
סמסטר א' תשנ"ט JAVA


/**
* @author Nurit Lederman
* Game of blowing up balloons
*/


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

					 	    	 
public class Game extends Applet
 {    
   private int pause=100;           //pause time each painting                        	     
   private int hitsCounter=0;       //to count number of hits
   private int balloonX=500;        //x coordinate of the balloon
   private int balloonY=50;         //y coordinate of the balloon
   private int centerX=balloonX+50; //center point of the balloon
   private int centerY=balloonY+50;
   private final int RADIUS=50;     // balloon's diameter
   private boolean play=false;  //the game's state-running or not
    
  
   public void init()
    { 
         // the applet is divided to two areas- menu and game 

      setLayout(null);                     
      MenuPanel menuP=new MenuPanel();    
      menuP.setBounds(0,0,200,500);  // fixing the size and place
      menuP.init();
      menuP.start();
      add(menuP);
      
      GamePanel gameP=new GamePanel(); 
      gameP.setBounds(201,0,500,500); //fixing the size and place
      gameP.init();
      gameP.start();    
      add(gameP);              
    }

 
      // main method to make it possible to run as an application
   public static void main(String args[])
    {
     Game bGame=new Game();     // instance of the Game class
     Frame f=new Frame();
     f.setTitle("Hit the balloon"); 
     f.addWindowListener(new WindowAdapter()
                        {public void windowClosing(WindowEvent e)
                          { System.exit(0); }
                        }); 
     f.add(bGame,BorderLayout.CENTER); 
     bGame.init();
     bGame.start();
     f.setSize(700,500);
     f.setVisible(true);
    }

  
   //-----------------------------------------------------------
   // inner class in Game. The menu area.

  class MenuPanel extends Applet implements Runnable
   { 
        
    Button startB;         // button to start a new game 
    Button stopB;          // button to stop the game
        // group of radio buttons to change the level of the game
    CheckboxGroup gameLevel; 
    Checkbox begginer,better,expert;    
    TextField hits;  
    Thread hitsThread;     // to write number of hits so far
   
    public void init()
     {                    
       setLayout(null);
       setBackground(Color.green); 

         // initialing the components
       startB=new Button("new game");   
       stopB=new Button("stop game");     
       gameLevel=new CheckboxGroup();            
       begginer=new Checkbox("Begginer",gameLevel,true);
       better=new Checkbox("Better",gameLevel,false);
       expert=new Checkbox("Expert",gameLevel,false);
       hits=new TextField();  
       hitsThread=new Thread(this);   
   
         //adding listeners to all components
       startB.addActionListener(new NewGame());
       stopB.addActionListener(new StopGame());
       begginer.addItemListener(new ChangeLevel(70));
       better.addItemListener(new ChangeLevel(40));
       expert.addItemListener(new ChangeLevel(20)); 
         
        //fixing size and place of all components 
       begginer.setBounds(50,50,70,20);   // (x,y,width,height) 
       better.setBounds(50,70,70,20);
       expert.setBounds(50,90,70,20);	
       startB.setBounds(25,310,70,30);  
       stopB.setBounds(110,310,70,30);
       hits.setBounds(70,400,50,20);
       
        //adding components
       add(startB);   
       add(stopB);                 
       add(hits);
       add(begginer);
       add(better);
       add(expert);
 
       hitsThread.start();   // starting the thread in order to 
                             // write the current number of hits
     }


     
    public void paint(Graphics g)
     {       
       Font theFont=new Font("Helvetica",Font.BOLD,14);
       g.setFont(theFont);
       g.setColor(Color.blue);
       g.drawRect(10,20,180,440);  // draw frame     
       g.drawString("Level of game:",50,40);   
       g.setColor(Color.red);  
       g.fillRect(30,150,140,120);
       g.setColor(Color.black);       
       g.drawString("Try to blow up as",32,170);	
       g.drawString("many balloons as",34,185);
       g.drawString("you can.",34,200);
       g.drawString(" By clicking on the",32,225);
       g.drawString("balloon you can",34,240);
       g.drawString("change its color.",34,255);
       g.drawString("Number of hits:",50,380);
     }  

 
       // run method for hitsThread
    public void run()
     {
       while (true)
        {
         try
          {     		     // updating number of
            hitsThread.sleep(500);   // hits every half a second
          }
         catch(InterruptedException e) {}
         hits.setText(Integer.toString(hitsCounter));
       }
     }

      				 
    /* changing the level of the game:
       The lowest the level is,the more time we wait between each
       time we draw the balloon in his new position. 
       To each radio button we attach a listener with a different
       pause time. When the player choose certain botton of 
       level,the listener of that button changes "pause" to the
       pause time of that certain level.  
    */
  
    class ChangeLevel implements ItemListener
     { 
       int pauseTime;
  
       public ChangeLevel(int pt)
        {
	 pauseTime=pt;    // the pause time in that level
        } 

       public void itemStateChanged(ItemEvent event)
        {
          pause=pauseTime;  
        }
     }



       //listener to the startB button 
    class NewGame implements ActionListener
     {
       public void actionPerformed(ActionEvent e)
        {    	            
          play=true;  //so the balloon will be drawn
          balloonX=500;   //start from right side of the window
          hitsCounter=0;  // initial number of hits
        }
     }     



      // listener to the stopB button
    class StopGame implements ActionListener
     {
      public void actionPerformed(ActionEvent e)
        {    	            
          play=false;    // to stop drawing the balloon 
        }
     }


  }  //end MenuPanel 




  //--------------------------------------------------------
  // inner class in Game. The game area

  class GamePanel extends Applet 
   {  
     private Button b1,b2,b3,b4;     //buttons to hit the balloon
     Balloon balloonPanel=new Balloon();    // the balloon area
      
     public void init()
      {
       setLayout(null);      
       b1=new Button("1");
       b2=new Button("2");
       b3=new Button("3");
       b4=new Button("4");
       b1.setBounds(85,430,30,30);
       b2.setBounds(185,430,30,30);
       b3.setBounds(285,430,30,30);
       b4.setBounds(385,430,30,30);	

	 // adding listeners with the range of each balloon
       b1.addActionListener(new ButtonPressed(85,115));
       b2.addActionListener(new ButtonPressed(185,215));
       b3.addActionListener(new ButtonPressed(285,315));
       b4.addActionListener(new ButtonPressed(385,415));
       
       add(b1);
       add(b2);
       add(b3);
       add(b4);
       
       balloonPanel.setBounds(0,0,500,400);       	    
       add(balloonPanel);              
       balloonPanel.init();
       balloonPanel.start();
     }
       

   
      // listener to the buttons.
      // Each button has it's range,if the balloon is in that  
      // range when the player presses that button,the 
      // balloon explodes

    class ButtonPressed implements ActionListener
     {
       int startPoint,endPoint;   // the range of the button

          	  //ctor to get the range
       public ButtonPressed(int x,int y)  
        {
          startPoint=x; 
          endPoint=y;
        }

       public void actionPerformed(ActionEvent event)
        {
                           // balloonX-the start of the balloon
         int balloonEnd;  //the end of the balloon(x coordinate)
         balloonEnd=balloonX+2*RADIUS;

         if       
            //cases in which the balloon is in the button's range
          (
           ((startPoint<=balloonX) && (balloonX<=endPoint)) ||
           ((startPoint<=balloonEnd) && (balloonEnd<=endPoint))||
           ((startPoint>balloonX) && (endPoint