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

public class seesaw extends Applet implements Runnable{

   int td[][] = {{-1,-1,-1,-1,-1, 0, 6,-1,-1,-1,-1,-1},
                 {-1,-1,-1,-1, 0, 0, 6, 5,-1,-1,-1,-1},
                 {-1,-1,-1, 0, 0, 0, 6, 5, 4,-1,-1,-1},
                 {-1,-1, 0, 0, 0, 0, 6, 5, 4, 3,-1,-1},
                 {-1, 0, 0, 0, 0, 0, 6, 5, 4, 3, 2,-1},
                 { 0, 0, 0, 0, 0, 0, 6, 5, 4, 3, 2, 1}};
   int tn[][] = {{1,2,3,4,5,6},{1,2,3,4,5,6}};
   int ro[] = new int[6];
   int rn,fn;
   int sw = 1;
   int px,py;
   double ds = 3.14159263 / 72;
   Thread th = null;
   Image ar[] = new Image[3];
   Image os;
   Graphics og;

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

      ar[0] = getImage(getDocumentBase(),"Picture/ar8.gif");
      ar[1] = getImage(getDocumentBase(),"Picture/ar4.gif");
      ar[2] = getImage(getDocumentBase(),"Picture/ar1.gif");

      addMouseListener(
         new MouseAdapter(){
            public void mousePressed(MouseEvent e){
               px = e.getX();
               py = e.getY();
               if((py > 220)&&(py < 265)){
                  if((px > 20)&&(px < 70)){
                     sw = -1;
                  }
                  if((px > 350)&&(px < 400)){
                     sw = 1;
                  }
                  if((px > 95)&&(px < 145)){
                     for(int r1 = 0;r1 < 6;r1 ++){
                        ro[r1] = td[5][r1];
                     }
                     rn = tn[0][5];
                     for(int r2 = 0;r2 < 5;r2 ++){
                        for(int r1 = 0;r1 < 6;r1 ++){
                           td[5-r2][r1] = td[4-r2][r1];
                        }
                        tn[0][5-r2] = tn[0][4-r2];
                     }
                     for(int r1 = 0;r1 < 6;r1 ++){
                         td[0][r1] = ro[r1];
                     }
                     tn[0][0] = rn;
                  }
                  if((px > 275)&&(px < 325)){
                     for(int r1 = 0;r1 < 6;r1 ++){
                        ro[r1] = td[5][r1+6];
                     }
                     rn = tn[1][5];
                     for(int r2 = 0;r2 < 5;r2 ++){
                        for(int r1 = 0;r1 < 6;r1 ++){
                           td[5-r2][r1+6] = td[4-r2][r1+6];
                        }
                        tn[1][5-r2] = tn[1][4-r2];
                     }
                     for(int r1 = 0;r1 < 6;r1 ++){
                         td[0][r1+6] = ro[r1];
                     }
                     tn[1][0] = rn;
                  }
               }

               if(sw == -1){
                  for(int c1 = 0;c1 < 6;c1 ++){
                     fn = 6-tn[0][c1];
                     for(int c2 = 0;c2 < 12;c2 ++){
                        if((td[c1][c2] != 0)&&(td[c1][c2] != -1)){
                           td[c1][fn] = td[c1][c2];
                           if(fn != c2)td[c1][c2] = 0;
                           fn ++;
                        }
                     }
                  }
               }else if(sw == 1){
                  for(int c1 = 0;c1 < 6;c1 ++){
                     fn = 5+tn[1][c1];
                     for(int c2 = 0;c2 < 12;c2 ++){
                        if((td[c1][11-c2] != 0)&&(td[c1][11-c2] != -1)){
                           td[c1][fn] = td[c1][11-c2];
                           if(fn != 11-c2)td[c1][11-c2] = 0;
                           fn --;
                        }
                     }
                  }
               }
               repaint();
            }
         }
      );
   }

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

      for(int w1 = 0;w1 < 6;w1 ++){
         og.setColor(Color.blue);
         og.drawLine(tx(210,30*w1+30),ty(210,30*w1+30),tx(210+30*tn[1][w1],30*w1+30),ty(210+30*tn[1][w1],30*w1+30));
         og.drawLine(tx(210+30*tn[1][w1],30*w1+30),ty(210+30*tn[1][w1],30*w1+30),tx(210+30*tn[1][w1],30*w1+60),ty(210+30*tn[1][w1],30*w1+60));
         og.drawLine(tx(210+30*tn[1][w1],30*w1+60),ty(210+30*tn[1][w1],30*w1+60),tx(210,30*w1+60),ty(210,30*w1+60));
         og.drawLine(tx(210,30*w1+30),ty(210,30*w1+30),tx(210-30*tn[0][w1],30*w1+30),ty(210-30*tn[0][w1],30*w1+30));
         og.drawLine(tx(210-30*tn[0][w1],30*w1+30),ty(210-30*tn[0][w1],30*w1+30),tx(210-30*tn[0][w1],30*w1+60),ty(210-30*tn[0][w1],30*w1+60));
         og.drawLine(tx(210-30*tn[0][w1],30*w1+60),ty(210-30*tn[0][w1],30*w1+60),tx(210,30*w1+60),ty(210,30*w1+60));
      }
      og.setColor(Color.red);
      og.drawLine(tx(210,15),ty(210,15),tx(210,210),ty(210,210));
      og.drawLine(tx(209,15),ty(209,15),tx(209,210),ty(209,210));
      og.drawLine(tx(15,210),ty(15,210),tx(405,210),ty(405,210));
      og.drawLine(tx(15,211),ty(15,211),tx(405,211),ty(405,211));
      og.setColor(Color.black);
      og.drawLine(210,210,240,240);
      og.drawLine(240,240,180,240);
      og.drawLine(180,240,210,210);
      for(int b1 = 0;b1 < 12;b1 ++){
         for(int b2 = 0;b2 < 6;b2 ++){
            if(td[b2][b1] > 0){
               for(int cd = 0;cd < 14;cd ++){
                  switch(td[b2][b1]){
                     case 1 : og.setColor(new Color(255,100,230-cd*10)) ; break ;
                     case 2 : og.setColor(new Color(230-cd*10,255,100)) ; break ;
                     case 3 : og.setColor(new Color(100,230-cd*10,255)) ; break ;
                     case 4 : og.setColor(new Color(255,255,230-cd*10)) ; break ;
                     case 5 : og.setColor(new Color(255,230-cd*10,255)) ; break ;
                     case 6 : og.setColor(new Color(230-cd*10,255,255)) ; break ;
                  }
                  og.drawOval(tx(30*b1+45,30*b2+45)-cd,ty(30*b1+45,30*b2+45)-cd,2*cd,2*cd);
               }
            }
         }
      }
      og.drawImage(ar[0],23,220,this);
      og.drawImage(ar[0],353,220,this);
      og.drawImage(ar[1],98,220,this);
      og.drawImage(ar[2],278,220,this);
      g.drawImage(os,0,0,this);
   }

   public int tx(int ix,int iy){
      int rx;
      int x = ix;
      int y = iy;
      rx = (int)(x*Math.cos(ds*sw)-y*Math.sin(ds*sw)+210*(Math.sin(ds*sw)-Math.cos(ds*sw)+1));
      return rx;
   }
   public int ty(int ix,int iy){
      int ry;
      int x = ix;
      int y = iy;
      ry = (int)(x*Math.sin(ds*sw)+y*Math.cos(ds*sw)-210*(Math.sin(ds*sw)+Math.cos(ds*sw)-1));
      return ry;
   }

   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 ) ;
   }
}