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

public class t_over extends Applet implements Runnable{

    Thread th = null;
    double pi = 3.1415;
    int mtd[][] = new int [5][5];
    double st = 0;
    int swd = -1,swl = 0,swt = 0;
    Color co[] = new Color[2];
    int dx,dy;
    int wid = 40,sp = 30;
    Image os;
    Graphics og;
    int tn = 3;
    int ch;
    Button rep = new Button("  replay  ");
    Choice cho = new Choice();

    public void init(){
        os = createImage(260,280);
        og = os.getGraphics();
        co[0] = new Color(255,0,128);
        co[1] = new Color(0,128,255);
        setBackground(Color.white);
        addMouseListener(
            new MouseAdapter(){
                public void mousePressed(MouseEvent e){
                    dx = (e.getX() - sp + wid - (5 - tn) * 20) / wid;
                    dy = (e.getY() - sp + wid - (5 - tn) * 20) / wid;
                    if((dx == 0) || (dx == tn + 1)){
                        swd = -1;swl = dy - 1;
                        th.resume();
                    }
                    if((dy == 0) || (dy == tn + 1)){
                        swd = 1;swl = dx - 1;
                        th.resume();
                    }
                }
            }
        );
        setLayout(new BorderLayout());
        Panel pan = new Panel();
        pan.setLayout(new FlowLayout());
        pan.add(cho);
        pan.add(rep = new Button("  replay  "));
        cho.addItem("  3~3  ");
        cho.addItem("  4~4  ");
        cho.addItem("  5~5  ");
        cho.addItemListener(new MyListener());
        add("South",pan);
        rep.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                R_set();
            repaint();
            }
        });
    }

    class MyListener implements ItemListener{
        public void itemStateChanged(ItemEvent e){
            Choice cho = (Choice)e.getSource();
            switch(cho.getSelectedIndex()){
                case 0 : tn = 3;R_set();repaint();break;
                case 1 : tn = 4;R_set();repaint();break;
                case 2 : tn = 5;R_set();repaint();break;
            }
        }
    }

    public void paint(Graphics g){

        og.setColor(Color.white); 
        og.fillRect(0,0,260,280);
        for(int ix = 0;ix < tn;ix ++){
            for(int iy = 0;iy < tn;iy ++){
                if(swd == 1){
                    if(ix != swl){
                        og.setColor(co[mtd[iy][ix]]);
                        og.fillRect(ix * wid + sp + (5 - tn) * 20,iy * wid + sp + (5 - tn) * 20,wid,wid);
                        og.setColor(Color.black);
                        og.drawRect(ix * wid + sp + (5 - tn) * 20,iy * wid + sp + (5 - tn) * 20,wid,wid);
                    }
                }else{
                    if(iy != swl){
                        og.setColor(co[mtd[iy][ix]]);
                        og.fillRect(ix * wid + sp + (5 - tn) * 20,iy * wid + sp + (5 - tn) * 20,wid,wid);
                        og.setColor(Color.black);
                        og.drawRect(ix * wid + sp + (5 - tn) * 20,iy * wid + sp + (5 - tn) * 20,wid,wid);
                    }
                }
            }
        }
        if(swd == 1){
            for(int kt = 0; kt < tn;kt ++){
                og.setColor(co[mtd[kt][swl]]);
                og.fillRect(swl * wid + sp + (5 - tn) * 20,(wid * tn) / 2 + sp + (int)(Math.round((wid * (kt - tn / 2) - (tn % 2) * (wid / 2)) * Math.cos(st))) + (5 - tn) * 20,wid,(int)(Math.round(wid * Math.cos(st))));
                og.setColor(Color.black);
                og.drawRect(swl * wid + sp + (5 - tn) * 20,(wid * tn) / 2 + sp + (int)(Math.round((wid * (kt - tn / 2) - (tn % 2) * (wid / 2)) * Math.cos(st))) + (5 - tn) * 20,wid,(int)(Math.round(wid * Math.cos(st))));
            }
        }else{
            for(int kt = 0; kt < tn;kt ++){
                og.setColor(co[mtd[swl][kt]]);
                og.fillRect((wid * tn) / 2 + sp + (int)(Math.round((wid * (kt - tn / 2) - (tn % 2) * (wid / 2)) * Math.cos(st))) + (5 - tn) * 20,swl * wid + sp + (5 - tn) * 20,(int)(Math.round(wid * Math.cos(st))),wid);
                og.setColor(Color.black);
                og.drawRect((wid * tn) / 2 + sp + (int)(Math.round((wid * (kt - tn / 2) - (tn % 2) * (wid / 2)) * Math.cos(st))) + (5 - tn) * 20,swl * wid + sp + (5 - tn) * 20,(int)(Math.round(wid * Math.cos(st))),wid);
            }
        }
        g.drawImage(os,0,0,this);
    }

    public void R_set(){
        for(int ix = 0;ix < tn;ix ++){
            for(int iy = 0;iy < tn;iy ++){
                 mtd[iy][ix] = 0;
                 swl = 0;
            }
        }
       return;
    }
    public void start(){    
        if(th == null){
            th = new Thread(this);
            th.start();
        }
        th.suspend();
    }
    public void stop(){
        th = null;
    }
    public void run(){
        while (th != null){
            try {
                if(swt == 0){
                    st = st + pi / 50;
                    if(st > pi / 2){
                        if(swd == 1){
                            for(int cd = 0;cd < (tn / 2 + tn % 2);cd ++){
                               ch = mtd[cd][swl];
                               mtd[cd][swl] = (mtd[tn - cd - 1][swl] + 1) % 2;
                               mtd[tn - cd - 1][swl] = (ch + 1) % 2;
                            }
                        }else{
                            for(int cd = 0;cd < (tn / 2 + tn % 2);cd ++){
                               ch = mtd[swl][cd];
                               mtd[swl][cd] = (mtd[swl][tn - cd - 1] + 1) % 2;
                               mtd[swl][tn - cd - 1] = (ch + 1) % 2;
                            }
                        }
                        swt = 1;
                    }
                }else{
                    st = st - pi / 50;
                    if(st < 0){
                        st = 0;
                        swt = 0;
                        th.suspend();
                    }
                }
                Thread.sleep(30);
            }
            catch (InterruptedException e){ }
            repaint();
        }
    }
    public void update(Graphics g){
        paint(g) ;
    }
}