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

public class Test extends Applet 
{
    private Spline splineX;
    private Spline splineY;
    private double[] px;
    private double[] py;
    private int dragPoint = -1;
    
    public Test()
    {
        px = new double[] { 100, 200, 100, 200 };
        splineX = new Spline(px);
        System.out.println(splineX.fn(2, 0.3));
        py = new double[] { 100, 200, 300, 400 };
        splineY = new Spline(py);
    }

    public void paint(Graphics g)
    {
        double x0 = 0;
        double y0 = 0;
        for (int i = 0; i < px.length - 1; i++) {
            x0 = splineX.fn(i, 0);
            y0 = splineY.fn(i, 0);
            g.fillRect((int)x0 - 4, (int)y0 - 4, 8, 8);
            for (int t = 1; t < 31; t++) {
                double x1 = splineX.fn(i, ((double) t) / 30.0);
                double y1 = splineY.fn(i, ((double) t) / 30.0);
                g.drawLine((int)x0, (int)y0, (int)x1, (int)y1);
                x0 = x1;
                y0 = y1;
            }
        }
        g.fillRect((int)x0 - 4, (int)y0 - 4, 8, 8);
    }
    
    public boolean mouseDrag(Event evt, int x, int y)
    {
        if (dragPoint >= 0) {
            px[dragPoint] = x;
            py[dragPoint] = y;
            splineX = new Spline(px);
            splineY = new Spline(py);
            repaint();
        }
        return true;
    }

    public boolean mouseDown(Event evt, int x, int y)
    {
        for (int i = 0; i < px.length; i++) {
            if (Math.abs(px[i] - x) < 8 && Math.abs(py[i] - y) < 8) {
                dragPoint = i;
                break;
            }
        }
        return true;
    }
    
    public boolean mouseUp(Event evt, int x, int y)
    {
        dragPoint = -1;
        return true;
    }
}
