//======================================================
// Numerical Analysis package in java
// plot output class vplotTable
// This class plot 2D data
// 
// Dr. Turhan Coban
// =====================================================
import java.io.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.awt.geom.*;

public class vplotTable extends JFrame implements ActionListener
{private static final long serialVersionUID =7841845865L;
  public String plotst[]={"0 ______",
"1 __ __ ",
"2 . _ . ",
"3 . _ _ .",
"4 - - - ",
"5 bold _____",
"6 bold __ __",
"7 bold . _ . _",
"8 bold . _ _ . .",
"9 bold - - - ",
"10  "+'\u2295',
"11  "+'\u2297',
"12  "+'\u2299',
"13  "+'\u229A',
"14  "+'\u229B',
"15  "+'\u229E',
"16  "+'\u22A0',
"17  "+'\u22A1',
"18  "+'\u22B9',
"19  "+'\u22C7',
"20  square",  
"21  filled square",
"22  circle",
"23  filled circle",
"24  triangle",
"25  equilateral quadrangle",
"26  pentagon",
"27  hexagon",
"28  filled triangle",
"28  filled equilateral quadrangle",
"30  filled  pentagon",
"31  filled hexagon",
"32  3 sided star",
"33  4 sided star",
"34  5 sided star",
"35  6 sided star",
"36  filled 3 sided star",
"37  filled 4 sided star",
"38  filled 5 sided star",
"39  filled 6 sided star",
"40  bar graphic",
"41  filled bar graphic"}; 
 vplot v;
 JLabel jl[]=new JLabel[10];
 JTextField jtf[]=new JTextField[10];
 JTextArea jta[]=new JTextArea[10];
 JButton jb[]=new JButton[10];
 JComboBox<String> jcb;
 int ncolumn,nrow;
 String filename;
 JPanel jp[]=new JPanel[10];
 JTable jt[]=new JTable[10];
 gm g[]=new gm[10];
 double a[][];
 double c[][];
 int x1array[]={0};
 int y1array[]={1};
 int npoly;
 int ndata;
 double z[][][];
 int iz;
 double R2[];
 int idata;
 int gridnumber=20,ticnumber=10;
        public vplotTable()
        {super("Polynomial Least Square Curve Fitting");
	     z=new double[10][][];
	     npoly=3;
	     ndata=10;
	     iz=0;
	     
	    jl[0]=new JLabel("data file name:");
	    jtf[0]=new JTextField(20);
	    jta[0]=new JTextArea(20,13);
        jcb=new JComboBox<String>(plotst);
        jcb.setSelectedIndex(15);
        idata=jcb.getSelectedIndex();
	    jtf[0].setText("a1.txt");
	    filename="a1.txt";
	    try{
	        double b[][]=veri.inputdouble(filename);
	        a=b;
            } catch(IOException e){System.out.println("veri okuma hatası");}
        jl[1]=new JLabel("x axis column number(s)  ");
        jl[2]=new JLabel("y axis column number(s)  "); 
        jl[3]=new JLabel("degree of polynomial curve fitting  "); 
        jl[4]=new JLabel("number of additional data between points"); 
        jl[5]=new JLabel("input data plot symbol"); 
        jl[6]=new JLabel("Plot window grid number");
        jl[7]=new JLabel("Plot window tic number");
        jtf[1]=new JTextField(20);
        jtf[1].setText(""+0);
        jtf[2]=new JTextField(20);
        jtf[2].setText(""+1);
        jtf[3]=new JTextField(20);
        jtf[3].setText(""+3);
        jtf[4]=new JTextField(20);
        jtf[4].setText(""+10);
        jtf[6]=new JTextField(20);
        jtf[6].setText(""+20);
        jtf[7]=new JTextField(20);
        jtf[7].setText(""+10);
	    jb[0]=new JButton("select file");
	    jp[0]=new JPanel();
	    jp[1]=new JPanel();
	    jp[2]=new JPanel();
	    jp[2].setLayout(new BorderLayout());
	    jp[1].setLayout(new GridLayout(8,2));
	    jp[0].add(jl[0]);
	    jp[0].add(jtf[0]); 
	    jp[0].add(jb[0]);
	    for(int i=1;i<5;i++)
	    {
	    jp[1].add(jl[i]);
	    jp[1].add(jtf[i]); 
	    jtf[i].addActionListener(this); 
        }
        jp[1].add(jl[6]);
	    jp[1].add(jtf[6]); 
	    jp[1].add(jl[7]);
	    jp[1].add(jtf[7]); 
        jp[1].add(jl[5]);
        jp[1].add(jcb);	
	    jb[1]=new JButton("calculate curve fitting coefficients");
        jb[2]=new JButton("input data");
	    jb[3]=new JButton("output data(s)");
	    jb[4]=new JButton("output plot");
	    jp[3]=new JPanel();
	    jp[3].setLayout(new GridLayout(4,1));
	    jp[3].add(jb[1]);
	    jp[3].add(jb[2]);
	    jp[3].add(jb[3]);
	    jp[3].add(jb[4]);
	    jp[2].add(jp[0],BorderLayout.NORTH);  
        jp[2].add(jp[1],BorderLayout.CENTER); 
        jp[2].add(jp[3],BorderLayout.SOUTH);   	    
	    jp[9]=new JPanel(); 
	    jp[9].setLayout(new BorderLayout());
	    jp[9].add(jp[2],BorderLayout.NORTH);
	    jp[9].add(new JScrollPane(jta[0]),BorderLayout.SOUTH);
	    add(jp[9]);
	    jtf[0].addActionListener(this); 
	    jtf[1].addActionListener(this); 
	    jtf[2].addActionListener(this); 
	    jtf[3].addActionListener(this);
	    jtf[4].addActionListener(this); 
	    jtf[6].addActionListener(this);
	    jtf[7].addActionListener(this); 
	    jb[0].addActionListener(this);
	    jb[1].addActionListener(this);
	    jb[2].addActionListener(this);
	    jb[3].addActionListener(this);	 
	    jb[4].addActionListener(this);	
	    jcb.addActionListener(this);   
	    }
	    public void actionPerformed( ActionEvent e1)
        {       
        if(e1.getSource()==jb[0])
        {iz=0;
	        filename=veri.filename();
         	try{
	        double b[][]=veri.inputdouble(filename);
	        a=b;
            } catch(IOException e){System.out.println("veri okuma hatası");}
         jtf[0].setText(filename); 
          int m=a[0].length;
          String b[]=new String[m];
          for(int i=0;i<m;i++) {b[i]="x["+i+"]";}
          Text.printT(a,b);
        }
        else if(e1.getSource()==jtf[0])
        { iz=0;
          filename=jtf[0].getText();
          try{
	        double b[][]=veri.inputdouble(filename);
	        a=b;
            } catch(IOException e){System.out.println("veri okuma hatası");}
           int m=a[0].length;
          String b[]=new String[m];
          for(int i=0;i<m;i++) {b[i]="x["+i+"]";}
          Text.printT(a,b);
        }
        else if(e1.getSource()==jtf[1])
        { x1array=veri.inputInt_fromString(jtf[1].getText());}
        else if(e1.getSource()==jtf[2])
        { y1array=veri.inputInt_fromString(jtf[2].getText());}
        else if(e1.getSource()==jtf[3])
        { npoly=Integer.parseInt(jtf[3].getText());}
        else if(e1.getSource()==jtf[4])
        {
	       ndata=Integer.parseInt(jtf[4].getText());
          String s="";
          
          double c1[][]=new double[x1array.length][];
          double R21[]=new double[x1array.length];
          for(int i=0;i<x1array.length;i++)
          {  c1[i]=SCO11B4.EKK(a[x1array[i]],a[y1array[i]],npoly);
             R21[i]=SCO11B4.R2(a[x1array[i]],a[y1array[i]],c1[i]);
             for(int j=0;j<=npoly;j++)
               {s+="c["+j+"] = "+c1[i][j]+"\n";}
               s+="R2 = "+R21[i]+"\n";
             s+="============================\n";
          }
          c=c1;
          R2=R21;
        }
        else if(e1.getSource()==jb[1])
        { iz=0;
          String s="";
          double c1[][]=new double[x1array.length][];
          double R21[]=new double[x1array.length];
          for(int i=0;i<x1array.length;i++)
          {  c1[i]=SCO11B4.EKK(a[x1array[i]],a[y1array[i]],npoly);
             R21[i]=SCO11B4.R2(a[x1array[i]],a[y1array[i]],c1[i]);
             s+="===data = column "+x1array[i]+" - column "+y1array[i]+"===\n";
             for(int j=0;j<=npoly;j++)
               {s+="c["+j+"] = "+c1[i][j]+"\n";}
               s+="R2 = "+R21[i]+"\n";
             s+="============================\n";
             z[iz]=SCO11B4.funcEKK(a[x1array[i]],a[y1array[i]],npoly,ndata);iz++;
          }  
          c=c1;
          R2=R21;  
          jta[0].setText(s);
          }
        else if(e1.getSource()==jb[2])
        { 
           Text.printT(a,"Original data");
        }
        else if(e1.getSource()==jb[3])
        { 
           for(int ii=0;ii<iz;ii++)
           {
	        Text.printT(z[ii],"data set no : "+ii);
           } 
        }
        else if(e1.getSource()==jb[4])
        {   int plottype1[]={idata,0};
            Color c1[]={Color.blue,Color.black};
            double X[][]={a[x1array[0]],z[0][0]};
            double Y[][]={a[y1array[0]],z[0][1]};
	        vplot vv=new vplot(plottype1,c1,X,Y);
	        vv.setGridNumber(gridnumber);
	        vv.setTicNumber(ticnumber);
           for(int ii=1;ii<iz;ii++)
           {
	        double X1[][]={a[x1array[ii]],z[ii][0]};
            double Y1[][]={a[y1array[ii]],z[ii][1]};
            vv.addPlot(plottype1,c1,X1,Y1);
           } 
           vv.plot();
        }
        else if(e1.getSource()==jcb)
        {   
	        idata=jcb.getSelectedIndex();
        }
        else if(e1.getSource()==jtf[6])
        {gridnumber=Integer.parseInt(jtf[6].getText());}
        else if(e1.getSource()==jtf[7])
        {ticnumber=Integer.parseInt(jtf[7].getText());}
        validate();
        repaint();
        }
        
        public static void main(String arg[])
        {
        vplotTable f = new vplotTable();
        f.setDefaultCloseOperation( JFrame.HIDE_ON_CLOSE );
        f.setSize(800,700);
        f.setVisible(true); 
        }
}