import java.applet.Applet;
import java.awt.*;
//import gjt.*;

/*
 *
 * Nernst
 *
 */
public class Fick1 extends Applet
{
        DiffusionCanvas11 CDiff;
        RBox bR_in, bR_out, bB_in, bB_out, bVoltage, bInside, bOutside, bAverage;
        Choice Num_Particles;
        Button Start_Stop, Plot, Plot2, Fit;
        ThreeDBorder tdbDisplay,graph;
        ReportPanel11 pReport;
        ControlPanel11 pControl;
        boolean running=true, prun=true, rst=false;
        int Particles[] = {50,100,200,300,400,500};
        protected BarChart OutBar=new BarChart(Color.red);
        GraphFickCanvas1 Gr;
        Thread t;
        int PLOT_REAL=0, is=0, xs[]=new int[3000], ys[]= new int[3000], zs[]=new int[3000];

        public void init(){
                //obsolete.applet.JavaConsole.init(this);
                Panel pnl=new Panel();
                Panel pnl2=new Panel();
             Gr=new GraphFickCanvas1(this);
           tdbDisplay=new ThreeDBorder(pnl2,5);
           pnl2.setLayout(new BorderLayout());
           CDiff=new DiffusionCanvas11(this);
           pnl2.add("Center",CDiff);
           pnl2.add("South", OutBar);
           tdbDisplay.inset();
           pReport=new ReportPanel11(this);
           pControl=new ControlPanel11(this);
           setLayout(new BorderLayout());
           add("Center",new ThreeDBorder(pnl));
           pnl.setLayout(new BorderLayout());
           pnl.add("Center",tdbDisplay);
           pnl.add("North", pReport);
           pnl.add("South", pControl);
           Gr.resize(300,300);
           graph= new ThreeDBorder(Gr,5);
           add("East", graph);
           t= new PThread11(this);
           t.start();
     }

        public void destroy()
        {
                //obsolete.applet.JavaConsole.flushClasses(this);
        }


        public void start(){
                CDiff.init();
                CDiff.start();
        }

        public void stop(){
                CDiff.stop();
        }

        public boolean action(Event event, Object what){
                if(event.target==Start_Stop){
                        if (running){
                                CDiff.stop();
                                OutBar.stop();
                                running=false;
                                Start_Stop.setLabel("Start");
                                t.suspend();
                        }else{
                                CDiff.start();
                                OutBar.start();
                                running=true;
                                Start_Stop.setLabel("Stop");
                                t.resume();
                        }
                }else if (event.target==Num_Particles){
                        int NewNum=Particles[Num_Particles.getSelectedIndex()];
                        if (running){ 
                                CDiff.stop();
                                rst=true;
                                t.suspend();
                        }
                        CDiff.NUM_PARTICLE=NewNum;
                        CDiff.init();
                        if (running){ 
                                CDiff.start();
                                t.resume();
                        }
                }else if (event.target==Plot){
                        if(prun){
                                prun=false;
                                t.suspend();
                                Plot.setLabel("Start Plot");
                        }
                        else{
                                prun=true;
                                t.resume();
                                Plot.setLabel("Stop Plot");
                        }
                }

                return true;

        }

}


class ReportPanel11 extends Panel{
        Fick1 app;

        public ReportPanel11(Applet applet){
                app=(Fick1)applet;
                Panel pIn=new Panel();
                Label inside=new Label("Inside",Label.CENTER);
                app.bInside= new RBox(pIn,inside);
                pIn.setLayout(new BorderLayout());
                pIn.add("Center",app.CDiff.R_in);
                // pIn.add("East",app.CDiff.B_in);

                Panel pOut=new Panel();
                Label outside=new Label("Outside",Label.CENTER);
                app.bOutside= new RBox(pOut,outside);
                pOut.setLayout(new BorderLayout());
                pOut.add("Center",app.CDiff.R_out);
                // pOut.add("East",app.CDiff.B_out);

                app.bVoltage= new RBox(app.CDiff.Voltage,"ChangeRate");

                setLayout(new GridLayout(1,3,10,10));

                add(new ThreeDBorder(app.bInside));
                add(new ThreeDBorder(app.bVoltage));
                add(new ThreeDBorder(app.bOutside));
        }

