//======================================================
// Numerical Analysis package in java
// Plot (Çizim ) programı
// Dr. Turhan Coban
// EGE Üniversitesi Mühendislik Fakultesi, Makina Bölümü
// turhan.coban@mail.ege.edu.tr
// =====================================================
/*
 * Swing version.
 */

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 Plot2D;
//import PlotShapesSW;
//import BasicWindowMonitor;

public class PlotWa extends JFrame implements ItemListener,ActionListener, MouseListener,MouseMotionListener
{
    double xx[];
    double yy[];
    int xn;
	String line="";
	double P=1.01325;
    wetairModel wam;
	private String s="";
	f2 ff=new f2();
	boolean inAnApplet = true;
    final static String KONTROLPANEL = "Control Page       ";
    final static String    PLOTPANEL = "Plot page         ";
    final static String   Psikometri = "Pyschrometry data ";
    Color bg;
    public Plot2D jta;
    double xi1,yi1,xi2,yi2,xi,yi;
    JTable ajt,ajt2;          //table
    JTextArea textA;           //Text Area 
    JLabel altbar;           //writing area   
    JLabel promptXmin;       // Label prompt in Xmin field
    JLabel promptXmax;       // Label prompt in Xmax field
    JLabel promptYmin;       // Label prompt in Ymin field
    JLabel promptYmax;       // Label prompt in Ymax field
    JLabel promptLabel;      // Label prompt Plot Label
    JLabel promptXLabel;     // Label prompt Plot XLabel
    JLabel promptYLabel;     // Label prompt Plot YLabel
    JLabel promptXntic;      // Label prompt in Xmin field
    JLabel promptYntic;      // Label prompt in Xmax field
    JLabel promptXgridon;    // Label prompt in Ymin field
    JLabel promptYgridon;    // Label prompt in Ymax field
    JTextField inputXmin;    // input field Xmin
    JTextField inputXmax;    // input field Xmax
    JTextField inputYmin;    // input field Ymin
    JTextField inputYmax;    // input field Ymax
    JTextField inputLabel;   // input field Label
    JTextField inputXLabel;  // input field XLabel
    JTextField inputYLabel;  // input field YLabel
    JTextField inputXntic;   // input field xntic
    JTextField inputYntic;   // input field yntic
    JCheckBox inputXgridon;  // input field xgridon
    JCheckBox inputYgridon;  // input field ygridon
    JButton printButton;
    JTextArea output; 

    public void setP(double pi)
    {
    P=pi;
	}
    
