//======================================================
// Termodinamics-Numerical Analysis package
// Gibbs free energy minimisation
// Dr. Turhan Coban
// Ege University, School of Engineering, Department of Mechanical Eng.
// web : www.turhancoban.com
// =====================================================

import java.lang.Integer;
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.Locale;
import java.text.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.table.*;

public class gibbsTableEN extends JApplet implements  ActionListener,ItemListener
{    private static final long serialVersionUID = 37259887589L;
     String state[]={" product temperature is given         ","   heat transfer is given          "};
     String g1[]={"ch4","c2h6","c3h8","co2","n2","o2","h2o","co","no"}; 
     double gn1[]={0.906,0.056,8.0e-4,0.017,18.5365,4.9245,0,0,0};
     double gT[]={322.93,322.93,322.93,322.93,322.93,322.93,322.93,322.93,322.93};
     double Te=1300.0;
     double Pe=1.0;
     double Qe=0;
     JPanel panel[];
     JTextArea output;
     gibbs gib;
     JButton b1;
     girdiP3EN gp3; 
     boolean caseT=true;
     JCheckBox hhv;
     JTextField hhv_lhv;
   public String setArea()
   {
    String s="";  
    s+="    Dr. Turhan Çoban, \n";
    s+="    Ege University, School of Engineering, Department of Mechanical Eng.\n";
    s+="    web : www.turhancoban.com\n";
    s+="    =======================================================\n";   
    for(int i=0;i<gib.n.length;i++) s+=gib.gs[i].Formula+"   "+gib.n[i][0]+"\n";
    Qe=gib.delQ();
    s+="Q   = "+Qe+" KJ/kmol\n";
    s+="Te  = "+Te+" degree K\n";
    s+="Pe  = "+Pe+" bar\n";
    s+="---------------------------------";
    return s;
   }
   
    public void init()
    {      
      //ilk değerler
      gp3=new girdiP3EN(g1,gn1,gT,Te,Pe,"Gibbs free energy minimisation");     
	  gib=new gibbs(g1,gn1,gT,Te,Pe);
      gib.getEq();
      Qe=gib.delQ();
      hhv=new JCheckBox("exit temperature/reaction energy ? ",true); 
      hhv.addItemListener(this);     
      hhv_lhv=new JTextField(state[0]);
      hhv_lhv.setFont(new Font("TimesRoman",Font.BOLD,14));                      
      panel=new JPanel[2];
      panel[1]= new JPanel(new FlowLayout());  
      panel[1].add(hhv); 
      panel[1].add(hhv_lhv);
	  JTabbedPane jtp=new JTabbedPane();
	  b1=new JButton("Gibbs free energy minimisation");
	  b1.addActionListener(this);
      panel[0]= new JPanel(new BorderLayout());
           
      output=new JTextArea(setArea()); 
      output.setFont(new Font("TimesRoman",Font.BOLD,14));
      panel[0].add(gp3,BorderLayout.NORTH);
      panel[0].add(panel[1],BorderLayout.CENTER);
      panel[0].add(b1,BorderLayout.SOUTH);
      //b1.addActionListener(this);  
      JScrollPane skrolPane=new JScrollPane(panel[0]);
      jtp.addTab("Input Panel",skrolPane);     
      jtp.addTab("Output Panel",new JScrollPane(output));
      add(jtp);                      
    }

    public void actionPerformed(ActionEvent e)
    {
    if (e.getSource()==b1)
    {
      g1=gp3.gas;
      gn1=gp3.ngas;
      gT=gp3.Tgas;
      Pe=gp3.Pegas;
      if(caseT)
      { Te=gp3.Tegas; 
        gib.setgibbs(g1,gn1,gT,Te,Pe);
        gib.getEq();
        Qe=gib.delQ();
       
      }
      else
      { Qe=Double.parseDouble(gp3.t2[gp3.n].getText());
        Te=gib.set_Q(g1,gn1,gT,Qe,Pe);
        gib.getEq();
      }
    }
    else if (e.getSource()==gp3.t2[gp3.n])
    {
      g1=gp3.gas;
      gn1=gp3.ngas;
      gT=gp3.Tgas;
      Pe=gp3.Pegas;
      if(caseT)
      {Te=Double.parseDouble(gp3.t2[gp3.n].getText());
       gib.setgibbs(g1,gn1,gT,Te,Pe);
       gib.getEq();
       Qe=gib.delQ();
      }
      else
      { Qe=Double.parseDouble(gp3.t2[gp3.n].getText());
        Te=gib.set_Q(g1,gn1,gT,Qe,Pe);
        gib.getEq();
      }
    }
    output.setText(setArea());
    repaint();
  }
              
public void itemStateChanged(ItemEvent e)
{   if(e.getSource()==hhv)
    { if(e.getStateChange()==ItemEvent.SELECTED)
        {caseT=true;
        hhv_lhv.setText(state[0]);
        gp3.jl[3].setText("Exit temperature of the gas : ");
        gp3.jl[4].setText("degree K ");
        g1=gp3.gas;
        gn1=gp3.ngas;
        gT=gp3.Tgas;
        Te=gp3.Tegas;
        Pe=gp3.Pegas;
        gib.setgibbs(g1,gn1,gT,Te,Pe);
        gib.getEq();
        output.setText(setArea());
        }
      else
        {caseT=false;
        hhv_lhv.setText(state[1]);
        gp3.jl[3].setText("energy output of the reaction : ");
        gp3.jl[4].setText("KJ/kmol ");
        g1=gp3.gas;
        gn1=gp3.ngas;
        gT=gp3.Tgas;
        Qe=gp3.Tegas;
        Pe=gp3.Pegas;
        Te=gib.set_Q(g1,gn1,gT,Qe,Pe);
        gib.getEq();
        output.setText(setArea());
        }
    }
    repaint();
}
    public static void main(String s[])
    {
        //main program
	    JFrame f = new JFrame("Gibbs free energy minimisation");
       f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
	    JApplet applet = new gibbsTableEN();
        f.getContentPane().add("Center", applet);
        applet.init();
        f.pack();
        f.setSize(new Dimension(1000,600));
        f.setVisible(true);
    }    
 }