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

public class r_chart extends Applet implements Runnable{

   Thread th = null;
   Image os;
   Graphics og;
   double pi = 3.141592;
   int td[][][] = {{{2,3},{4,4}},
                   {{6,2},{4,3}},
                   {{5,5},{3,2}},
                   {{3,5},{2,4}},
                   {{2,4},{3,5}},
                   {{3,4},{5,2}},
                   {{5,5},{2,4}},
                   {{2,6},{4,4}}};
   int px[] = new int[5];
   int py[] = new int[5];
   double kt;
   int pn,sn,sf,sw,af;

   public void init(){
      os = createImage(320,320);
      og = os.getGraphics();
      sf = 1;

      addMouseListener(
         new MouseAdapter(){
            public void mousePressed(MouseEvent e){
               pn = (13-(int)((Math.atan2(e.getX()-160,e.getY()-160)+pi)*4/pi))%8;
               if(sf == 1){
                  sn = pn;
                  sf = 2;
               }else{
                  if(pn == sn){
                     sw = td[pn][0][0] ; td[pn][0][0] = td[pn][1][1] ; td[pn][1][1] = sw ;
                     sw = td[pn][0][1] ; td[pn][0][1] = td[pn][1][0] ; td[pn][1][0] = sw ;
                     sf = 1;
                  }else{
                     sw = td[pn][0][0] ; td[pn][0][0] = td[sn][0][0] ; td[sn][0][0] = sw ;
                     sw = td[pn][0][1] ; td[pn][0][1] = td[sn][0][1] ; td[sn][0][1] = sw ;
                     sw = td[pn][1][0] ; td[pn][1][0] = td[sn][1][0] ; td[sn][1][0] = sw ;
                     sw = td[pn][1][1] ; td[pn][1][1] = td[sn][1][1] ; td[sn][1][1] = sw ;
                     sf = 1;
                  }
               }
               repaint();
            }
         } 
      );

   }

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

