import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import java.lang.Math.*;
import javax.swing.*;
import javax.swing.event.*;

public class oneD extends AnimeApplet
{
    JCheckBox cbI,cbP,cbG;
    int stp,endp;
    Random r;

    public void resetAll() {
        r=new Random();
    }

    public void init() {
        super.init();
        setStepTime(0.05);

        Container cont=getContentPane();
        cont.setLayout(new BorderLayout());

        resetAll();

        JPanel p=new JPanel();
        p.setLayout( new GridLayout(2,1));
        p.add(animePanel);

        cbI=new JCheckBox("速度を描く",false);

        cbI.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent evt) {
                ChangeShowVFlg(cbI.isSelected());
            }
        });

        cbG=new JCheckBox("力を描く",false);

        cbG.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent evt) {
                ChangeShowFFlg(cbG.isSelected());
            }
        });

        cbP=new JCheckBox("一カ所だけ見せる",false);

        cbP.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                ChangeShowPFlg(cbP.isSelected());
            }
        });

        Panel p5=new Panel();
        p5.add(cbI);
        p5.add(cbG);
        p5.add(cbP);
        p.add(p5);

        cont.add("South",p);
        cont.add("Center",canvas);

        tm.start();
        start();
    }

    boolean ShowFFlg=false;
    boolean ShowVFlg=false;
    boolean ShowPFlg=false;
    boolean ShowRFlg=false;
    double kabe;
    double k=4;

    public void setKabe(double a) {
        kabe=a;
        Graphics g=getGraphics();
        if( g != null ) { update(g); }
    }

    public void ChangeShowFFlg(boolean a) {
        ShowFFlg=a;
        Graphics g=getGraphics();
        if( g != null ) { update(g); }
    }

    public void ChangeShowVFlg(boolean a) {
        ShowVFlg=a;
        Graphics g=getGraphics();
        if( g != null ) { update(g); }
    }

    public void ChangeShowPFlg(boolean a) {
        ShowPFlg=a;
        if( a ) {
            stp=r.nextInt(canvas.getWidth()/16)*16+8;
            endp=stp+16;
        }
        Graphics g=getGraphics();
        if( g != null ) { update(g); }
    }

    private void writeSegment(Graphics g,int i,int y, int ny) {
        if( ny> y) {
            g.fillRect(i,y,1,ny-y);
        } else {
            if( ny==y ) {
                g.fillRect(i,y,1,1);
            } else {
                g.fillRect(i,ny,1,y-ny);
            }
        }
    }

    void drawYajirusi(Graphics g,int x,int y,double angle,int len, int w)
    {
        Polygon p=new Polygon();

        int x1=(int)(-w*Math.sin(angle)/2);
        int y1=(int)(-w*Math.cos(angle)/2);
        int x2=(int)(len*Math.cos(angle)/3);
        int y2=(int)(-len*Math.sin(angle)/3);
        int xl=(int)(len*Math.cos(angle));
        int yl=(int)(-len*Math.sin(angle));

        p.addPoint(x-x1,y-y1);
        p.addPoint(x+x1,y+y1);
        p.addPoint(x+x1+xl-x2,y+y1+yl-y2);
        p.addPoint(x+2*x1+xl-x2,y+2*y1+yl-y2);
        p.addPoint(x+xl,y+yl);
        p.addPoint(x-2*x1+xl-x2,y-2*y1+yl-y2);
        p.addPoint(x-x1+xl-x2,y-y1+yl-y2);

        g.fillPolygon(p);
    }


    public void writeCanvas(Graphics g) {
        int i;
        double x;
        double nx;
        double y1,y3,y4,ny1,ny3,ny4;
        int w,h;

        double R,P,phi;

        w=canvas.getWidth();
        h=canvas.getHeight();

        g.setColor(Color.white);
        g.fillRect(0,0,w,h);

        g.setColor(Color.gray);
        g.drawLine(0,h/2,w,h/2);
        g.drawLine(w/2,h,w/2,-h);


        double E=k*k-40*kabe;
        double kdash;

        int st,end;

        if( ShowPFlg ) {
            st=stp;
            end=endp;
            g.setColor(Color.black);
            g.fillRect(0,0,st,h);
            g.fillRect(end,0,w-end,h);
        } else {
            st=0;
            end=w;
        }


        for( i=st ; i<end ; i++ ) {
            x= 2*i*Math.PI/w-Math.PI;
            nx=2*(i+1)*Math.PI/w-Math.PI;

            double kx=k*x;
            double knx=k*nx;

            y1=Math.sin(kx-t);
            ny1=Math.sin(knx-t);


            if( i%16 == 0 ) {
                g.setColor(Color.gray);
                g.fillOval(i-8,(int)((y1)*h/5)+h/2-8,16,16);
                if( ShowFFlg ) {
                    g.setColor(Color.red);
                    drawYajirusi(g,i,(int)((y1)*h/5)+h/2,0.5*Math.PI,(int)(30.0*Math.sin(kx-t)),6);
                }
                if( ShowVFlg ) {
                    g.setColor(Color.blue);
                    drawYajirusi(g,i,(int)((y1)*h/5)+h/2,0.5*Math.PI,(int)(30.0*Math.cos(kx-t)),4);
                }
            }
            g.setColor(Color.black);
            writeSegment(g,i,(int)((y1)*h/5)+h/2,
                         (int)((ny1)*h/5)+h/2);

        }
    }
}






//  LocalWords:  touka
