import java.applet.*;
import java.awt.*;
import java.awt.event.*;

/*
<applet code="Bezier.class" width=480 height=480>
</applet>
*/

public class Bezier extends Applet
{
    static int WIDTH, HEIGHT;        // 画面サイズ
    Point point[][];                 // 制御点
    int point_num = 0;               // 制御点の番号
    int captured_point = -1;         // つかんでいる制御点の番号
                                     //（-1のときつかんでいない）
    boolean capture_mode_FLG;        // 線を制御できるかを表すフラグ

    static final int point_data[][][] =    // 点情報
        {
            { {120,360}, {120,410}, {120,460}, },
            { {120,410}, {190,460}, {190,460}, },
            { {120,360}, {190,360}, {190,385}, },
            { {190,385}, {190,410}, {120,410}, },
            { {245,360}, {210,360}, {210,410}, },
            { {210,410}, {210,460}, {245,460}, },
            { {245,460}, {280,460}, {280,410}, },
            { {280,410}, {280,360}, {245,360}, },
            { {370,380}, {335,340}, {300,380}, },
            { {300,380}, {283,400}, {335,410}, },
            { {335,410}, {387,420}, {370,440}, },
            { {370,440}, {335,480}, {300,440}, },
            { {400,360}, {400,410}, {400,460}, },
            { {400,360}, {435,360}, {470,360}, },
            { {400,410}, {435,410}, {470,410}, },
            { {400,460}, {435,460}, {470,460}, }, //ROZE
	    { {60,425}, {120,310}, {200,220}, },
	    { {60,425}, {85,342}, {198,212}, },
	    { {65,420}, {60,425}, {60,425}, },
	    { {125,315}, {130,325}, {155,330}, },
	    { {155,330}, {190,340}, {215,315}, },
	    { {215,315}, {230,295}, {255,295}, },
	    { {130,310}, {160,275}, {205,285}, },
	    { {255,295}, {240,300}, {205,285}, },
	    { {220,295}, {170,290}, {130,315}, },
	    { {110,325}, {80,200}, {110,170}, },
	    { {120,220}, {100,180}, {110,170}, },
	    { {120,220}, {140,270}, {110,325}, },
	    { {110,240}, {130,290}, {110,325}, },
	    { {110,170}, {95,205}, {110,240}, },
            { {200,220}, {240,205}, {260,245}, },
            { {200,220}, {240,220}, {260,245}, },
            { {340,260}, {275,280}, {260,245}, },
            { {340,260}, {300,180}, {200,220}, },
            { {140,110}, {230,140}, {200,220}, },
            { {160,170}, {170,200}, {200,220}, },
            { {140,110}, {165,155}, {160,170}, },
            { {140,110}, {140,150}, {160,170}, },
            { {200,220}, {220,195}, {228,190}, },
            { {205,205}, {220,185}, {225,185}, },
            { {220,150}, {210,220}, {280,180}, },
            { {220,150}, {225,130}, {220,110}, },
            { {280,180}, {290,175}, {320,170}, },
            { {220,110}, {240,120}, {240,140}, },
            { {240,140}, {260,125}, {280,125}, },
            { {280,125}, {280,145}, {275,160}, },
            { {275,160}, {300,160}, {320,170}, },
            { {160,270}, {170,265}, {185,245}, },
            { {185,245}, {170,260}, {165,260}, },
            { {145,275}, {145,270}, {155,250}, },
            { {150,270}, {150,260}, {155,250}, },
            { {230,118}, {255,55}, {295,40}, }, //hana
            { {245,135}, {245,60}, {295,40}, },
            { {295,40}, {315,30}, {300,55}, },
            { {245,120}, {280,60}, {320,60}, },
            { {320,60}, {305,110}, {280,125}, },
            { {310,90}, {330,70}, {360,60}, },
            { {360,60}, {360,80}, {340,110}, },
            { {280,160}, {335,105}, {385,85}, },
            { {360,125}, {365,105}, {385,85}, },
            { {360,125}, {345,155}, {315,170}, },
            { {365,85}, {400,75}, {385,85}, },
            { {345,35}, {310,35}, {285,70}, },
            { {345,35}, {335,40}, {315,60}, },
            { {345,35}, {350,35}, {340,50}, },
            { {355,45}, {330,50}, {320,60}, },
            { {355,45}, {350,60}, {340,70}, },
            { {355,45}, {355,55}, {350,65}, },
            { {375,45}, {360,50}, {355,55}, },
            { {375,45}, {375,80}, {360,95}, },
            };

    Label tf;

    Graphics offg, backg;
    Image    offi, backi;

    public void initialize()                   // 制御点の設定
    {
        int i;
        point = new Point[point_data.length][];

        for(i = 0; i < point.length; i++)
        {
            point[i] = new Point[point_data[i].length];
            for(int j = 0; j < point_data[i].length; j++)
                point[i][j] = new Point(point_data[i][j][0],
                                        point_data[i][j][1]);
        }
    }
    
