import java.applet.*;
import java.awt.*;
import java.awt.event.*;

//m0104160　皇甫　将//

/*
<applet code="kadai3.class" width=800 height=800>
</applet>
*/

public class kadai3 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[][][] =    // 点情報
	{
		{{200,450},{250,450}},
		{{200,400},{200,450}},
		{{250,400},{250,450}},
		{{180,400},{270,400}},
		{{180,380},{270,380}},
		{{180,400},{180,380}},
	    {{270,400},{270,380}},
		{{210,380},{210,360}},
		{{240,380},{240,360}},

		{{225,350},{210,370},{100,380}},
		{{100,380},{170,350},{180,330}},
		{{180,330},{160,340},{120,350}},
		{{120,350},{160,340},{180,300}},
		{{180,300},{150,320},{140,320}},
		{{140,320},{160,310},{180,270}},
		{{180,270},{170,280},{150,290}},
		{{150,290},{180,270},{225,200}},

		{{225,350},{240,370},{350,380}},
		{{350,380},{280,350},{260,330}},
		{{260,330},{290,340},{320,350}},
		{{320,350},{290,340},{260,300}},
		{{260,300},{300,320},{300,320}},
		{{300,320},{290,310},{270,270}},
		{{270,270},{280,280},{300,290}},
		{{225,200},{270,270},{300,290}},

		{{225,200},{240,210}},
		{{240,210},{245,195}},

		{{245,195},{260,185}},
		{{260,185},{245,180}},

		{{245,180},{225,160}},

		{{225,160},{205,180}},

		{{205,180},{190,185}},
		{{190,185},{205,195}},

		{{205,195},{210,210}},
		{{210,210},{225,200}},


		{{400,400},{405,390},{410,400}},
		{{400,400},{405,410},{410,400}},
		{{405,300},{410,305},{405,310}},
		{{405,310},{400,315},{405,320}},
		{{405,320},{410,325},{405,330}},
		{{405,330},{400,335},{405,340}},
		{{405,340},{410,345},{405,350}},
		{{405,350},{400,355},{405,360}},


		{{360,370},{365,360},{370,370}},
		{{360,370},{365,380},{370,370}},
		{{365,270},{370,275},{365,280}},
		{{365,280},{360,285},{365,290}},
		{{365,290},{370,295},{365,300}},
		{{365,300},{360,305},{365,310}},
		{{365,310},{370,315},{365,320}},
		{{365,320},{360,325},{365,330}},


		{{340,320},{345,310},{350,320}},
		{{340,320},{345,330},{350,320}},
		{{345,220},{350,225},{345,230}},
		{{345,230},{340,235},{345,240}},
		{{345,240},{350,245},{345,250}},
		{{345,250},{340,255},{345,260}},
		{{345,260},{350,265},{345,270}},
		{{345,270},{340,275},{345,280}},


		{{440,320},{445,310},{450,320}},
		{{440,320},{445,330},{450,320}},
		{{445,220},{450,225},{445,230}},
		{{445,230},{440,235},{445,240}},
		{{445,240},{450,245},{445,250}},
		{{445,250},{440,255},{445,260}},
		{{445,260},{450,265},{445,270}},
		{{445,270},{440,275},{445,280}},


		{{140,220},{145,210},{150,220}},
		{{140,220},{145,230},{150,220}},
		{{145,120},{150,125},{145,130}},
		{{145,130},{140,135},{145,140}},
		{{145,140},{150,145},{145,150}},
		{{145,150},{140,155},{145,160}},
		{{145,160},{150,165},{145,170}},
		{{145,170},{140,175},{145,180}},


		{{90,320},{95,310},{100,320}},
		{{90,320},{95,330},{100,320}},
		{{95,220},{100,225},{95,230}},
		{{95,230},{90,235},{95,240}},
		{{95,240},{100,245},{95,250}},
		{{95,250},{90,255},{95,260}},
		{{95,260},{100,265},{95,270}},
		{{95,270},{90,275},{95,280}},


		{{590,320},{595,310},{600,320}},
		{{590,320},{595,330},{600,320}},
		{{595,220},{600,225},{595,230}},
		{{595,230},{590,235},{595,240}},
		{{595,240},{600,245},{595,250}},
		{{595,250},{590,255},{595,260}},
		{{595,260},{600,265},{595,270}},
		{{595,270},{590,275},{595,280}},




	};

    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 = 800; HEIGHT = 500;

        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);
    }
}

