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;	
     YGraph          Graph1,Graph2,Graph3;          (3x)   (i.e. No=3)
     TextField       Text1,Text2,Text3,Text4;       (4x)   (i.e. Nt=4)
     Label           Label1,Label2,Label3,Label4;   (4x)   (i.e. Nt=4)
     NumberChooser   Input1,Input2;                 (2x)   (i.e. Ni=2)
    
     double dN1dt,dN2dt,dN3dt,N1,N2,N3,K1,K2,Error;  (16) 
     double t,dt,Tmin,Tmax;
    
    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,6,0));   (27a)
    
     temp_panel1.add(Input1 = 
     new NumberChooser("Tap1","[liter/sec]",1.0,0.0,1.0));  (17) (2x=Ni)
     temp_panel1.add(Input2 = 
     new NumberChooser("Tap2","[liter/sec]",0.5,0.0,1.0));  
    		
     screenView.add("West", temp_panel1);
     Graph1 = new YGraph("N1",Color.red,0.0,1.0);     (18) (3x=No)
     Graph2 = new YGraph("N2",Color.green,0.0,1.0);   
     Graph3 = new YGraph("N3",Color.orange,0.0,1.0);   
     
     Panel temp_panel2 = new Panel();
     temp_panel2.setLayout(new GridLayout(8,1));   (27b)
    
     temp_panel2.add(Label1 = new Label("N1 "));      (26c) (4x=Nt)
     temp_panel2.add(Text1 = new TextField(" "));
     Text1.setEditable(false);
    	
     temp_panel2.add(Label2 = new Label("N2 "));     
     temp_panel2.add(Text2 = new TextField(" "));
     Text2.setEditable(false);	
    	
     temp_panel2.add(Label3 = new Label("N3 "));   
     temp_panel2.add(Text3 = new TextField(" "));
     Text3.setEditable(false);
     
     temp_panel2.add(Label4 = new Label("Error "));    
     temp_panel2.add(Text4 = new TextField(" "));
     Text4.setEditable(false);
    
     screenView.add("East", temp_panel2);
    
     Input1.setColor(Color.blue);      (2x=Ni)
     Input2.setColor(Color.blue);
    	
     graphViewer.addYGraph(Graph1);      (3x=No)
     graphViewer.addYGraph(Graph2);
     graphViewer.addYGraph(Graph3);  
    }
    
    public void initModel() 
    { 
      Tmin	= 0.0;     (19a)
      Tmax	= 10.0;    (19b)
      dt	= 0.03;    (20)
      t	= 0.0;
    
      K1	= 1.0;     (21)
      K2	= 0.5;
      N1	= 1.0;
      N2	= 0;
      N3	= 0;
    }
    
    public void initInput()
    {
      Input1.setValue(K1);     (22) (2x=Ni)
      Input2.setValue(K2);    
    }
    
    public void initGraph()
    {
      graphViewer.initGraph();
    }
    
    public boolean stepModel()
    {
     K1 = Input1.getValue();      (23)  (2x=Ni)
     K2 = Input2.getValue();      
     
     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)                   (25)
     //   {voice(..) or video(..) with content 'the tank is almost empty'};
     
     Graph1.newValue(N1);      (26a) (3x)
     Graph2.newValue(N2);
     Graph3.newValue(N3);
    
     Text1.setText(Double.toString(N1));      (26b) (4x=Nt)
     Text2.setText(Double.toString(N2));
     Text3.setText(Double.toString(N3));
     Text4.setText(Double.toString(Error));
     
     graphViewer.newTValue(t);
     return t<Tmax; 
     }
    }