	public PlotWa()
    {
	super("Plot çizimi");
    xx=new double[200];
    yy=new double[200];
    xn=0;
    String s="";
    s+="Dr. M. Turhan Çoban, \n";
    s+="Ege Üniversitesi, Mühendislik Bilimleri Fak., Makina Bölümü\n";
    s+="phone : 90(232)3434000-5387\n";
    s+="email : turhan.coban@mail.ege.edu.tr\n";
    s+="web   : www.axtelsoft.com/turhan.coban\n";   
    //note if you would like to list additional information on the screen
    //add to string s
    output=new JTextArea(s);    
    Container contentPane = getContentPane();
    output.setBackground(contentPane.getBackground());
    JTabbedPane tabbedPane = new JTabbedPane();
    bg=contentPane.getBackground();
        promptXmin=new JLabel("Xmin ");
        inputXmin=new JTextField(5);
        promptXmax=new JLabel("Xmax ");
        inputXmax=new JTextField(5);
        promptYmin=new JLabel("Ymin ");
        inputYmin=new JTextField(5);
        promptYmax=new JLabel("Ymax ");
        inputYmax=new JTextField(5);
        //*******
        promptLabel=new  JLabel("           Plot başlığı     : ");
        promptXLabel=new JLabel("           x ekseni başlığı : ");
        promptYLabel=new JLabel("           y ekseni başlığı : ");
        inputLabel=new JTextField(30);
        inputXLabel=new JTextField(30);
        inputYLabel=new JTextField(30);
        //*******
        promptXntic=new JLabel("X tik no");
        inputXntic=new JTextField(5);
        promptYntic=new  JLabel("Y tik no");
        inputYntic=new JTextField(5);
        promptXgridon=new JLabel("X grid (küçük kare)");
        inputXgridon=new JCheckBox(" ");
        promptYgridon=new JLabel("Y grid (küçük kare)");
        inputYgridon=new JCheckBox(" ");
        textA=new JTextArea(line,10,30);
        //*******
        JPanel pane1 = new JPanel()
        {
        public Dimension getPrefferedSize()
        {
            Dimension size=super.getPreferredSize();
            size.width=800;
            return size;
        }
        };
        pane1.add(output,BorderLayout.NORTH);
        JPanel mpane=new JPanel();
        mpane.setLayout(new GridLayout(1,8));
        altbar=new JLabel();
        mpane.add(altbar,BorderLayout.SOUTH);
        mpane.add(promptXmin);
        mpane.add(inputXmin);
        mpane.add(promptXmax);
        mpane.add(inputXmax);
        mpane.add(promptYmin);
        mpane.add(inputYmin);
        mpane.add(promptYmax);
        mpane.add(inputYmax);
        pane1.add(mpane,BorderLayout.NORTH);
        JPanel xpane=new JPanel();
        xpane.setLayout(new GridLayout(1,8));
        xpane.add(promptXntic);
        xpane.add(inputXntic);
        xpane.add(promptYntic);
        xpane.add(inputYntic);
        xpane.add(promptXgridon);
        xpane.add(inputXgridon);
        xpane.add(promptYgridon);
        xpane.add(inputYgridon);
        pane1.add(xpane,BorderLayout.NORTH);
        //********
        JPanel lpane=new JPanel();
        lpane.setLayout(new GridLayout(3,2));
        lpane.add(promptLabel);
        lpane.add(inputLabel);
        lpane.add(promptXLabel);
        lpane.add(inputXLabel);
        lpane.add(promptYLabel);
        lpane.add(inputYLabel);
        pane1.add(lpane,BorderLayout.SOUTH);
        //*********
        inputXmin.addActionListener(this);
        inputXmax.addActionListener(this);
        inputYmin.addActionListener(this);
        inputYmax.addActionListener(this);
        inputLabel.addActionListener(this);
        inputXLabel.addActionListener(this);
        inputYLabel.addActionListener(this);
        inputXntic.addActionListener(this);
        inputYntic.addActionListener(this);
        inputXgridon.addItemListener(this);
        inputYgridon.addItemListener(this);
        jta=new Plot2D();
        inputXmin.setText(Double.toString(jta.p1.xmin));
        inputXmax.setText(Double.toString(jta.p1.xmax));
        inputYmin.setText(Double.toString(jta.p1.ymin));
        inputYmax.setText(Double.toString(jta.p1.ymax));
        inputXntic.setText(Integer.toString(jta.p1.xntic));
        inputYntic.setText(Integer.toString(jta.p1.yntic));
        inputLabel.setText(jta.p1.label);
        inputXLabel.setText(jta.p1.xlabel);
        inputYLabel.setText(jta.p1.ylabel);
        printButton=new JButton("Yazdır");
        JPanel pane2 = new JPanel();
        pane2.setLayout(new BorderLayout());
        altbar=new JLabel();
        pane2.add(altbar,BorderLayout.SOUTH);
        pane2.addMouseListener(this);
        pane2.addMouseMotionListener(this);
        
        pane1.add(printButton,BorderLayout.SOUTH);
        printButton.addActionListener(this);
        wam=new wetairModel("SI","tdb_w",20.0,0.004,P);
        line=wam.readName()+"\n";
        line+=wam.readUnit()+"\n";        
        //wam2=new wetairModel("SI","tdb_w",40.0,0.01,P);
        ajt=new JTable(wam);
        //ajt2=new JTable(wam2);
        
        pane2.add(jta,BorderLayout.CENTER);
        //pane2.add(ajt,BorderLayout.WEST);
        pane1.add(ajt,BorderLayout.SOUTH);
        textA=new JTextArea(line,10,80);
        JScrollPane textAP=new JScrollPane(textA);
        pane1.add(textAP,BorderLayout.SOUTH);
        tabbedPane.addTab(PLOTPANEL,    pane2);
        tabbedPane.addTab(KONTROLPANEL, pane1);
        contentPane.add(  tabbedPane, BorderLayout.CENTER);
    }
    
