import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class m0104418 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[][][] =    // 点情報
        {
			//鳥
			{{190,130},{240,120}},//くちばし
			{{240,120},{200,105}},
			{{200,105},{190,80},{150,95}},//頭部
			{{150,95},{125,110},{110,115}},
			{{110,115},{120,118}},
			{{120,118},{130,140},{105,165}},
			{{105,165},{55,240},{80,315},{220,345}},//腹
			{{220,345},{250,350},{270,330},{295,310},{320,325}},
			{{320,325},{360,345},{390,355},{420,350}},//尾
			{{420,350},{390,350},{350,315}},
			{{350,315},{410,325},{470,360}},
			{{470,360},{420,310},{340,290}},
			{{340,290},{320,210},{225,190}},//背（羽）
			{{225,190},{200,165},{185,160}},
			{{185,160},{175,145},{190,130}},
			//足
			{{220,345},{190,380},{185,410}},//右
			{{185,410},{175,425}},
			{{175,425},{173,400}},
			{{173,400},{140,455},{158,405}},
			{{158,405},{185,385},{200,355},{205,343}},
			{{160,330},{80,360},{95,380}},//左
			{{95,380},{80,365},{65,370}},
			{{65,370},{45,365},{65,363}},
			{{65,363},{85,370},{150,320}},
			//枝
			{{350,480},{230,440},{120,390},{70,330}},
			{{70,330},{5,260},{60,340}},
			{{60,340},{75,360},{0,350}},
			{{0,360},{70,360},{85,380}},
			{{85,380},{190,440},{250,480}},
			//木
			{{210,0},{230,35},{260,35}},
			{{250,20},{260,70},{305,65}},
			{{280,60},{305,140},{385,140}},
			{{375,125},{420,190},{480,170}},
			//花びら
			{{65,35},{50,30},{40,45}},//左上
			{{40,45},{50,50},{65,35}},
			{{50,130},{40,130},{30,135}},//左中
			{{30,135},{43,145},{50,130}},
			{{240,75},{245,65},{255,65}},//中上
			{{255,65},{250,75},{240,75}},
			{{380,200},{390,185},{405,190}},//右中
			{{405,190},{395,200},{380,200}},
			{{417,245},{425,230},{435,235}},
			{{435,235},{420,245},{417,245}},
			{{415,400},{430,390},{445,390}},//右下
			{{445,390},{425,410},{415,400}},
			//模様
			{{225,190},{180,180},{200,230}},//羽
			{{200,230},{235,275},{350,305}},
			{{350,305},{345,295},{340,290}},
			{{250,270},{295,320},{310,320}},//尾
			{{350,315},{340,305},{330,300}},
			{{195,220},{195,190},{170,160}},//首
			{{180,155},{120,140},{90,245}},

            };

    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()
    {
        WIDTH = 480; HEIGHT = 480;

        Button increment, decrement, modify;
        increment = new Button("線を選ぶ(+)");          // 画面設定
        decrement = new Button("線を選ぶ(-)");
        modify = new Button("制御点を移動する");
        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);
    }
}