//======================================================
// Thermodynamic package in java
// GmixProperty class to calculate propertes of gas mixtures
// user interface (applet)
// Dr. Turhan Coban
// TUBÝTAK MArmara Research Center
// Energy Systems and Environmental Research Institute
// email : turhan@mam.gov.tr
// =====================================================
import java.io.*;
import Gmix;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.util.*;

public class GmixProperty extends Applet implements ActionListener,ItemListener
{
  protected Panel inputPanel;
  protected Label promptGmixName;         // Label prompt GmixName
  protected Label promptTemperature;      // Label prompt Temperature
  protected Label promptPressure;         // Label prompt Pressure
  protected Choice inputGmixName;      // input GmixName
  protected TextField inputTemperature;   // input Temperature
  protected TextField inputPressure;      // input Pressure
  protected double T;
  protected double P;
  String GmixName;
  protected Gmix g1;
  protected File gmixFile;
  protected StringTokenizer token;

  public void init()
  {
    //adding max-min prompts and input fields
    inputPanel=new Panel();
    inputPanel.setLayout(new GridLayout(3,2));
    promptGmixName=new Label("Gaz karisimi ismi (Gmix.dat) :");
    GmixName="air";
    inputGmixName=    new Choice();
       try{
       g1=new Gmix(GmixName);
       token=new StringTokenizer(g1.readGmixNames());
       } catch(IOException ioe) {System.out.println("IOException");}

    while(token.hasMoreTokens())
    {
    inputGmixName.add(token.nextToken());
    }

    GmixName=inputGmixName.getSelectedItem();
    try{
       g1=new Gmix(GmixName);
       } catch(IOException ioe) {System.out.println("IOException");}
    promptTemperature=new Label("         Temperature (øC) : ");
    inputTemperature=new TextField(20);
    promptPressure=   new Label("         Pressure (Bars)  :  ");
    inputPressure=new TextField(20);
    T=27.0;
    P=1.0;
    inputGmixName.addItemListener(this);
    inputTemperature.addActionListener(this);
    inputPressure.addActionListener(this);
    inputPanel.add(promptGmixName);
    inputPanel.add(inputGmixName);
    inputPanel.add(promptTemperature);
    inputPanel.add(inputTemperature);
    inputPanel.add(promptPressure);
    inputPanel.add(inputPressure);
    setLayout(new BorderLayout());
    add(inputPanel,BorderLayout.NORTH);
  }

  public void itemStateChanged(ItemEvent ev)
  {
  GmixName=inputGmixName.getSelectedItem();
  try{
  g1=new Gmix(GmixName);
  } 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("new gas mixtures can be added to Gmix.dat data file ",20,100);
    g.drawString(g1.toString("formula"),20,115);
    g.drawString(g1.toString("composition"),20,130);
    g.drawString("======================================================",10,140);
    g.drawString("M                        kg/kmol  : ",20,150);
    g.drawString(""+g1.M,250,150);
    g.drawString("h, enthalpy              KJ/kmol  : ",20,165);
    g.drawString(Double.toString(g1.h(TK)),250,165);
    g.drawString("u, Internal energy       KJ/kmol  : ",20,180);
    g.drawString(Double.toString(g1.u(TK)),250,180);
    g.drawString("s, Entropy               KJ/kmol K: ",20,195);
    g.drawString(Double.toString(g1.s(TK,P)),250,195);
    g.drawString("v, Specific volume       m^3/kmol : ",20,210);
    g.drawString(Double.toString(g1.v(TK,P)),250,210);
    g.drawString("k, Thermal conductivity  W/m K    : ",20,225);
    g.drawString(Double.toString(g1.k(TK)),250,225);
    g.drawString("h+hf, ent + form. enth. KJ/kmol : ",20,240);
    g.drawString(Double.toString(g1.ht(TK)),250,240);
    g.drawString("Density   kg/m^3                  : ",20,255);
    g.drawString(Double.toString(g1.M/g1.v(TK,P)),250,255);
    g.drawString("Cp                      KJ/kmol K : ",20,270);
    g.drawString(Double.toString(g1.Cp(TK)),250,270);
    g.drawString("Cv                      KJ/kmol K : ",20,285);
    g.drawString(Double.toString(g1.Cv(TK)),250,285);
    g.drawString("Cp/Cv adiabatic constant          : ",20,300);
    g.drawString(Double.toString(g1.gamma(TK)),250,300);
    g.drawString("c speed of sound m/s              : ",20,315);
    g.drawString(Double.toString(g1.c(TK)),250,315);
    g.drawString("Viscosity                N s/m^2  : ",20,330);
    g.drawString(Double.toString(g1.vis(TK)),250,330);
    g.drawString("Pr,Prandtl Number                 : ",20,345);
    g.drawString(Double.toString(g1.Pr(TK)),250,345);
    }//end of method

public void actionPerformed( ActionEvent e)
{
  /*
  try{
  g1=new Gmix(GmixName);
  } catch(IOException ioe1) {System.out.println("IOException");}
  */
  Double valTemp=new Double(inputTemperature.getText());
  T=valTemp.doubleValue();
  Double valPressure=new Double(inputPressure.getText());
  P=valPressure.doubleValue();
  repaint();
}


}