        public Insets insets(){
                return new Insets(5,5,5,5);
        }

}

class ControlPanel11 extends Panel{

        Fick1 app;

        public ControlPanel11(Applet applet){
                app=(Fick1)applet;
                app.Num_Particles= new Choice();
                // app.Num_Particles.addItem("1 Particle");
                // app.Num_Particles.addItem("2 Particles");
                // app.Num_Particles.addItem("5 Particles");
                // app.Num_Particles.addItem("10 Particles");
                // app.Num_Particles.addItem("20 Particles");
                app.Num_Particles.addItem("50 Particles");
                app.Num_Particles.addItem("100 Particles");
                app.Num_Particles.addItem("200 Particles");
                app.Num_Particles.addItem("300 Particles");
                app.Num_Particles.addItem("400 Particles");
                app.Num_Particles.addItem("500 Particles");
                app.Num_Particles.select(2);

                app.Start_Stop= new Button("Stop");
                app.Plot= new Button("Stop Plot");
                setLayout(new GridLayout(1,3,0,0));

                add(new ThreeDBorder(app.Num_Particles));
                add(new ThreeDBorder(app.Start_Stop));
                add(new ThreeDBorder(app.Plot));
        }

        public Insets insets(){
                return new Insets(5,5,5,5);
        }

}
class GraphFickCanvas1 extends Canvas {

        Fick1 app1;        

        static final int POSX = 525;
        static final int NEGX = 25;
        static final int POSY = 15;
        static final int NEGY = 10;
        
        int a,b,N,x1,x2,y1,y2;

          String Str0=" ";
        String Str1=" ";
        String Str2=" ";

        
        double xstep;
        double ystep;

        public GraphFickCanvas1 (Applet a){
                app1=(Fick1)a;
        }
        