    public PlotWa(Plot1 pi)
    {
    super(pi.label);
	xx=new double[200];
    yy=new double[200];
    xn=0;
    String s="";
    s+="Dr. M. Turhan Çoban, \n";
    s+="Ege Üniversitesi, Mühendislik Bilimleri Fak., Makina Bölümü\n";
    s+="phone : 90(232)3434000-5387\n";
    s+="email : turhan.coban@mail.ege.edu.tr\n";
    s+="web   : www.axtelsoft.com/turhan.coban\n";   
    //note if you would like to list additional information on the screen
    //add to string s
    output=new JTextArea(s);
    Container contentPane = getContentPane();
    output.setBackground(contentPane.getBackground());
    JTabbedPane tabbedPane = new JTabbedPane();
        bg=contentPane.getBackground();
        promptXmin=new JLabel("Xmin ");
        inputXmin=new JTextField(5);
        promptXmax=new JLabel("Xmax ");
        inputXmax=new JTextField(5);
        promptYmin=new JLabel("Ymin ");
        inputYmin=new JTextField(5);
        promptYmax=new JLabel("Ymax ");
        inputYmax=new JTextField(5);
        //*******
        promptLabel=new  JLabel("           Plot başlığı     : ");
        promptXLabel=new JLabel("           x ekseni başlığı : ");
        promptYLabel=new JLabel("           y ekseni başlığı : ");
        inputLabel=new JTextField(30);
        inputXLabel=new JTextField(30);
        inputYLabel=new JTextField(30);
        //*******
        promptXntic=new JLabel("X tik no");
        inputXntic=new JTextField(5);
        promptYntic=new  JLabel("Y tik no");
        inputYntic=new JTextField(5);
        promptXgridon=new JLabel("X grid (küçük kare)");
        inputXgridon=new JCheckBox(" ");
        promptYgridon=new JLabel("Y grid (küçük kare)");
        inputYgridon=new JCheckBox(" ");
        //*******
        JPanel pane1 = new JPanel()
        {
        public Dimension getPrefferedSize()
        {
            Dimension size=super.getPreferredSize();
            size.width=800;
            return size;
        }
        };
        pane1.add(output,BorderLayout.NORTH);
        JPanel mpane=new JPanel();
        mpane.setLayout(new GridLayout(1,8));
        mpane.add(promptXmin);
        mpane.add(inputXmin);
        mpane.add(promptXmax);
        mpane.add(inputXmax);
        mpane.add(promptYmin);
        mpane.add(inputYmin);
        mpane.add(promptYmax);
        mpane.add(inputYmax);
        pane1.add(mpane,BorderLayout.NORTH);
        JPanel xpane=new JPanel();
        xpane.setLayout(new GridLayout(1,8));
        xpane.add(promptXntic);
        xpane.add(inputXntic);
        xpane.add(promptYntic);
        xpane.add(inputYntic);
        xpane.add(promptXgridon);
        xpane.add(inputXgridon);
        xpane.add(promptYgridon);
        xpane.add(inputYgridon);
        pane1.add(xpane,BorderLayout.NORTH);
        //********
        JPanel lpane=new JPanel();
        lpane.setLayout(new GridLayout(3,2));
        lpane.add(promptLabel);
        lpane.add(inputLabel);
        lpane.add(promptXLabel);
        lpane.add(inputXLabel);
        lpane.add(promptYLabel);
        lpane.add(inputYLabel);
        pane1.add(lpane,BorderLayout.SOUTH);
        //*********
        inputXmin.addActionListener(this);
        inputXmax.addActionListener(this);
        inputYmin.addActionListener(this);
        inputYmax.addActionListener(this);
        inputLabel.addActionListener(this);
        inputXLabel.addActionListener(this);
        inputYLabel.addActionListener(this);
        inputXntic.addActionListener(this);
        inputYntic.addActionListener(this);
        inputXgridon.addItemListener(this);
        inputYgridon.addItemListener(this);
        jta=new Plot2D(pi);
        inputXmin.setText(Double.toString(jta.p1.xmin));
        inputXmax.setText(Double.toString(jta.p1.xmax));
        inputYmin.setText(Double.toString(jta.p1.ymin));
        inputYmax.setText(Double.toString(jta.p1.ymax));
        inputXntic.setText(Integer.toString(jta.p1.xntic));
        inputYntic.setText(Integer.toString(jta.p1.yntic));
        inputLabel.setText(jta.p1.label);
        inputXLabel.setText(jta.p1.xlabel);
        inputYLabel.setText(jta.p1.ylabel);
        printButton=new JButton("Yazdır");
        JPanel pane2 = new JPanel();
        pane2.setLayout(new BorderLayout());
        altbar=new JLabel();
        pane2.add(altbar,BorderLayout.SOUTH);
        pane2.addMouseListener(this);
        pane2.addMouseMotionListener(this);        
        pane2.add(jta);
        pane1.add(printButton,BorderLayout.SOUTH);
        wam=new wetairModel("SI","tdb_w",20.0,0.004,P);
        line=wam.readName()+"\n";
        line+=wam.readUnit()+"\n";        
        ajt=new JTable(wam);
        pane1.add(ajt,BorderLayout.SOUTH);
        textA=new JTextArea(line,10,80);
        JScrollPane textAP=new JScrollPane(textA);
        pane1.add(textAP,BorderLayout.SOUTH);
        tabbedPane.addTab(PLOTPANEL,    pane2);
        tabbedPane.addTab(KONTROLPANEL, pane1);
        contentPane.add(  tabbedPane, BorderLayout.CENTER);
    }


