import java.applet.*;
import java.awt.*;
import java.awt.event.*;

/*
<applet code="o0566ik21.class" width=480 height=480>
</applet>
*/

public class o0566ik21 extends Applet
{
    static int WIDTH, HEIGHT;        // 画面サイズ
    Point point[][];                 // 制御点
    int point_num = 0;               // 制御点の番号
    int captured_point = -1;         // つかんでいる制御点の番号
                                     //（-1のときつかんでいない）
    boolean capture_mode_FLG;        // 線を制御できるかを表すフラグ
	
	boolean swichi;					//解説を表示しているかを表すフラグ
	
	
	
	static final int fontsize[]={2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1}; //線の太さの初期値

	static final int point_data[][][] =    // 点情報
        {


			//クチバシ
			{{53,154},{74,142},{84,130},{91,126}},
			{{53,154},{43,167},{86,154},{104,145}},
			{{90,125},{88,131},{89,132},{96,130}},
			{{97,130},{94,137},{92,144},{102,145}},
			{{98,144},{87,152},{71,155},{73,149}},
			{{76,138},{75,143},{83,141},{72,149}},
			{{98,128},{105,132},{108,121},{98,128}},
			{{100,127},{103,128},{107,126},{100,127}},
			
			//首
			{{89,126},{104,99},{137,103},{147,141}},
			{{147,139},{179,184},{157,214},{146,251}},
			{{102,145},{117,149},{124,138},{135,163}},
			{{134,161},{149,176},{134,220},{117,247}},
			
			//羽
			{{147,246},{254,171},{295,229},{355,236}},
			{{353,235},{410,232},{411,247},{378,261}},
			{{397,245},{472,243},{428,277},{391,283}},
			{{398,280},{413,286},{388,287},{353,316}},
			{{362,297},{368,305},{360,316},{315,332}},
			{{208,331},{258,343},{299,348},{323,327}},
			{{139,301},{178,308},{184,327},{209,332}},
			{{118,248},{103,281},{122,303},{140,301}},
			{{277,299},{267,297},{256,309},{212,303}},
			{{327,284},{327,303},{314,298},{305,301}},
			{{333,273},{330,264},{340,269},{331,249}},
			{{178,276},{198,286},{193,294},{225,294}},
			
			//水面
			{{146,357},{111,332},{68,326},{83,305}},
			{{64,318},{50,338},{70,338},{85,357}},
			{{340,377},{273,415},{296,390},{237,391}},
			{{413,336},{410,320},{437,313},{450,280}},

    	};


    TextField zahyo;       // 曲線を指定する点の座標を表示
	
	Label tf;
	Graphics offg, backg, setumei, setumei2, setumei3, setumei4, setumei5;
    Image    offi, backi;
	Button kaisetu = new Button("説明の表示");		//説明のボタン


    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("＜＜線の選択");          // 画面設定
        decrement = new Button("線の選択＞＞");	
        modify = new Button("修正");
		
	tf = new Label(Integer.toString(point_num));  // 線の番号
		
	Button width_up = new Button("太くする");
	Button width_down = new Button("細くする");
	
	zahyo = new TextField("オオハクチョウ", 50);		//座標表示用
	
		add(zahyo);
		add(modify);
        add(increment);
        add(decrement);
        add(tf);
		add(width_up);
	    add(width_down);
		add(kaisetu);
		

        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();
            }
        });

	kaisetu.addActionListener(new ActionListener()  // 解説表示を切り替える
	{
			public void actionPerformed(ActionEvent ae)
			{

			   if(swichi)
			   {
			   setumei = getGraphics();
			   setumei2 = getGraphics();
			   setumei3 = getGraphics();
			   setumei4 = getGraphics();
			   setumei5 = getGraphics();
			   setumei.drawString("",180,100);
			   setumei2.drawString("",180,114);
			   setumei3.drawString("",180,128);
			   setumei4.drawString("",180,142);
			   setumei5.drawString("",60,423);
			   kaisetu.setLabel("説明の表示");
			   swichi = !swichi;
			   
			   repaint();
			   
			   }
			   else
			   {
			   setumei = getGraphics();
			   setumei2 = getGraphics();
			   setumei3 = getGraphics();
			   setumei4 = getGraphics();
			   setumei5 = getGraphics();
			   setumei.drawString("オオハクチョウ",180,100);
			   setumei2.drawString("分類:鳥類 カモ目 カモ科学名:Cygnus cygnus",180,114);
			   setumei3.drawString("本州に渡ってくる冬鳥。冬のあいだは湖、川、海岸",180,128);
			   setumei4.drawString("などに住み、アマモやマコモの根などを食べる。",180,142);
			   setumei5.drawString("←−−−−−−−−−−−全長：約140cm−−−−−−−−−−−−→",60,423);
			   kaisetu.setLabel("非表示");
			   swichi = !swichi;
			   }
			 }
		});

	width_up.addActionListener(new ActionListener()  // 線を太くする
	{
			public void actionPerformed(ActionEvent ae)
			{
				if(capture_mode_FLG){
					if(fontsize[point_num] == 7)
					fontsize[point_num] = 7;
					else
					fontsize[point_num]++;
				}
			repaint();
			}
	});

	width_down.addActionListener(new ActionListener()  // 線を細くする
	{
			public void actionPerformed(ActionEvent ae)
			{
				if(capture_mode_FLG){
					if(fontsize[point_num] == 1)
					fontsize[point_num] = 1;
					else
					fontsize[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 num)  // 曲線描画
    {
		Graphics2D g2=(Graphics2D)g;
        int i, j;
        double x1, x2, y1, y2;
        double bernstein[] = new double[point.length+1];
        x1 = point[0].x;
        y1 = point[0].y;

        g.setColor(color);
		g2.setStroke(new BasicStroke(fontsize[num]));

        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);
			}
		
		zahyo.setText(	//座標の表示
		
			"{{"+ Integer.toString( point[point_num][0].x ) + "," + Integer.toString( point[point_num][0].y ) +"}," + 
				"{" + Integer.toString( point[point_num][1].x ) + "," + Integer.toString( point[point_num][1].y ) + "}," +
				"{" + Integer.toString( point[point_num][2].x ) + "," + Integer.toString( point[point_num][2].y ) + "}," +
				"{" + Integer.toString( point[point_num][3].x ) + "," + Integer.toString( point[point_num][3].y ) + "}}," 
		);
	}



        for(int i = 0; i < point.length; i++)     // 曲線描画
        {
            if(capture_mode_FLG && point_num == i)
                BezierCurve(offg, Color.blue, point[i],i);
            else BezierCurve(offg, Color.black, point[i],i);
        }

        g.drawImage(offi, 0, 0, this);
    }

    public void update(Graphics g)
    {
        paint(g);
    }
}

