import java.applet.*;
    import java.awt.Graphics;
    import java.awt.*;
    import java.lang.System;
    import java.lang.*;
    import java.net.*;
    import netscape.javascript.JSObject;
    import netscape.javascript.JSException;
    import SimLib.*;
    
    public class Cascade extends thesisApplet   //(15)
    {
      boolean useBackgroundImage = true;
      BaseGraphView graphViewer;	
      JSObject jsWindow;
      JSException exp;
    
      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)
      int             vuur1=0,vuur2=0,vuur3=0,vuur4=0,vuur5=0;       //(Nr=5: 1...4)
      int             gehad1=0,gehad2=0,gehad3=0,gehad4=0,gehad5=0;  //(Nr=5: 1...5)
    
      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");
       }
    }
    
    String achterkomma(String t, int aantal)
    {
      String tTemp;
      int number;
      tTemp = "   ";
      number = t.indexOf(".");
      tTemp = t.substring(0,number + aantal + 1);
      return(tTemp);
    }
    
    void NaarJS(String t)
    {
      Object args[] = new Object[2];
      args[0] = getParameter("name");
      args[1] = t;
      if (jsWindow!=null) jsWindow.call("infoFromJava",args);
      else System.out.println("Cannot call JavaScript, because jsWindow is null");
    }
    
    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 (Ni,1,pix,pix)
    
      temp_panel1.add(Input1 = 
      new NumberChooser("Tap1:","[liter/min]", 0.6, 0.0, 1.0)); //scrollbar 1 (17)
                  Input1.setBackground(new Color(255,251,131)); //(17f)
                  Input1.setColor(Color.blue);                  //(17g)
    
      temp_panel1.add(Input2 = 
      new NumberChooser("Tap2:","[liter/min]", 0.4, 0.0, 1.0)); //scrollbar 2
                  Input2.setBackground(new Color(255,251,131));
                  Input2.setColor(Color.blue);                  //(Ni=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 (2*Nt,1)
    
      temp_panel2.add(Label1 = new Label("Volume 1:")); //counter 1: 26b (4x)
      temp_panel2.add(Text1 = new TextField(" "));
      Text1.setEditable(false);
                  Label1.setBackground(new Color(255,251,131)); //(26c)
                  Label1.setForeground(Color.blue);             //(26d)
                  Text1.setBackground(Color.white);             //(26c)
                  Text1.setForeground(Color.red);               //(26d)
    
      temp_panel2.add(Label2 = new Label("Volume 2:")); //counter 2
      temp_panel2.add(Text2 = new TextField(" "));
      Text2.setEditable(false);
                  Label2.setBackground(new Color(255,251,131));
                  Label2.setForeground(Color.blue);
                  Text2.setBackground(Color.white);
                  Text2.setForeground(Color.green);
    
      temp_panel2.add(Label3 = new Label("Volume 3:")); //counter 3
      temp_panel2.add(Text3 = new TextField(" "));
      Text3.setEditable(false);
                  Label3.setBackground(new Color(255,251,131));
    
                  Label3.setForeground(Color.blue);
                  Text3.setBackground(Color.white);
                  Text3.setForeground(Color.orange);
    
      temp_panel2.add(Label4 = new Label("time [min]:")); //counter 4
      temp_panel2.add(Text4 = new TextField(" "));
      Text4.setEditable(false);   
                  Label4.setBackground(new Color(255,251,131));
                  Label4.setForeground(Color.blue);
                  Text4.setBackground(Color.white);
                  Text4.setForeground(Color.black);  //(Nt=4)
    
      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    = 0.6; 
      K2    = 0.4;  
      try 
        {jsWindow = JSObject.getWindow(this);}	   
      catch (Exception e)
        {System.out.println("Error in JSObject.getWindow, " + e.getMessage());}
    }
    
    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);
    
      //message 1: N1 < 0.5 geeft: "bak half vol" (rule 1)                           //rules (25) (5x)
      if ( (N1 < 0.5) && (K1 > 0.0) && (gehad1==0) ) {gehad1=1; vuur1=1; NaarJS("1");} 
      if ( (N1 < 0.5) && (K1 > 0.0) && (gehad1==1) && (vuur1==1) ) {vuur1=0;}
      if ( (N1 < 0.5) && (K1 > 0.0) && (gehad1==1) ) {gehad1=1;} else {gehad1=0;};
    
      //message 2: N2 < 0.1 geeft: bijna leeg (rule 2)
      if ( (N1 < 0.1) && (K2 > 0.0) && (gehad2==0) ) {gehad2=2; vuur2=2; NaarJS("2");} 
      if ( (N1 < 0.1) && (K2 > 0.0) && (gehad2==2) && (vuur2==2) ) {vuur2=0;}
      if ( (N1 < 0.1) && (K2 > 0.0) && (gehad2==2) ) {gehad2=2;} else {gehad2=0;};
     
      //message 3: N2 < 0.01 geeft: helemaal leeg (rule 3)
      if ( (N1 < 0.01) && (K2 > 0.0) && (gehad3==0) ) {gehad3=3; vuur3=3; NaarJS("3");} 
      if ( (N1 < 0.01) && (K2 > 0.0) && (gehad3==3) && (vuur3==3) ) {vuur3=0;}
      if ( (N1 < 0.01) && (K2 > 0.0) && (gehad3==3) ) {gehad3=3;} else {gehad3=0;};
     
      //message 4: N1 < 0.5 en kraan 1 staat te ver open geeft: xxxx (rule 4)
      if ( (N1 < 0.5) && (K1 > 0.1) && (gehad4==0) ) {gehad4=4; vuur4=4; NaarJS("4");} 
      if ( (N1 < 0.5) && (K1 > 0.1) && (gehad4==4) && (vuur4==4) ) {vuur4=0;}
      if ( (N1 < 0.5) && (K1 > 0.1) && (gehad4==4) ) {gehad4=4;} else {gehad4=0;};
     
      //message 5: N1 < 0.5 en kraan 2 staat te ver open geeft: xxxx (rule 5)
      if ( (N1 < 0.5) && (K2 > 0.1) && (gehad5==0) ) {gehad5=5; vuur5=5; NaarJS("5");} 
      if ( (N1 < 0.5) && (K2 > 0.1) && (gehad5==5) && (vuur5==5) ) {vuur5=0;}
      if ( (N1 < 0.5) && (K2 > 0.1) && (gehad5==5) ) {gehad5=5;} else {gehad5=0;};    //(Nr=5)
    
      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(achterkomma(Double.toString(N1),3));  //assignment counter 1 (26b) (4x)
      Text2.setText(Double.toString(N2));  //assignment counter 2 
      Text3.setText(Double.toString(N3));  //assignment counter 3 
      Text4.setText(achterkomma(Double.toString(t),1));   //assignment counter 4, etc. (Nt=4)
    
      graphViewer.newTValue(t);
      return t<Tmax;
    }
    }