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

public class stuff_s extends Applet implements Runnable{

   Thread th = null;
   Image os;
   Graphics og;
   double pi = 3.141592;
   int td[] = {1,1,1,1,0};
   int tx[] = {-40,20,40,-20,-40},ty[] = {160,160,140,140,160};
   int dx[] = new int[5],dy[] = new int[5];
   int px,sw,sp=4;
   int mx,my,md;
   Button sel = new Button("   >>>    ");

   public void init(){
      os = createImage(360,180);
      og = os.getGraphics();
      add(sel);

      addMouseListener(
         new MouseAdapter(){
            public void mousePressed(MouseEvent e){
               if(sw == 0){
                  if((e.getX()>30)&&(e.getX()<330)&&(e.getY()>90)&&(e.getY()<150)){
                     px = (e.getX()-30)/60;
                     if((td[px] != 0)&&(Math.abs(sp-px) != 1)){
                        mx = px * 60 + 60;
                        my = 120;
                        sw = 1;
                        repaint();
                     }
                  }
               }
            }
         } 
      );
      sel.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
            td[sp] = 1;
            sp = sp + 1;
            if(sp > 4)sp = 1;
            td[sp] = 0;
            for(int d1 = 0;d1 < 5;d1 ++){
               if(td[d1] == -1)td[d1] = 1;
            }
            repaint();
         }
      });
   }

   public void paint(Graphics g){
      og.setColor(new Color(235,255,255));
      og.fillRect(0,0,360,180);

      for(int d1 = 0;d1 < 5;d1 ++){
         dx[d1] = tx[d1];
         dy[d1] = ty[d1];
      }
      for(int d1 = 0;d1 < 5;d1 ++){
         for(int d2 = 0;d2 < 5;d2 ++){
            dx[d2] = dx[d2]+60;
         }
         og.setColor(Color.green);
         og.fillPolygon(dx,dy,5);
         og.setColor(Color.black);
         og.drawPolygon(dx,dy,5);
      }

      if(sw == 0){
         for(int d1 = 0;d1 < 5;d1 ++){
            if(td[d1] != 0){
               if(td[d1] == 1){
                  draw_b(d1*60+60,120,0);
               }else if(td[d1] == -1){
                  draw_b(d1*60+60,120,180);
               }
            }   
         }
      }else{
         if(td[px] == 1){
            draw_b(mx,my,0);
         }else{
            draw_b(mx,my,180);
         }
         for(int d1 = 0;d1 < 5;d1 ++){
            if((px-d1)*(sp-d1) < 0){
               md = (360 + (int)(Math.atan2(mx-(d1*60+60),my-120)*180/pi)) % 360 - 90;
               if(px < sp){
                  if(td[d1] == 1){
                     draw_b(d1*60+60,120,md-180);
                  }else if(td[d1] == -1){
                     draw_b(d1*60+60,120,md);
                  }
               }else{
                  if(td[d1] == 1){
                     draw_b(d1*60+60,120,md);
                  }else if(td[d1] == -1){
                     draw_b(d1*60+60,120,180-md);
                  }
               }
            }else if((px-d1)*(sp-d1) > 0){
               if(td[d1] == 1){
                  draw_b(d1*60+60,120,0);
               }else if(td[d1] == -1){
                  draw_b(d1*60+60,120,180);
               }
            }
         }
      }
      if((td[1]==1)&&(td[2]==1)&&(td[3]==1)&&(td[4]==1)){
         og.setColor(Color.red);
         og.setFont(new Font("",Font.BOLD,48));
         og.drawString("clear!!",120,80);
      }
      g.drawImage(os,0,0,this);
   }

   public void draw_b(int dx,int dy,int dr){
      og.setColor(Color.white);
      og.fillOval(dx-30,dy-30,60,60);
      og.setColor(Color.black);
      og.drawOval(dx-30,dy-30,60,60);
      og.setColor(Color.yellow);
      og.fillOval(dx+(int)(15*Math.cos(dr*pi/180))-15,dy-(int)(15*Math.sin(dr*pi/180))-15,30,30);
      og.setColor(Color.black);
      og.drawOval(dx+(int)(15*Math.cos(dr*pi/180))-15,dy-(int)(15*Math.sin(dr*pi/180))-15,30,30);
      og.fillOval(dx+(int)(25*Math.cos(dr*pi/180))-5,dy-(int)(25*Math.sin(dr*pi/180))-5,10,10);
   }

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

   public void stop(){
      th = null;
   }

   public void run(){
      while(th != null){
         try{
            switch(sw){
               case 1 : my = my - 3;
                        if(my < 60){
                           my = 60;
                           sw = 2;
                        }
                        break;
               case 2 : if(px < sp){
                           mx = mx + 3;
                           if(mx > sp * 60 + 60){
                              mx = sp * 60 + 60;
                              sw = 3;
                           }
                        }else{
                           mx = mx - 3;
                           if(mx < sp * 60 + 60){
                              mx = sp * 60 + 60;
                              sw = 3;
                           }
                        }
                        break;
               case 3 : my = my + 3;
                        if(my > 120){
                           my = 120;
                           td[sp] = td[px];
                           if(px < sp){
                              for(int t1 = px + 1;t1 < sp;t1 ++){
                                 td[t1] = td[t1] * (-1);
                              }
                           }else{
                              for(int t1 = sp + 1;t1 < px;t1 ++){
                                 td[t1] = td[t1] * (-1);
                              }
                           }
                           td[px] = 0;sp = px;
                           sw = 0;
                        }
                        break;
            }
            Thread.sleep(10);
         }
         catch (InterruptedException e){
         }
         repaint();
      }
   }

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