// ======================================================
// Thermodynamics package in java
// GasProperty class to calculate properties of gases
// user interface (applet)
// Dr. Turhan Coban
// TUBITAK Marmara Research Center
// Energy Systems and Environmental Research Institute
// email : turhan@mam.gov.tr
// =====================================================

import java.io.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class GasPropertySW extends JApplet implements ActionListener,ItemListener
{
  JPanel inputPanel;
  JPanel outputPanel;
  JPanel totalPanel;
  JTextField promptGasName;     // Label prompt GasName
  JTextField promptTemperature; // Label prompt Temperature
  JTextField promptPressure;    // Label prompt Pressure
  JComboBox inputGasName;       // input GasName
  JTextField inputTemperature;  // input (from list )Temperature
  JTextField inputPressure;     // input Pressure
  JTextArea outputTextArea;
  double T;
  double P;
  String gasName;
  Gas g1;
  protected File gmixFile;
  protected StringTokenizer token;
  String st[];
    final static Color bg=Color.lightGray;
    final static Color fg=Color.black;
    final static Color kirmizi=Color.red;
    final static Color beyaz=Color.white;

  public void init()
  {
    //setBackground(bg);
    Container c=getContentPane();
    c.setLayout(new FlowLayout());
    //adding max-min prompts and input fields
    gasName=new String("air");
    T=27.0;
    P=1.0;
    inputPanel=new JPanel();
    inputPanel.setLayout(new GridLayout(3,2));
    outputPanel=new JPanel();
    totalPanel=new JPanel();
    totalPanel.setLayout(new BorderLayout());
    try{
       g1=new Gas(gasName);
       token=new StringTokenizer(g1.readGasNames());
       st=new String[token.countTokens()];
       } catch(IOException ioe) {System.out.println("IOException");}
    int i=0;
    while(token.hasMoreTokens())
    {
    st[i++]=new String((String)token.nextToken());
    }
    inputGasName=new JComboBox(st);
    gasName=(String)inputGasName.getSelectedItem();
    try{
       g1=new Gas(gasName);
       } catch(IOException ioe) {System.out.println("IOException");}
    promptGasName=    new JTextField(20);
    promptGasName.setText("Simple gas Name  : ");
    promptGasName.setBackground(Color.lightGray);
    promptTemperature=new JTextField(20);
    promptTemperature.setText("Sicaklik  øC : ");
    promptTemperature.setBackground(Color.lightGray);
    inputTemperature=new JTextField(20);
    inputTemperature.setBackground(Color.lightGray);
    promptPressure=  new JTextField(20);
    promptPressure.setText("Pressure (Bars)  : ");
    promptPressure.setBackground(Color.lightGray);
    outputTextArea = new JTextArea(20,20);
    outputTextArea.setBackground(Color.lightGray);
    inputPressure=new JTextField(20);
    inputPressure.setBackground(Color.lightGray);
    inputPanel.add(promptGasName);
    inputPanel.add(inputGasName);
    inputPanel.add(promptTemperature);
    inputPanel.add(inputTemperature);
    inputPanel.add(promptPressure);
    inputPanel.add(inputPressure);
    inputGasName.addItemListener(this);
    inputTemperature.addActionListener(this);
    inputPressure.addActionListener(this);
    setArea();
    outputPanel.add(outputTextArea);
    totalPanel.add(inputPanel,BorderLayout.NORTH);
    totalPanel.add(outputPanel,BorderLayout.SOUTH);
    c.add(totalPanel,BorderLayout.NORTH);
  }

 public void setArea()
 {
    double TK=T+273.0;
    inputTemperature.setText(Double.toString(T));
    inputPressure.setText(Double.toString(P));
    String s="additional data can be added to Gas.txt file \n";
    s+="=======================================================\n";
    s+="Gas Formula : "+g1.toString()+"\n";
    s+="M                        kg/kmol   : "+g1.M+"\n";
    s+="h, enthalpy              KJ/kg     : "+g1.h(TK)/g1.M+"\n";
    s+="u, Internal energy       KJ/kg     : "+Double.toString(g1.u(TK)/g1.M)+"\n";
    s+="s, Entropy               KJ/kg K   : "+Double.toString(g1.s(TK,P)/g1.M)+"\n";
    s+="pr, reduced pressure               : "+Double.toString(g1.pr(TK))+"\n";
    s+="vr, reduced volume                 : "+Double.toString(g1.vr(TK))+"\n";
    s+="v, Specific volume       m^3/kg    : "+Double.toString(g1.v(TK,P)/g1.M)+"\n";
    s+="k, Thermal conductivity  W/m K     : "+Double.toString(g1.k(TK))+"\n";
    s+="h+hf, enthalpy + form. enth. KJ/kg : "+Double.toString(g1.ht(TK))+"\n";
    s+="Density   kg/m^3                   : "+Double.toString(g1.M/g1.v(TK,P))+"\n";
    s+="Cp                      KJ/kmol K  : "+Double.toString(g1.Cp(TK)/g1.M)+"\n";
    s+="Cv                      KJ/kg K    : "+Double.toString(g1.Cv(TK)/g1.M)+"\n";
    s+="k=Cp/Cv adiabatic constant         : "+Double.toString(g1.gamma(TK))+"\n";
    s+="c speed of sound m/s               : "+Double.toString(g1.c(TK))+"\n";
    s+="Viscosity                N s/m^2   : "+Double.toString(g1.vis(TK))+"\n";
    s+="Pr,Prandtl Number                  : "+Double.toString(g1.Prandtl(TK))+"\n";
    s=s+"=======================================================\n";
    s=s+"h, enthalpy              KJ/kmol     : ";
    s=s+Double.toString(g1.h(TK))+"\n";
    s=s+"u, Internal energy       KJ/kmol     : ";
    s=s+Double.toString(g1.u(TK))+"\n";
    s=s+"s, Entropy               KJ/kmol K   : ";
    s=s+Double.toString(g1.s(TK,P))+"\n";
    s=s+"v, Specific volume       m^3/kmol    : ";
    s=s+Double.toString(g1.v(TK,P))+"\n";
    s=s+"h+hf, enthalpy + form. enth. KJ/kmol : ";
    s=s+Double.toString(g1.ht(TK))+"\n";
    s=s+"Cp                      KJ/kmol K    : ";
    s=s+Double.toString(g1.Cp(TK))+"\n";
    s=s+"Cv                      KJ/kmol K    : ";
    s=s+Double.toString(g1.Cv(TK))+"\n";
    outputTextArea.setText(s);

 }

  public void itemStateChanged(ItemEvent ev)
  {
  gasName=(String)inputGasName.getSelectedItem();
  try{
  g1=new Gas(gasName);
  } catch(IOException ioe1) {System.out.println("IOException");}
  setArea();
  }


public void actionPerformed( ActionEvent e)
{
  Double valTemp=new Double(inputTemperature.getText());
  T=valTemp.doubleValue();
  Double valPressure=new Double(inputPressure.getText());
  P=valPressure.doubleValue();
  getContentPane().setLayout(new FlowLayout(FlowLayout.RIGHT));
  setArea();
}

public static void main(String s[]) {
        JFrame f = new JFrame("Properties of perfect gases");
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
        });
        JApplet applet = new GasPropertySW();
        f.getContentPane().add("Center", applet);
        applet.init();
        f.pack();
        f.setSize(new Dimension(500,600));
        f.show();
    }

}