      for(int rt = 0;rt < 8;rt ++){
         og.setColor(new Color(255,190,190));
         kt = Math.atan((td[rt][1][0]*Math.sin(rt*pi/4)-td[rt][1][1]*Math.sin((rt+1)*pi/4))/(td[rt][1][0]*Math.cos(rt*pi/4)-td[rt][1][1]*Math.cos((rt+1)*pi/4)));
         px[0] = 160+(int)(20*td[rt][1][0]*Math.cos(rt*pi/4))+(int)(3*Math.cos(kt+pi/2));
         py[0] = 160+(int)(20*td[rt][1][0]*Math.sin(rt*pi/4))+(int)(3*Math.sin(kt+pi/2));
         px[1] = 160+(int)(20*td[rt][1][0]*Math.cos(rt*pi/4))+(int)(3*Math.cos(kt-pi/2));
         py[1] = 160+(int)(20*td[rt][1][0]*Math.sin(rt*pi/4))+(int)(3*Math.sin(kt-pi/2));
         px[2] = 160+(int)(20*td[rt][1][1]*Math.cos((rt+1)*pi/4))+(int)(3*Math.cos(kt-pi/2));
         py[2] = 160+(int)(20*td[rt][1][1]*Math.sin((rt+1)*pi/4))+(int)(3*Math.sin(kt-pi/2));
         px[3] = 160+(int)(20*td[rt][1][1]*Math.cos((rt+1)*pi/4))+(int)(3*Math.cos(kt+pi/2));
         py[3] = 160+(int)(20*td[rt][1][1]*Math.sin((rt+1)*pi/4))+(int)(3*Math.sin(kt+pi/2));
         px[4] = px[0] ; py[4] = py[0] ;
         og.fillPolygon(px,py,5);
      }
      for(int rt = 0;rt < 8;rt ++){

         og.setColor(Color.lightGray);
         for(int yl = 2;yl < 7;yl ++){
            og.drawLine(160+(int)(20*yl*Math.cos(rt*pi/4)),160+(int)(20*yl*Math.sin(rt*pi/4)),
                        160+(int)(20*yl*Math.cos((rt+1)*pi/4)),160+(int)(20*yl*Math.sin((rt+1)*pi/4)));
         }
         og.setColor(Color.black);
         og.drawLine(160+(int)(20*Math.cos(rt*pi/4)),160+(int)(20*Math.sin(rt*pi/4)),
                     160+(int)(140*Math.cos(rt*pi/4)),160+(int)(140*Math.sin(rt*pi/4)));
         og.drawLine(160+(int)(20*Math.cos(rt*pi/4)),160+(int)(20*Math.sin(rt*pi/4)),
                     160+(int)(20*Math.cos((rt+1)*pi/4)),160+(int)(20*Math.sin((rt+1)*pi/4)));
         og.drawLine(160+(int)(140*Math.cos(rt*pi/4)),160+(int)(140*Math.sin(rt*pi/4)),
                     160+(int)(140*Math.cos((rt+1)*pi/4)),160+(int)(140*Math.sin((rt+1)*pi/4)));
         og.setColor(Color.red);
         kt = Math.atan((td[rt][0][0]*Math.sin(rt*pi/4)-td[rt][0][1]*Math.sin((rt+1)*pi/4))/(td[rt][0][0]*Math.cos(rt*pi/4)-td[rt][0][1]*Math.cos((rt+1)*pi/4)));
         px[0] = 160+(int)(20*td[rt][0][0]*Math.cos(rt*pi/4))+(int)(3*Math.cos(kt+pi/2));
         py[0] = 160+(int)(20*td[rt][0][0]*Math.sin(rt*pi/4))+(int)(3*Math.sin(kt+pi/2));
         px[1] = 160+(int)(20*td[rt][0][0]*Math.cos(rt*pi/4))+(int)(3*Math.cos(kt-pi/2));
         py[1] = 160+(int)(20*td[rt][0][0]*Math.sin(rt*pi/4))+(int)(3*Math.sin(kt-pi/2));
         px[2] = 160+(int)(20*td[rt][0][1]*Math.cos((rt+1)*pi/4))+(int)(3*Math.cos(kt-pi/2));
         py[2] = 160+(int)(20*td[rt][0][1]*Math.sin((rt+1)*pi/4))+(int)(3*Math.sin(kt-pi/2));
         px[3] = 160+(int)(20*td[rt][0][1]*Math.cos((rt+1)*pi/4))+(int)(3*Math.cos(kt+pi/2));
         py[3] = 160+(int)(20*td[rt][0][1]*Math.sin((rt+1)*pi/4))+(int)(3*Math.sin(kt+pi/2));
         px[4] = px[0] ; py[4] = py[0] ;
         og.fillPolygon(px,py,5);
      }
      if(sf == 2){
         og.setColor(Color.blue);
         px[0] = 160+(int)(20*Math.cos(sn*pi/4));
         py[0] = 160+(int)(20*Math.sin(sn*pi/4));
         px[1] = 160+(int)(140*Math.cos(sn*pi/4));
         py[1] = 160+(int)(140*Math.sin(sn*pi/4));
         px[2] = 160+(int)(140*Math.cos((sn+1)*pi/4));
         py[2] = 160+(int)(140*Math.sin((sn+1)*pi/4));
         px[3] = 160+(int)(20*Math.cos((sn+1)*pi/4));
         py[3] = 160+(int)(20*Math.sin((sn+1)*pi/4));
         px[4] = px[0] ; py[4] = py[0] ;
         og.drawPolygon(px,py,5);
      }

      af = 1;
      for(int ch = 0;ch < 7;ch ++){
         if(td[ch][0][1] != td[ch+1][0][0])af = 0;
      }
      if(td[7][0][1] != td[0][0][0])af = 0;
      og.setFont(new Font("",Font.BOLD,36));
      og.setColor(Color.blue);
      if(af == 1)og.drawString("clear!!",100,290);

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

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