import java.applet.*;
    import java.awt.Graphics;
    import java.awt.*;
    import java.lang.System;
    import java.lang.*;
    import java.net.*;
    import SimLib.*;
    
    public class Cascade extends thesisApplet   //(15)
    {
      boolean useBackgroundImage = true;
      BaseGraphView graphViewer;
      NumberChooser   Input1,Input2;                //(Ni=2: 1...2)
      YGraph          Graph1,Graph2,Graph3;         //(No=3: 1...3)
      TextField       Text1,Text2,Text3,Text4;      //(Nt=4: 1...4)
      Label           Label1,Label2,Label3,Label4;  //(Nt=4: 1...4)
    
      double t,dt,Tmin,Tmax;
      double N1,N2,N3,K1,K2,dN1dt,dN2dt,dN3dt,Error;  //decalaration list (16)
    
    public void readParameters()
    {
      String useBackgroundImageString = getParameter("useBackgroundImage");
      if (useBackgroundImageString != null)
       {
         useBackgroundImageString.trim();
         useBackgroundImage = useBackgroundImageString.equalsIgnoreCase("true");
       }
    }
    
    public void setupGraphs(Container screenView)
    {
      initModel();
      screenView.setLayout(new BorderLayout());
      if (useBackgroundImage)
            {graphViewer=new ImageGraphView((thesisApplet)this,Tmin,Tmax);}
      else
            {graphViewer=new ArrayGraphView((thesisApplet)this,Tmin,Tmax);}
    
      screenView.add("Center", graphViewer);
      Panel temp_panel1 = new Panel();
      temp_panel1.setLayout(new GridLayout(2,1,5,5)); //27a (kolommen,1,x,y)
    
      temp_panel1.add(Input1 = 
      new NumberChooser("Tap1:","[liter/min]", 1.0, 0.0, 1.0)); //scrollbar 1 (17)
    
      temp_panel1.add(Input2 = 
      new NumberChooser("Tap2:","[liter/min]", 0.5, 0.0, 1.0)); //scrollbar 2
    
      screenView.add("West", temp_panel1);
      Graph1 = new YGraph("N1",Color.red,0.0,1.0);    //graph 1 (18) (3x)
      Graph2 = new YGraph("N2",Color.green,0.0,1.0);  //graph 2
      Graph3 = new YGraph("N3",Color.orange,0.0,1.0); //graph 3, etc. (No=3)
    
      Panel temp_panel2 = new Panel();
      temp_panel2.setLayout(new GridLayout(8,1));  //27b (Rijen,1)
    
      temp_panel2.add(Label1 = new Label("Volume 1:")); //counter 1: 26b (4x)
      temp_panel2.add(Text1 = new TextField(" "));
      Text1.setEditable(false);
    
      temp_panel2.add(Label2 = new Label("Volume 2:")); //counter 2
      temp_panel2.add(Text2 = new TextField(" "));
      Text2.setEditable(false);
     
      temp_panel2.add(Label3 = new Label("Volume 3:")); //counter 3
      temp_panel2.add(Text3 = new TextField(" "));
      Text3.setEditable(false);
    
      temp_panel2.add(Label4 = new Label("time [min]:")); //counter 4
      temp_panel2.add(Text4 = new TextField(" "));
      Text4.setEditable(false);   
    
      screenView.add("East", temp_panel2);
    
      graphViewer.addYGraph(Graph1); 
      graphViewer.addYGraph(Graph2); 
      graphViewer.addYGraph(Graph3);     //No=3
    }
    
    public void initModel()
    {
      Tmin  = 0.0;     //start time (19a)
      Tmax  = 3.0;     //maximum run time (19b)
      dt    = 0.002;   //delta t (20)
      t     = 0.0;
    
      N1    = 1.0;     //starting values (21)
      N2    = 0;
      N3    = 0; 
      K1    = 1.0; 
      K2    = 0.5;  
    }
    
    public void initInput()
    {
      Input1.setValue(K1);   //input value 1 (22) (2x)
      Input2.setValue(K2);   //input value 2, etc. (Ni=2)
    }
    
    public void initGraph()
    {
      graphViewer.initGraph();
    }
    
    public boolean stepModel()
    {
      K1 = Input1.getValue();   //get the value 1 (23) (2x)
      K2 = Input2.getValue();   //get the value 2, etc. (Ni=2)
    
      t = t + dt;
    
      dN1dt  = - K1 * N1 - K2 * N1;    // (24)
      dN2dt  = + K1 * N1;
      dN3dt  = + K2 * N1;
      N1     = N1 + dN1dt * dt;
      N2     = N2 + dN2dt * dt;
      N3     = N3 + dN3dt * dt;
      Error = 1.0 - (N1 + N2 + N3);
    
      //if (N1 < 0.01) {'the tank is almost empty'}; //rule 1 (25) (3x)
      //if (... ...) {message 2};                    //rule 2 
      //if (... ...) {message 3};                    //rule 3, etc. (Nr=3)
    
      Graph1.newValue(N1);   //assignment graph 1 (26a) (3x)
      Graph2.newValue(N2);   //assignment graph 2 
      Graph3.newValue(N3);   //assignment graph 3, etc. (No=3)
    
      Text1.setText(Double.toString(N1));  //assignment counter 1 (26b) (4x)
      Text2.setText(Double.toString(N2));  //assignment counter 2 
      Text3.setText(Double.toString(N3));  //assignment counter 3 
      Text4.setText(Double.toString(t));   //assignment counter 4, etc. (Nt=4)
    
      graphViewer.newTValue(t);
     return t<Tmax;
    }
    }