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

public class b_box extends Applet implements Runnable{

   Thread th = null;
   Image os;
   Graphics og;
   int td[][] = new int[10][10];
   int at[][] = new int[10][10];
   int ds,co;
   int px,py;
   int lg = 0,ln = 1;
   int lx,ly,ld,lf=0,cf,hf=1;
   Color c1,c2,c3;
   Button ng,op;
   Label sp;

   public void init(){
      os = createImage(420,420);
      og = os.getGraphics();
      setBackground(Color.white);

      setLayout(new BorderLayout());
      Panel pan = new Panel();
      pan.setLayout(new FlowLayout());
      pan.add(ng = new Button(" new game "));
      pan.add(sp = new Label("       "));
      pan.add(op = new Button(" open/close "));
      add("South",pan);

      addMouseListener(
         new MouseAdapter(){
            public void mousePressed(MouseEvent e){
               px = (e.getX()-60)/30;
               py = (e.getY()-60)/30;
               if((e.getX() > 90)&&(e.getX() < 330)&&(e.getY() > 30)&&(e.getY() < 60)){
                  lg = 0 ; ln = px ; lf = 0 ; cf = 0 ;
               }
               if((e.getX() > 30)&&(e.getX() < 60)&&(e.getY() > 90)&&(e.getY() < 330)){
                  lg = 1 ; ln = py ; lf = 0 ; cf = 0 ;
               }
               if((e.getX() > 360)&&(e.getX() < 390)&&(e.getY() > 90)&&(e.getY() < 330)){
                  lg = 2 ; ln = py ; lf = 0 ; cf = 0 ;
               }
               if((e.getX() > 90)&&(e.getX() < 330)&&(e.getY() > 360)&&(e.getY() < 390)){
                  lg = 3 ; ln = px ; lf = 0 ; cf = 0 ;
               }
               if((hf == 1)&&(e.getX() > 90)&&(e.getX() < 330)&&(e.getY() > 90)&&(e.getY() < 330)){
                  at[py][px] = (at[py][px] + 1) % 2;
                  lf = 0;
               }else{
                  lf = 0;
               }
               repaint();
            }
         }
      );

      ng.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
            b_set();
            repaint();
         } 
      });

      op.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
            hf = -1 * hf;
            lf = 0;
            repaint();
         } 
      });

      c1 = new Color(255,102,102);
      c2 = new Color(255,0,0);
      c3 = new Color(255,255,153);
      b_set();

   }

   public void paint(Graphics g){
      og.setColor(Color.white);
      og.fillRect(0,0,420,420);

      for(int k1 = 1;k1 < 9;k1 ++){
         for(int k2 = 1;k2 < 9;k2 ++){
            og.setColor(c3);
            og.fillRect(k1*30+60,k2*30+60,30,30);
            if(td[k2][k1] == 1)b_draw(k1*30+60,k2*30+60,2);
         }
      }

      og.setColor(Color.gray);
      for(int k1 = 0;k1 < 8;k1 ++){
         og.drawRect(k1*30+90,30,30,30);
         og.drawRect(k1*30+90,360,30,30);
         og.drawRect(30,k1*30+90,30,30);
         og.drawRect(360,k1*30+90,30,30);
      }

      switch(lg){
         case 0 : og.setColor(Color.black) ; og.fillRect(ln*30+60,30,30,30);
                  og.setColor(Color.white) ; og.drawLine(ln*30+60,35,ln*30+90,35) ; og.drawLine(ln*30+60,40,ln*30+90,40);
                  lx = ln ; ly = 0 ; ld = 3 ;
                  og.setColor(c1) ; og.fillRect(lx*30+72,ly*30+60,6,30);
                  break;
         case 1 : og.setColor(Color.black) ; og.fillRect(30,ln*30+60,30,30);
                  og.setColor(Color.white) ; og.drawLine(35,ln*30+60,35,ln*30+90) ; og.drawLine(40,ln*30+60,40,ln*30+90);
                  lx = 0 ; ly = ln ; ld = 0 ;
                  og.setColor(c1) ; og.fillRect(lx*30+60,ly*30+72,30,6);
                  break;
         case 2 : og.setColor(Color.black) ; og.fillRect(360,ln*30+60,30,30);
                  og.setColor(Color.white) ; og.drawLine(385,ln*30+60,385,ln*30+90) ; og.drawLine(380,ln*30+60,380,ln*30+90);
                  lx = 9 ; ly = ln ; ld = 2 ;
                  og.setColor(c1) ; og.fillRect(lx*30+60,ly*30+72,30,6);
                  break;
         case 3 : og.setColor(Color.black) ; og.fillRect(ln*30+60,360,30,30);
                  og.setColor(Color.white) ; og.drawLine(ln*30+60,385,ln*30+90,385) ; og.drawLine(ln*30+60,380,ln*30+90,380);
                  lx = ln ; ly = 9 ; ld = 1 ;
                  og.setColor(c1) ; og.fillRect(lx*30+72,ly*30+60,6,30);
                  break;
      }

      while(lf == 0){
         switch(ld){
            case 0 : lx ++;
                     if(lx == 9){
                        og.fillRect(lx*30+60,ly*30+72,30,6);lf = 1;
                     }else if(td[ly][lx]==1){
                        lf = 1;
                     }else if((lx==1)&&((td[ly-1][lx]==1)||(td[ly+1][lx]==1))){
                        og.setColor(c2) ; cf = 1 ; og.fillRect(lx*30+60,ly*30+72,30,6);
                        og.fillOval(lx*30+78,ly*30+69,13,13);
                        ld = 2 ;
                     }else if(td[ly][lx+1]==1){
                        og.fillRect(lx*30+60,ly*30+72,30,6);
                        lf = 1;
                     }else if((td[ly-1][lx+1]==1)&&(td[ly+1][lx+1]==1)){
                        og.setColor(c2) ; cf = 1 ; og.fillRect(lx*30+60,ly*30+72,30,6);
                        og.fillOval(lx*30+78,ly*30+69,13,13);
                        ld = 2 ; 
                     }else if(td[ly-1][lx+1]==1){
                        og.fillArc(lx*30+60-18,ly*30+60+12,36,36,0,90);
                        og.setColor(c3) ; og.fillArc(lx*30+60-12,ly*30+60+18,24,24,0,90);
                        ld = 3;
                        if(cf == 0){
                           og.setColor(c1);
                        }else{
                           og.setColor(c2);
                        }
                     }else if(td[ly+1][lx+1]==1){
                        og.fillArc(lx*30+60-18,ly*30+60-18,36,36,270,90);
                        og.setColor(c3) ; og.fillArc(lx*30+60-12,ly*30+60-12,24,24,270,90);
                        ld = 1;
                        if(cf == 0){
                           og.setColor(c1);
                        }else{
                           og.setColor(c2);
                        }
                     }else{
                        og.fillRect(lx*30+60,ly*30+72,30,6);
                     }
                     break;
            case 1 : ly --;
                     if(ly == 0){
                        og.fillRect(lx*30+72,ly*30+60,6,30);lf = 1;
                     }else if(td[ly][lx]==1){
                        lf = 1;
                     }else if((ly==8)&&((td[ly][lx-1]==1)||(td[ly][lx+1]==1))){
                        og.setColor(c2) ; cf = 1 ; og.fillRect(lx*30+72,ly*30+60,6,30);
                        og.fillOval(lx*30+69,ly*30+60,13,13);
                        ld = 3 ;
                     }else if(td[ly-1][lx]==1){
                        og.fillRect(lx*30+72,ly*30+60,6,30);
                        lf = 1;
                     }else if((td[ly-1][lx-1]==1)&&(td[ly-1][lx+1]==1)){
                        og.setColor(c2) ; cf = 1 ; og.fillRect(lx*30+72,ly*30+60,6,30);
                        og.fillOval(lx*30+69,ly*30+60,13,13);
                        ld = 3 ; 
                     }else if(td[ly-1][lx-1]==1){
                        og.fillArc(lx*30+60+12,ly*30+60+12,36,36,90,90);
                        og.setColor(c3) ; og.fillArc(lx*30+60+18,ly*30+60+18,24,24,90,90);
                        ld = 0;
                        if(cf == 0){
                           og.setColor(c1);
                        }else{
                           og.setColor(c2);
                        }
                     }else if(td[ly-1][lx+1]==1){
                        og.fillArc(lx*30+60-18,ly*30+60+12,36,36,0,90);
                        og.setColor(c3) ; og.fillArc(lx*30+60-12,ly*30+60+18,24,24,0,90);
                        ld = 2;
                        if(cf == 0){
                           og.setColor(c1);
                        }else{
                           og.setColor(c2);
                        }
                     }else{
                        og.fillRect(lx*30+72,ly*30+60,6,30);
                     }
                     break;
            case 2 : lx --;
                     if(lx == 0){
                        og.fillRect(lx*30+60,ly*30+72,30,6);lf = 1;
                     }else if(td[ly][lx]==1){
                        lf = 1;
                     }else if((lx==8)&&((td[ly-1][lx]==1)||(td[ly+1][lx]==1))){
                        og.setColor(c2) ; cf = 1 ; og.fillRect(lx*30+60,ly*30+72,30,6);
                        og.fillOval(lx*30+60,ly*30+69,13,13);
                        ld = 0 ;
                     }else if(td[ly][lx-1]==1){
                        og.fillRect(lx*30+60,ly*30+72,30,6);
                        lf = 1;
                     }else if((td[ly-1][lx-1]==1)&&(td[ly+1][lx-1]==1)){
                        og.setColor(c2) ; cf = 1 ; og.fillRect(lx*30+60,ly*30+72,30,6);
                        og.fillOval(lx*30+60,ly*30+69,13,13);
                        ld = 0 ; 
                     }else if(td[ly-1][lx-1]==1){
                        og.fillArc(lx*30+60+12,ly*30+60+12,36,36,90,90);
                        og.setColor(c3) ; og.fillArc(lx*30+60+18,ly*30+60+18,24,24,90,90);
                        ld = 3;
                        if(cf == 0){
                           og.setColor(c1);
                        }else{
                           og.setColor(c2);
                        }
                     }else if(td[ly+1][lx-1]==1){
                        og.fillArc(lx*30+60+12,ly*30+60-18,36,36,180,90);
                        og.setColor(c3) ; og.fillArc(lx*30+60+18,ly*30+60-12,24,24,180,90);
                        ld = 1;
                        if(cf == 0){
                           og.setColor(c1);
                        }else{
                           og.setColor(c2);
                        }
                     }else{
                        og.fillRect(lx*30+60,ly*30+72,30,6);
                     }
                     break;
            case 3 : ly ++;
                     if(ly == 9){
                        og.fillRect(lx*30+72,ly*30+60,6,30);lf = 1;
                     }else if(td[ly][lx]==1){
                        lf = 1;
                     }else if((ly==1)&&((td[ly][lx-1]==1)||(td[ly][lx+1]==1))){
                        og.setColor(c2) ; cf = 1 ; og.fillRect(lx*30+72,ly*30+60,6,30);
                        og.fillOval(lx*30+69,ly*30+78,13,13);
                        ld = 1 ;
                     }else if(td[ly+1][lx]==1){
                        og.fillRect(lx*30+72,ly*30+60,6,30);
                        lf = 1;
                     }else if((td[ly+1][lx-1]==1)&&(td[ly+1][lx+1]==1)){
                        og.setColor(c2) ; cf = 1 ; og.fillRect(lx*30+72,ly*30+60,6,30);
                        og.fillOval(lx*30+69,ly*30+78,13,13);
                        ld = 1 ; 
                     }else if(td[ly+1][lx-1]==1){
                        og.fillArc(lx*30+60+12,ly*30+60-18,36,36,180,90);
                        og.setColor(c3) ; og.fillArc(lx*30+60+18,ly*30+60-12,24,24,180,90);
                        ld = 0;
                        if(cf == 0){
                           og.setColor(c1);
                        }else{
                           og.setColor(c2);
                        }
                     }else if(td[ly+1][lx+1]==1){
                        og.fillArc(lx*30+60-18,ly*30+60-18,36,36,270,90);
                        og.setColor(c3) ; og.fillArc(lx*30+60-12,ly*30+60-12,24,24,270,90);
                        ld = 2;
                        if(cf == 0){
                           og.setColor(c1);
                        }else{
                           og.setColor(c2);
                        }
                     }else{
                        og.fillRect(lx*30+72,ly*30+60,6,30);
                     }
                     break;
         }
      }

      for(int k1 = 1;k1 < 9;k1 ++){
         for(int k2 = 1;k2 < 9;k2 ++){
            og.setColor(Color.gray);
            og.drawRect(k1*30+60,k2*30+60,30,30);
         }
      }

      if(hf == 1){
         for(int k1 = 1;k1 < 9;k1 ++){
            for(int k2 = 1;k2 < 9;k2 ++){
               og.setColor(Color.black);
               og.fillRect(k1*30+60,k2*30+60,30,30);
               og.setColor(Color.lightGray);
               og.drawRect(k1*30+60,k2*30+60,30,30);
               if(at[k2][k1] == 1)b_draw(k1*30+60,k2*30+60,0);
            }
         }
      }else{
         for(int k1 = 1;k1 < 9;k1 ++){
            for(int k2 = 1;k2 < 9;k2 ++){
               if((td[k2][k1] == 1)&&(at[k2][k1] == 1)){
                  b_draw(k1*30+60,k2*30+60,1);
               }else if(at[k2][k1] == 1){
                  b_draw(k1*30+60,k2*30+60,0);
               }
            }
         }
      }

      g.drawImage(os,0,0,this);
   }

   public void b_draw(int x,int y,int c){
      for(int k1 = 0;k1 < 15;k1 ++){
         switch(c){
            case 0 : og.setColor(new Color(150+k1*5,0,0));break;
            case 1 : og.setColor(new Color(0,0,150+k1*5));;break;
            case 2 : og.setColor(new Color(0,150+k1*5,0));break;
         }
         og.drawOval(x+k1,y+k1,30-2*k1,30-2*k1);
      }
   }

   public void b_set(){
      for(int k1 = 0;k1 < 10;k1 ++){
         for(int k2 = 0;k2 < 10;k2 ++){
            td[k2][k1] = -1;
            at[k2][k1] = -1;
         }
      }
      for(int k1 = 1;k1 < 9;k1 ++){
         for(int k2 = 1;k2 < 9;k2 ++){
            td[k2][k1] = 0;
            at[k2][k1] = 0;
         }
      }
      co = 0;
      while(co < 5){
         ds = (int)(64*Math.random());
         if(td[ds/8+1][ds%8+1] == 0){
            td[ds/8+1][ds%8+1] = 1;
            co ++;
         }
      }
   }

   public void start(){
      if(th == null){
         th = new Thread(this);
         th.start();
      }
   }

   public void stop(){
      th = null;
   }

   public void run(){
      while(th != null){
         try{
            Thread.sleep(100);
         }
         catch (InterruptedException e){
         }
      }
   }

   public void update( Graphics g ){
      paint( g ) ;
   }
}