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[][] = {
	{ 90,140,240,340,140,340,390,},
	{ 240,240,240,240,90,240,390,},
        { 90,140,240,340,140,340,390,},
    };

    int kfdatay[][] = {
	{ 350,250,50,250,250,250,350,},
	{ 200,350,350,200,50,200,50,},
        { 350,250,50,250,250,250,350,},
   };

    int point_max = 0x3;
    int point = 0x7;  // cantidad de puntos de un frame  
    final int divide_min = 5;
    final int divide_max = 50;
    int count_max = 20;
    int count = 0;
    int num_max = 3;   // cantidad de cambios de los frames
    int num = 0;

    Panel pp;
    Button btn1, btn2;
    Label lab;
    TextField text;
    int textlen = 3;
    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(new Color(80,100,150));
        grf.fillRect(0, 0, size().width, size().height);
    }    

    void makeimage(){ 
        int modnum;
        double x1, y1, x2, y2, cc;

        cc = (double)count / count_max;

        x1 = 0;
        y1 = 0;
    
	
	if((count == 0) || (count == count_max)){
	    grf.setColor(Color.white);  
	}else{
	    grf.setColor(Color.white);
	}
	
	
	modnum = (num + 1) % num_max;
	for(int i = 0; i < point; i++){
	    x2 = kfdatax[num][i] * (1 - cc) + kfdatax[modnum][i] * cc;
	    y2 = kfdatay[num][i] * (1 - cc) + kfdatay[modnum][i] * cc;
	    
	    if(i != 0){
                grf.drawLine((int)x1, (int)y1, (int)x2, (int)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);
        }
    }
}


