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

/*
 *
 * Nernst
 *
 */
public class Nernst extends Applet
{
        DiffusionCanvas 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;
        ReportPanel pReport;
        ControlPanel pControl;
        boolean running=false;
        int Particles[] = {1,2,5,10,20,50,100,200,500};
        protected BarChart OutBar=new BarChart(Color.red);


        public void init(){
                //obsolete.applet.JavaConsole.init(this);
                Panel pnl=new Panel();
                Panel pnl2=new Panel();
           //tdbDisplay=new ThreeDBorder(pnl2,5);
           pnl2.setLayout(new BorderLayout());
           CDiff=new DiffusionCanvas(this);
           pnl2.add("Center",CDiff);
         //  pnl2.add("South", OutBar);
          // tdbDisplay.inset();
           pReport=new ReportPanel(this);
           pControl=new ControlPanel(this);
           setLayout(new BorderLayout());
           add("Center",new ThreeDBorder(pnl));
           pnl.setLayout(new BorderLayout());
           pnl.add("Center",CDiff);
           pnl.add("North", pReport);
           pnl.add("South", pControl);
           
          
    }

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


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

        public void stop(){
              if (running){
                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");
                        }else{
                               
                                CDiff.start();
                                OutBar.start();
                                running=true;
                                Start_Stop.setLabel("Stop");
                        }
                }else if (event.target==Num_Particles){
                        int NewNum=Particles[Num_Particles.getSelectedIndex()];
                        if (running) CDiff.stop();
                        CDiff.NUM_PARTICLE=NewNum;
                        CDiff.init();
                        if (running) CDiff.start();
                }

                return true;

        }

}


class ReportPanel extends Panel{
        Nernst app;

        public ReportPanel(Applet applet){
                app=(Nernst)applet;
                Panel pIn=new Panel();
                Label inside=new Label("Inside",Label.CENTER);
                app.bInside= new RBox(pIn,inside);
                pIn.setLayout(new BorderLayout());
                pIn.add("West",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("West",app.CDiff.R_out);
                pOut.add("East",app.CDiff.B_out);

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

                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 ControlPanel extends Panel{

        Nernst app;

        public ControlPanel(Applet applet){
                app=(Nernst)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("500 Particles");
                app.Num_Particles.addItem("Choose Number of Particles");
                app.Num_Particles.select(9);

                app.Start_Stop= new Button("Start");

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

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

        }

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

}