    public void itemStateChanged(ItemEvent e)
    {
    inputXmin.setText(Double.toString(jta.p1.xmin));
    inputXmax.setText(Double.toString(jta.p1.xmax));
    inputYmin.setText(Double.toString(jta.p1.ymin));
    inputYmax.setText(Double.toString(jta.p1.ymax));
    inputXntic.setText(Integer.toString(jta.p1.xntic));
    inputYntic.setText(Integer.toString(jta.p1.yntic));
    Object source=e.getItemSelectable();
    if(source==inputXgridon)
    {
     if (e.getStateChange() == ItemEvent.DESELECTED)
     {
     jta.p1.xgridon=0;
     }
     else
     {
      jta.p1.xgridon=1;
     }
    }
    else if(source==inputYgridon)
    {
     if (e.getStateChange() == ItemEvent.DESELECTED)
     {
     jta.p1.ygridon=0;
     }
     else
     {
      jta.p1.ygridon=1;
     }
    }
    inputLabel.setText(jta.p1.label);
    inputXLabel.setText(jta.p1.xlabel);
    inputYLabel.setText(jta.p1.ylabel);
    jta.yenidenciz();
    }

    public void actionPerformed(ActionEvent e)
    {
    if (e.getSource()==printButton )
    {
    jta.yazdir();
    }
    Double valXmin=new Double(inputXmin.getText());
    jta.p1.xmin=valXmin.doubleValue();
    Double valXmax=new Double(inputXmax.getText());
    jta.p1.xmax=valXmax.doubleValue();
    Double valYmin=new Double(inputYmin.getText());
    jta.p1.ymin=valYmin.doubleValue();
    Double valYmax=new Double(inputYmax.getText());
    jta.p1.ymax=valYmax.doubleValue();
    //*****
    Integer valXntic=new Integer(inputXntic.getText());
    jta.p1.xntic=valXntic.intValue();
    Integer valYntic=new Integer(inputYntic.getText());
    jta.p1.yntic=valYntic.intValue();
    //*****
    jta.p1.label=inputLabel.getText();
    jta.p1.xlabel=inputXLabel.getText();
    jta.p1.ylabel=inputYLabel.getText();
    jta.yenidenciz();
    }
      public static void plot()
      {
	  //takes plot data from Plot.txt file    
      PlotWa pencere= new PlotWa();
      pencere.addWindowListener(new BasicWindowMonitor());
      pencere.setSize(1000,800);
      pencere.setVisible(true);
      }
      
