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

public class Shift extends Applet implements Runnable{
 
   int bd[]={2,2,-1,-1,-1,0,1,1,1,2,2};
   int md[]={0,0,0,0,0,0,0,0,0,0,0};
   Image pic,mpic;
   int mx,my;
   int cx,cd;
   int cy = 35;
   int mdn,mun;
   Button rep = new Button("  replay  ");
   Thread th = null;
   Image os;
   Graphics og;

   public void init(){

      setBackground(Color.white);
      os = createImage(460,95);
      og = os.getGraphics();
      for(int k = 0;k < 11;k++)md[k]=bd[k];
      setLayout(new FlowLayout());
      add(rep);

   }

   public void paint(Graphics g){
      og.setColor(Color.white); 
      og.fillRect(0,0,460,95);
      og.setColor(Color.black); 
      og.drawLine(20,cy   ,20+60*7,cy   );
      og.drawLine(20,cy+45,20+60*7,cy+45);
      for(int i = 0;i < 8;i++)og.drawLine(20+60*i,cy,20+60*i,cy+45);
      for(int j = 2;j < 9;j++){
         if(md[j] == -1){
            pic = getImage(getDocumentBase(),"Picture/Right.gif");
         }else if(md[j] == 1){
            pic = getImage(getDocumentBase(),"Picture/Left.gif");
         }else{
            pic = getImage(getDocumentBase(),"");
         }
         og.drawImage(pic,20+60*(j-2),cy,this);
         og.drawImage(mpic,mx,my,this);
      }
      g.drawImage(os,0,0,this);
   }

   public boolean mouseDown(Event e,int x,int y){

      mdn = (x - 20) / 60 + 2;

      if(md[mdn] == -1){
         mpic = getImage(getDocumentBase(),"Picture/Right.gif");
         cx = md[mdn];
         cd = -1;
         md[mdn] = 0;
      }else if(md[mdn] == 1){
         mpic = getImage(getDocumentBase(),"Picture/Left.gif"); 
         cx = md[mdn];
         cd = 1;
         md[mdn] = 0;
      }else{
         mpic = getImage(getDocumentBase(),"");
         cx = 0;
      }

      mx = x - 30;
      my = y - 23;

      repaint();
      return true;
   }

   public boolean mouseDrag(Event e,int x,int y){
      mx = x-30;
      my = y-23;
      repaint();
      return true;  
   }  
  
   public boolean mouseUp(Event e,int x,int y){

      mun = (x - 20) / 60 + 2;

      if(cd == -1){
         if((mdn+1 == mun)&&(md[mun] == 0)){
            md[mun] = -1;
            mx = (mun - 2) * 60 + 20;
            my = cy;
         }else if((mdn+2 == mun)&&(md[mdn+1] == 1)&&(md[mun] == 0)){
            md[mun] = -1;
            mx = (mun - 2) * 60 + 20;
            my = cy;
         }else{
            md[mdn] = cx;
         } 
      }else if(cd == 1){
         if((mdn-1 == mun)&&(md[mun] == 0)){
            md[mun] = 1;
            mx = (mun - 2) * 60 + 20;
            my = cy;
         }else if((mdn-2 == mun)&&(md[mdn-1] == -1)&&(md[mun] == 0)){
            md[mun] = 1;
            mx = (mun - 2) * 60 + 20;
            my = cy;
         }else{
            md[mdn] = cx;
         } 
      }else{
         md[mdn] = cx;     
      }
      mpic = getImage(getDocumentBase(),"");
      repaint();
      return true;
   }

   public boolean action(Event e,Object o){
      if(e.target == rep){
         for(int k = 0;k < 11;k++)md[k]=bd[k];       
         repaint();
         return true;
      }
      return false;
   }
   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 ) ;
   }
}