import java.applet.*;
import java.awt.*;

public class keyframe extends Applet implements Runnable{
    Thread thread;
    int delay = 100;
    Dimension d;
    Image offs;
    Graphics grf;

    int kfdatax[][][] = {
	//a1 - T 
	{{100,120,120,140,120,120,120,120},   // TOKYO
        {244,237,237,220,220,213,213,244},    // Tokyo Tower 
        {236,209,245,242,245,270,248,236},   // CN Tower
        {227,234,241,245,245,224,224,245}},  // EIFFER
 
	//a2 - O
	{{160,200,200,160,160},
 	{217,240,240,217,217},
	{236,240,240,246,236},
        {224,249,260,209,224}},
          
	//a3 - K
	{{220,220,220,220,220,260,220,220,260},
	{210,142,154,194,261,299,308,247,207},   
	{225,260,261,267,267,259,251,233,223},
        {260,242,228,211,201,201,270,270,201}},

	//a4 - Y
	{{280,300,300,300,300,300,300,320,300,300},
	{237,230,230,230,230,230,230,220,220,237},  
	{235,237,232,234,249,250,247,249,247,237},
        {174,122,159,187,277,310,347,295,174,122}},

	//a5 - O
	{{340,380,380,340,340},
	{202,254,254,202,202},  
	{225,225,217,217,223},
        {205,266,289,180,205}},

        //a6 - punto
        {{100,380,380,100,100,380},
        {420,430,400,440,410,420},
        {420,430,400,440,410,420},
        {170,299,299,170,170,299}}
    };
    
	int kfdatay[][][] = {
	//b1 - T
	{{190,190,190,190,190,240,240,290},
	{296,176,176,176,176,296,296,296},   
	{175,460,460,175,460,460,175,175},
        {59,24,59,59,70,70,59,59}},

	//b2 - O
	{{190,190,290,290,190},
        {170,170,176,176,170},
	{86,20,20,86,86},
        {307,307,360,360,307}},

	//b3 - K
	{{190,240,240,290,240,190,240,240,290},
	{312,450,450,400,400,450,450,312,312},  
	{142,142,149,153,166,175,175,175,175},
        {274,70,70,274,274,286,286,274,274}},

	//b4 - Y
	{{190,240,240,290,290,240,240,190,240,290},
	{170,58,58,34,34,58,58,170,170,170},   
	{142,93,92,86,86,92,93,142,93,93},
        {377,462,462,402,402,462,462,377,377,462}},

	//b5 - O
	{{190,190,290,290,190},
	{296,296,312,312,296},   
	{142,149,153,166,175},
        {286,286,362,362,286}},

        //b6 - punto
        {{310,310,310,310,310,310},
        {20,50,30,30,50,20},
        {20,50,30,30,50,20},
        {362,362,377,377,362,362}}
    };

    int point_max = 0x10;
    int point = 0x35;    
    final int divide_min = 10;
    final int divide_max = 50;
    int count_max = 40;  // cantidad de frames total de datos
    int count = 0;
    int num_max = 4; // 4  cantidad de frames
    int num = 0;

    Panel pp;
    Button btn1, btn2;
    Label lab;
    TextField text;
    int textlen = 4;  //4
    int flag = 0;

    public void init(){
        d = size();
        offs = createImage(d.width, d.height);
        grf = offs.getGraphics();

        init_panel();

        grf.setFont(new Font("Curier", Font.PLAIN, 24));

        init_screen();
        makeimage();
    }

    void init_panel(){
        pp = new Panel();

        btn1 = new Button("start/stop");
        pp.add(btn1);

        btn2 = new Button("submit");
        pp.add(btn2);

        lab = new Label("divide = ");
        pp.add(lab);

        text = new TextField(Integer.toString(count_max), textlen);
        pp.add(text);

        setLayout(new BorderLayout());
        add("South", pp);
    }

    public void start(){
        if(thread == null){
            thread = new Thread(this);
            thread.start();
        }
    }

    public void stop(){
        if(thread != null){
            thread.stop();
            thread = null;
        }
    }

    public void run(){
        while(true){
            try{
                Thread.sleep(delay);
            }catch (InterruptedException e){
                break;
            }

            if(flag == 1){
                if(count <= count_max){
                    init_screen();
                    makeimage();
                    repaint();
                    count++;
                }else{
                    count = 0;
                    if(num + 1 < num_max){
                        num++;
                    }else{
                        num = 0;
                    }
                }
            }
        }
    }

    public void paint(Graphics g){
        g.drawImage(offs, 0, 0, this);
    }

    public void update(Graphics g){
        paint(g);
    }

    public boolean action(Event ev, Object ob){
        int scl;

        if(ev.target == btn1){
            flag ^= 1;
        }else if(ev.target == btn2){
            if(flag == 0){
                try{
                    scl = Integer.parseInt(text.getText());
                    if((scl >= divide_min) && (scl <= divide_max)){
                         count_max= scl;
                         count = 0;
                    }else{
                        text.setText(Integer.toString(count_max));
                    }
                }catch(NumberFormatException e){
                    text.setText(Integer.toString(count_max));
                }
            }

        }

        return true;
    }

    void init_screen(){
        //grf.setColor(Color.black);
        grf.setColor(new Color(80,100,150));
        grf.fillRect(0, 0, size().width, size().height);
    }    

   		
		
	private void makeimage(){
		int[] datax;
		int[] datay;
		double x1,y1,x2,y2;
		
		x1= kfdatax[0][0][0];
		y1= kfdatay[0][0][0];
		//描画処理ループ
		for(int j = 0; j < kfdatax.length ; j++){
			//画像データ作成処理
			datax = new int[ kfdatax[j][num].length ];
			datay = new int[ kfdatax[j][num].length ];

			double t = (double)count / (double)count_max;
			for(int i = 0; i < kfdatax[j][num].length ; i++){
				//System.out.println(""+kfdatax[j][num].length + " "+ j +" "+ num +" "+ i);
				if(i==0){
					x1 = (kfdatax[j][num][i] * (1-t) + kfdatax[j][(num+1)%num_max][i] * t);
					y1 = (kfdatay[j][num][i] * (1-t) + kfdatay[j][(num+1)%num_max][i] * t);
				}

				x2 = (kfdatax[j][num][i] * (1-t) + kfdatax[j][(num+1)%num_max][i] * t);
				y2 = (kfdatay[j][num][i] * (1-t) + kfdatay[j][(num+1)%num_max][i] * t);
				grf.setColor(Color.white);
				grf.drawLine((int)x1, (int)y1, (int)x2, (int)y2);//ystem.out.println("x1"+x1+" y1"+y1+" x2"+x2+" y2"+y2);
				x1 = x2;
            			y1 = y2;
			}
			
		}
        grf.setColor(Color.white);
        if(Integer.toString(count).length() == 1){
            grf.drawString("[ " + count + "/" + count_max + "]", 0, 20);
        }else{
            grf.drawString("[" + count + "/" + count_max + "]", 0, 20);
        }
    }

}



