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

public class Spot extends Applet implements Runnable{

   int pt[][][] = {{{0,0,0,0},{0,0,0,1},{0,0,0,1}},
                   {{0,1,0,1},{0,1,0,1},{0,1,1,0}},
                   {{0,1,1,1},{0,1,1,1},{1,1,1,1}}};
   int fg = 0;
   int px1,py1,px2,py2;
   Graphics g;
   int st[] = new int[4];
   int cs1,cs2,cs3,cs4;
   Thread th = null;
   Image os;
   Graphics og;

   public void init(){
      os = createImage(330,330);
      og = os.getGraphics();
      setBackground(Color.white);
      addMouseListener(
         new MouseAdapter(){
            public void mousePressed(MouseEvent e){
               px1 = (e.getX() - 25) / 90;
               py1 = (e.getY() - 25) / 90;
               if(px1 > 2)px1 = 2;
               if(py1 > 2)py1 = 2;
               if(fg == 0){
                  px2 = px1 ; py2 = py1 ; fg = 1;
                  repaint();
               }else if((px1 != px2) || (py1 != py2)){
                  for(int s = 0;s < 4;s ++){
                     st[s] = pt[py1][px1][s];
                     pt[py1][px1][s] = pt[py2][px2][s];
                     pt[py2][px2][s] = st[s];
                     fg = 0;
                     repaint();
                  }
               }else if((px1 == px2) || (py1 == py2)){
                  st[0] = pt[py1][px1][0];
                  pt[py1][px1][0] = pt[py1][px1][1];
                  pt[py1][px1][1] = pt[py1][px1][3];
                  pt[py1][px1][3] = pt[py1][px1][2];
                  pt[py1][px1][2] = st[0];
                  fg = 0;
                  repaint();
               }
            }
         }
      );
      
   }

   public void paint(Graphics g){
      og.setColor(Color.white); 
      og.fillRect(0,0,330,330);
      og.setColor(Color.red);
      for(int c1 = 0;c1 < 3;c1 ++){
         cs1 = 0 ; cs2 = 0 ; cs3 = 0 ; cs4 = 0;
         cs1 = pt[c1][0][0] + pt[c1][0][2] + pt[c1][1][0] + pt[c1][1][2] + pt[c1][2][0] + pt[c1][2][2];
         cs2 = pt[c1][0][1] + pt[c1][0][3] + pt[c1][1][1] + pt[c1][1][3] + pt[c1][2][1] + pt[c1][2][3];
         if(cs1 == 3)og.drawLine(20,c1 * 90 + 50,300,c1 * 90 + 50);
         if(cs2 == 3)og.drawLine(20,c1 * 90 + 90,300,c1 * 90 + 90);
         cs3 = pt[0][c1][0] + pt[0][c1][1] + pt[1][c1][0] + pt[1][c1][1] + pt[2][c1][0] + pt[2][c1][1];
         cs4 = pt[0][c1][2] + pt[0][c1][3] + pt[1][c1][2] + pt[1][c1][3] + pt[2][c1][2] + pt[2][c1][3];
         if(cs3 == 3)og.drawLine(c1 * 90 + 50,20,c1 * 90 + 50,300);
         if(cs4 == 3)og.drawLine(c1 * 90 + 90,20,c1 * 90 + 90,300);
      }
      cs1 = 0;
      cs1 = pt[0][0][0] + pt[0][0][3] + pt[1][1][0] + pt[1][1][3] + pt[2][2][0] + pt[2][2][3];
      if(cs1 == 3)og.drawLine(20,20,300,300);
      cs2 = 0;
      cs1 = pt[0][2][2] + pt[0][2][1] + pt[1][1][2] + pt[1][1][1] + pt[2][0][2] + pt[2][0][1];
      if(cs1 == 3)og.drawLine(20,300,300,20);
      for(int iy = 0;iy < 3;iy ++){
         for(int ix = 0;ix < 3;ix ++){
            og.setColor(Color.yellow);
            og.fill3DRect(ix * 90 + 30,iy * 90 + 30,80,80,true);
            og.setColor(Color.blue);
            for(int k = 0;k < 4;k ++){
               if(pt[iy][ix][k] == 1){
                  og.fillOval(ix * 90 + 35 + (k / 2) * 40,iy * 90 + 35 + (k % 2) * 40,30,30);
               }
            }
         }
      }
      if(fg == 1){
         og.setColor(Color.blue);
         og.draw3DRect(px1 * 90 + 30,py1 * 90 + 30,80,80,true);
      }
    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 ) ;
   }
}