      public static void plot(Plot1 pi)
      {
      //take plot data from pi object
	  PlotWa pencere= new PlotWa(pi);
      pencere.addWindowListener(new BasicWindowMonitor());
      pencere.setSize(1000,800);
      pencere.setVisible(true);
      }

      public static void plot(Plot1 pi,double Pi)
      {
      //take plot data from pi object
	  PlotWa pencere= new PlotWa(pi);
	  pencere.setP(Pi);
      pencere.addWindowListener(new BasicWindowMonitor());
      pencere.setSize(1000,800);
      pencere.setVisible(true);
      }
      public static void main(String[] args)
      {
      PlotWa pencere= new PlotWa();
      pencere.addWindowListener(new BasicWindowMonitor());
      pencere.setSize(1000,800);
      pencere.setVisible(true);
      }

      public void degerleriGir(String e,int x,int y)
      {
      s=e;
      xi=jta.p1.xmin+(double)(x-jta.p1.xabsmin)/(double)jta.p1.abswidth*(jta.p1.xmax-jta.p1.xmin);
      yi=jta.p1.ymin-(double)(y-jta.p1.yabsmin-jta.p1.absheight)/(double)jta.p1.absheight*(jta.p1.ymax-jta.p1.ymin);
      xn++;
      xx[xn-1]=xi;yy[xn-1]=yi;
      jta.setXY(xx,yy,xn);
      altbar.setText(e+" ["+toString(xi,5,2)+" , "+toString(yi,6,4)+"] ");
      wam.setValues("SI","tdb_w",xi,yi,P);
      line=wam.readValue()+"\n";
      textA.append(line);
      System.out.print(line);   
      jta.yenidenciz();
      repaint();
      }
      
      public void degerleriGir1(String e,int x,int y)
      {
      s=e;
      xi1=jta.p1.xmin+(double)(x-jta.p1.xabsmin)/(double)jta.p1.abswidth*(jta.p1.xmax-jta.p1.xmin);
      yi1=jta.p1.ymin-(double)(y-jta.p1.yabsmin-jta.p1.absheight)/(double)jta.p1.absheight*(jta.p1.ymax-jta.p1.ymin);
      altbar.setText(e+" ["+toString(xi1,5,2)+" , "+toString(yi1,6,4)+"] ");
      }
      
 public void mouseClicked(MouseEvent e)

 {  }

 public void mousePressed(MouseEvent e)
 { 
   degerleriGir("Basıldı",e.getX(),e.getY());
 }

  public void mouseReleased(MouseEvent e)
 { 
 }

 public void mouseEntered(MouseEvent e)
 { }

 public void mouseExited(MouseEvent e)
 { }

//MouseMotionListener (fare hareket dinleyicisi) metotlari

 public void mouseDragged(MouseEvent e)
 {degerleriGir1("Fare tuş basılı hareket ediyor",e.getX(),e.getY());}
 
 public void mouseMoved(MouseEvent e)
 {degerleriGir1("Fare hareket ediyor",e.getX(),e.getY());}
 
 public static String toString(double left, int w, int d)
  // converts a double to a string with given width and decimals.
  {
	NumberFormat df=NumberFormat.getInstance(Locale.US);
	df.setMaximumFractionDigits(d);
    df.setMinimumFractionDigits(d);
    df.setGroupingUsed(false);
    String s = df.format(left);
    while (s.length() < w)
      s = " " + s;
    if (s.length() > w)
    {
        s = "";
        for (int i=0; i<w; i++)
          s = s + "-";
    }
    return s;
 }
 }

