//======================================================
// Thermodynamics package in java
// GasProperty class to calculate properties of gases
// user interface (applet)
// Dr. Turhan Coban
// University of Nebraska - Lincoln
// Department of Mechanical Engineering
// N106 Walter Scott Engineering Center
// PO Box 880656
// Lincoln, NE 68588-0656
// email : MCOBAN2@netscape.net
// =====================================================

import java.io.*;
import Gas;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Gas_SI_mass extends Applet implements ActionListener,ItemListener
{
  Panel inputPanel;
  Label promptGasName;         // Label prompt GasName
  Label promptTemperature;     // Label prompt Temperature
  Label promptPressure;        // Label prompt Pressure
  Choice inputGasName;         // input GasName
  TextField inputTemperature;  // input (from list )Temperature
  TextField inputPressure;     // input Pressure
  double T;
  double P;
  String gasName;
  Gas g1;
  protected File gmixFile;
  protected StringTokenizer token;

  public void init()
  {
    //adding max-min prompts and input fields
    gasName=new String("air");
    T=27.0;
    P=1.0;
    inputPanel=new Panel();
    inputPanel.setLayout(new GridLayout(3,2));
    inputGasName=    new Choice();
    try{
       g1=new Gas(gasName);
       token=new StringTokenizer(g1.readGasNames());
       } catch(IOException ioe) {System.out.println("IOException");}
    while(token.hasMoreTokens())
    {
    inputGasName.add(token.nextToken());
    }
     gasName=inputGasName.getSelectedItem();
    try{
       g1=new Gas(gasName);
       } catch(IOException ioe) {System.out.println("IOException");}
    promptGasName=    new Label("         Simple gas Name ");
    promptTemperature=new Label("         Temperature ( C) : ");
    inputTemperature=new TextField(20);
    promptPressure=   new Label("         Pressure (Bars)  :  ");
    inputPressure=new TextField(20);
    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);
    setLayout(new BorderLayout());
    add(inputPanel,BorderLayout.NORTH);
  }

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

  public void paint(Graphics g)
  {
    double TK=T+273.0;
    inputTemperature.setText(Double.toString(T));
    inputPressure.setText(Double.toString(P));
    g.drawString("additional data can be added to Gas.txt file",20,100);
    g.drawString("=======================================================",20,115);
    g.drawString("Gas Formula : "+g1.toString(),20,130);
    g.drawString("M                        kg/kmol  : ",20,150);
    g.drawString(""+g1.M,250,150);
    g.drawString("h, enthalpy              KJ/kg  : ",20,165);
    g.drawString(Double.toString(g1.h(TK)/g1.M),250,165);
    g.drawString("u, Internal energy       KJ/kg  : ",20,180);
    g.drawString(Double.toString(g1.u(TK)/g1.M),250,180);
    g.drawString("s, Entropy               KJ/kg K: ",20,195);
    g.drawString(Double.toString(g1.s(TK,P)/g1.M),250,195);
    g.drawString("pr, reduced pressure              : ",20,210);
    g.drawString(Double.toString(g1.pr(TK)),250,210);
    g.drawString("vr, reduced volume                : ",20,225);
    g.drawString(Double.toString(g1.vr(TK)),250,225);
    g.drawString("v, Specific volume       m^3/kg : ",20,240);
    g.drawString(Double.toString(g1.v(TK,P)/g1.M),250,240);
    g.drawString("k, Thermal conductivity  W/m K    : ",20,255);
    g.drawString(Double.toString(g1.k(TK)),250,255);
    g.drawString("h+hf, enthalpy + form. enth. KJ/kg : ",20,270);
    g.drawString(Double.toString(g1.ht(TK)),250,270);
    g.drawString("Density   kg/m^3                  : ",20,285);
    g.drawString(Double.toString(g1.M/g1.v(TK,P)),250,285);
    g.drawString("Cp                      KJ/kmol K : ",20,300);
    g.drawString(Double.toString(g1.Cp(TK)/g1.M),250,300);
    g.drawString("Cv                      KJ/kg K : ",20,315);
    g.drawString(Double.toString(g1.Cv(TK)),250,315);
    g.drawString("Cp/Cv adiabatic constant          : ",20,330);
    g.drawString(Double.toString(g1.gamma(TK)),250,330);
    g.drawString("c speed of sound m/s              : ",20,345);
    g.drawString(Double.toString(g1.c(TK)),250,345);
    g.drawString("Viscosity                N s/m^2  : ",20,360);
    g.drawString(Double.toString(g1.vis(TK)),250,360);
    g.drawString("Pr,Prandtl Number                 : ",20,375);
    g.drawString(Double.toString(g1.Prandtl(TK)),250,375);
    }//end of method

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