    public void init()
    {
        Dimension d = getSize();
        WIDTH = d.width; HEIGHT = d.height;

        Button increment, decrement, modify;
        increment = new Button("increment");          // 画面設定
        decrement = new Button("decrement");
        modify = new Button("modify");
        tf = new Label(Integer.toString(point_num));
        add(modify);
        add(increment);
        add(decrement);
        add(tf);

        increment.addActionListener(new ActionListener() // 制御点番号を増やす
        {
            public void actionPerformed(ActionEvent ae)
            {
                point_num++;
                if(point_num >= point.length)
                    point_num = 0;
                tf.setText(Integer.toString(point_num));
                repaint();
            }
        });

        decrement.addActionListener(new ActionListener()  // 制御点番号を減らす
        {
            public void actionPerformed(ActionEvent ae)
            {
                point_num--;
                if(point_num < 0) point_num = point.length-1;
                tf.setText(Integer.toString(point_num));
                repaint();
            }
        });

        modify.addActionListener(new ActionListener()  // モード変更
        {
            public void actionPerformed(ActionEvent ae)
            {
                capture_mode_FLG = !capture_mode_FLG;
                repaint();
            }
        });

        addMouseListener(new MouseAdapter()
        {
            public void mousePressed(MouseEvent me)    // 制御点をつかむ
            {
                int i, mx = me.getX(), my = me.getY();
                for(i = 0; i < point[point_num].length; i++)
                {
                    if(point[point_num][i].x-3 <= mx &&
                       point[point_num][i].x+3 >= mx &&
                       point[point_num][i].y-3 <= my &&
                       point[point_num][i].y+3 >= my)
                        break;
                }
                if(i < point[point_num].length) captured_point = i;
            }

            public void mouseReleased(MouseEvent me)
            {
                captured_point = -1;
            }
        });

        addMouseMotionListener(new MouseMotionAdapter() // 制御点を動かす
        {
            public void mouseDragged(MouseEvent me)
            {
                if(captured_point == -1) return;
                point[point_num][captured_point].setLocation(me.getX(), me.getY());
                repaint();
            }
        });

        initialize();

        offi = createImage(WIDTH, HEIGHT);
        offg = offi.getGraphics();
        backi = createImage(WIDTH, HEIGHT);
        backg = backi.getGraphics();
        setBackImage(backg);

     // test!
        addKeyListener(new KeyAdapter()
        {
            public void keyPressed(KeyEvent ke)
            {
                int c = ke.getKeyCode();
                if(c == 'P')
                {
                    System.out.println("{");
                    for(int i = 0; i < point[point_num].length; i++)
                        System.out.println(""+point[point_num][i].x
                                        +", "+point[point_num][i].y);
                    System.out.println("},");
                }
                else if(c == 'B')
                {
                    // BezierCurve(backg, Color.black);
                    repaint();
                }
            }
        });
    }

    public void setBackImage(Graphics g)    // 背景の用意
    {
        g.setColor(Color.white);
        g.fillRect(0, 0, WIDTH, HEIGHT);
        g.setColor(Color.black);
        g.drawRect(0, 0, WIDTH-1, HEIGHT-1);  // 枠描画
    }

    public int kaijyo(int num)            // 階乗を計算
    {
        int total = 1;

        if(num == 0) return 1;

        for(; num > 1; num--)
            total *= num;
        return total;
    }

    public void BezierCurve(Graphics g, Color color, Point point[])  // 曲線描画
    {
        int i, j;
        double x1, x2, y1, y2;
        double bernstein[] = new double[point.length];
        x1 = point[0].x;
        y1 = point[0].y;

        g.setColor(color);

        for(double t = 0; t <= 1; t += 0.01)
        {
            for(i = 0; i < point.length; i++)        // bernstein関数を計算
            {
                bernstein[i] = (double)kaijyo(point.length-1)
                            / (kaijyo(i)*kaijyo(point.length-1-i));

                for(j = 1; j <= i; j++) bernstein[i] *= t;
                for(j = 1; j <= point.length-1-i; j++) bernstein[i] *= (1-t);
            }

            x2 = y2 = 0;
            for(i = 0; i < point.length; i++)
            {
                x2 += point[i].x * bernstein[i];
                y2 += point[i].y * bernstein[i];
            }

            g.drawLine((int)x1, (int)y1, (int)x2, (int)y2);
            x1 = x2;
            y1 = y2;
        }
    }
    
    public void paint(Graphics g)
    {
        requestFocus();
        offg.drawImage(backi, 0, 0, this);  // 背景の描画

        if(capture_mode_FLG)           // 制御点の描画
        {
            offg.setColor(Color.red);
            for(int i = 0; i < point[point_num].length; i++)
            {
                offg.fillRect(point[point_num][i].x-3,
                              point[point_num][i].y-3, 7, 7);
                offg.drawString(Integer.toString(i+1),
                              point[point_num][i].x-3,
                              point[point_num][i].y-6);
            }
        }

        for(int i = 0; i < point.length; i++)     // 曲線描画
        {
            if(capture_mode_FLG && point_num == i)
                BezierCurve(offg, Color.blue, point[i]);
            else BezierCurve(offg, Color.black, point[i]);
        }

        g.drawImage(offi, 0, 0, this);
    }

    public void update(Graphics g)
    {
        paint(g);
    }
}