import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class m0104272_3 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[][][] =    // 点情報
        {
			//輪郭
			{{180,113},{73,226},{180,320},{358,288},{325,147}},
			//目
			{{183,155},{171,158},{171,168},{180,173}},
			{{180,173},{195,171},{195,158},{183,155}},
			{{276,186},{266,188},{266,200},{275,205}},
			{{275,205},{290,204},{290,189},{276,186}},
			//鼻
			{{148,178},{208,225},{215,210}},
			{{148,178},{225,198},{215,210}},
			//ほっぺ
			{{265,225},{271,218},{280,218},{286,227}},
			{{265,225},{271,232},{280,232},{286,227}},
			//口
			{{170,235},{200,265},{244,246}},
			//帽子
			{{135,98},{168,120},{210,111}},
			{{210,111},{243,110},{270,123}},
			{{270,123},{300,146},{328,144}},
			{{328,144},{360,142},{388,148}},

			{{137,93},{136,96},{135,98}},
			{{390,143},{389,146},{388,148}},

			{{137,93},{170,115},{212,106}},
			{{212,106},{245,105},{272,118}},
			{{272,118},{302,141},{330,139}},
			{{330,139},{362,137},{390,143}},

			{{190,106},{214,32},{244,7}},
			{{244,7},{290,9},{310,20}},
			{{310,20},{369,30},{368,25}},
			{{368,25},{361,86},{333,138}},

			{{210,63},{282,98},{355,93}},
			//マフラー
			{{150,230},{147,250},{160,260}},
			{{160,260},{145,258},{122,284}},
			{{122,284},{110,310},{62,325}},
			{{62,325},{64,348},{100,351}},
			{{179,254},{159,251},{148,287}},
			{{148,287},{125,357},{65,370}},
			{{65,370},{135,430},{203,415}},
			{{203,415},{205,380},{215,358}},
			{{215,358},{240,300},{237,267}},
			{{235,278},{268,284},{286,270}},
			{{286,270},{310,250},{300,240}},

			{{123,340},{160,360},{212,364}},
			{{108,358},{150,380},{208,384}},
			//体
			{{60,480},{18,429},{10,348},{86,243},{155,251}},
			{{285,268},{355,318},{375,380},{352,445},{325,480}},
			//手
			{{310,365},{358,340},{405,326}},
			{{398,308},{404,327},{415,335}},
			{{398,308},{405,300},{411,302}},
			{{415,335},{421,333},{424,329}},
			{{411,302},{428,272},{468,277},{478,296},{454,308},{432,313}},
			{{432,313},{454,308},{476,315},{434,335},{424,329}},
			//飾り
			{{71,155},{59,158},{59,168},{68,173}},
			{{68,173},{83,171},{83,158},{71,155}},
			{{56,166},{46,168},{46,180},{55,185}},
			{{55,185},{70,184},{70,169},{56,166}},

			{{58,166},{33,125},{62,90}},
			{{62,90},{80,125},{58,166}},
			{{58,166},{54,125},{59,118}},
			{{58,166},{15,165},{10,145}},
			{{10,145},{50,145},{58,166}},

   		 };

    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.red);
                    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);
    }
}