        public synchronized void paint (Graphics g) {
                Dimension dm=size();
                int x,y,i,j;
                xstep=(double)dm.width/(double)(POSX+NEGX);
                ystep=(double)dm.height/(double)(POSY+NEGY);
                int xax=(int)Math.round(ystep*POSY);
                int yax=(int)Math.round(xstep*NEGX);
                g.setColor(Color.blue);
                g.drawLine(yax,0,yax,dm.height);
                g.drawLine(0,xax,dm.width,xax);
                for(i=0;i<(NEGX+POSX+1);i+=25){
                        x=(int)Math.round(i*xstep);
                        g.drawLine(x,xax-2,x,xax+2);
                }
                for(i=0;i<(NEGY+POSY+1);i++){
                        y=(int)Math.round(xax+i*ystep);
                        g.drawLine(yax-2,y,yax+2,y);
                }         
                for(i=0;i<(POSY+1);i++){
                        y=(int)Math.round(xax-i*ystep);
                        g.drawLine(yax-2,y,yax+2,y);
                } 
                g.setColor(Color.black);
                Font f = new Font("TimesRoman",Font.PLAIN,11);
                g.setFont(f);
                 for(i=0;i<500;i+=50){
                        g.drawString(" "+i,yax+(int)Math.round(i*xstep)-10,xax+12);      
                 }
//                g.drawString("5",yax+(int)Math.round(5*xstep)-2,xax+12);      
//                g.drawString("8",yax+(int)Math.round(8*xstep)-2,xax+12);
//                g.drawString("10",yax+(int)Math.round(10*xstep)-4,xax+12);
//                g.drawString("-10",yax+(int)Math.round(-10*xstep)-9,xax+12);
//                g.drawString("-20",yax+(int)Math.round(-20*xstep)-9,xax+12);
                f = new Font("TimesRoman",Font.PLAIN+Font.BOLD,12);
                g.setColor(Color.magenta);
                g.drawString("dC",dm.width-13,xax+13);
                g.drawString("R",yax-12,12);
                f = new Font("TimesRoman",Font.PLAIN,11);
                g.setColor(Color.black);
                for(i=15;i>(-NEGY);i-=2){
                        g.drawString(" "+i,yax+6,(int)Math.round((POSY-i)*ystep)+5);
                }
         /*       if(app1.PLOT_AV == 1){                
                        a=10-app1.Particles[app1.Num_Particles.getSelectedIndex()];
                        b=(int)Math.round(app1.CDiff.total_sum/app1.CDiff.n_runs);
                        app1.xs[app1.is]=a;
                        app1.ys[app1.is]=b;
                        app1.zs[app1.is]=1;
                        app1.is++;
                        app1.PLOT_AV=0;  
                }  */
                if(app1.PLOT_REAL == 1){
                        a=app1.CDiff.Rin-app1.CDiff.Rout;
                        b=app1.CDiff.rate;
                        app1.xs[app1.is]=a;
                        app1.ys[app1.is]=b;
                        app1.zs[app1.is]=app1.Num_Particles.getSelectedIndex();
                        app1.is++;
             //           g.drawString("Calc",2,30);
                        app1.PLOT_REAL=0;
                }
                for(i=0;i<(app1.is);i++){
                        a=app1.xs[i];
                        b=app1.ys[i];
                        N=4;
                    /*    int hit=0;
                        for(j=i;j>=0;j--){
                                if (a == app1.xs[j]){
                                        if(b == app1.ys[j]){
                                                if(j != i){
                                                        hit++;
                                                        if(hit == 3){
                                                                N++;
                                                                hit=0;
                                                        }        
                                                }       
                                        }
                                }
                        }     */
                        g.setColor(Color.black);
                        if(app1.zs[i] == 0){
                                g.setColor(Color.black);
                        }
                        if(app1.zs[i] == 1){
                                g.setColor(Color.red);
                        }
                        if(app1.zs[i] == 2){ 
                                g.setColor(Color.green);
                        }
                        if(app1.zs[i] == 3){
                                g.setColor(Color.cyan);
                        }
                        if(app1.zs[i] == 4){
                                g.setColor(Color.yellow);
                        }
                        if(app1.zs[i] == 5){
                                g.setColor(Color.magenta);
                        }
           //             g.drawString("oval",2,15);
                        g.fillOval((int)Math.round((NEGX+a)*xstep),(int)Math.round((POSY-b)*ystep),N,N);
                }
           /*         if(app1.ERR == 1){
                        g.setColor(Color.black);
                        g.drawString("Not enough data",2,12);
                        app1.ERR=0;        
                }
                if(app1.PLT == 1){
                        g.setColor(Color.black);
                        Str2=Str1;
                        Str1=Str0;
                        Str0="P="+(float)((float)Math.round(app1.a*100)/(float)100.0)+"+"+(float)((float)Math.round(app1.b*100)/(float)100.0)+"dC";
                        x1=(int)Math.round((NEGX-20)*xstep);
                        y1=(int)Math.round((POSY-(app1.a+app1.b*(-20)))*ystep);
                        x2=(int)Math.round((NEGX+10)*xstep);
                        y2=(int)Math.round((POSY-(app1.a+app1.b*10))*ystep);
                        g.drawLine(x1,y1,x2,y2);
                        app1.PLT=0;
                }        
                    g.setColor(Color.black);
                g.drawString(Str0,5,15);
                g.drawString(Str1,5,30);
                g.drawString(Str2,5,45); */      
         }

}

class PThread11 extends Thread{
        Fick1 a;
        
        public PThread11(Applet a){
                this.a=(Fick1)a;
        }

      public void run(){
                int c=0, d=0;
                while(c<5){
                        if((a.CDiff.Rin-a.CDiff.Rout)!=d){
                                d=a.CDiff.Rin-a.CDiff.Rout;
                                a.PLOT_REAL=1;
                                a.Gr.repaint();
                                c++;
                        }
                }
                while(true){
                       if (a.rst == true){
                                c=0; d=0;
                                while(c<5){
                                        if((a.CDiff.Rin-a.CDiff.Rout)!=d){
                                                d=a.CDiff.Rin-a.CDiff.Rout;
                                                a.PLOT_REAL=1;
                                                a.Gr.repaint();
                                                c++;
                                        }
                                }
                                a.rst=false;
                        }   
                        a.PLOT_REAL=1;
                        a.Gr.repaint();
                        try{
                                sleep(5000);
                        }
                        catch(InterruptedException e) {};
                }
        }    
}


        




        
