//======================================================
// Numerical Analysis package in java
// Plot (Çizim ) programı
// Dr. Turhan Coban
// EGE Üniversitesi Mühendislik Fakultesi, Makina Bölümü
// turhan.coban@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.io.*;
//import Plot2D;
//import PlotShapesSW;
//import BasicWindowMonitor;

public class PlotW extends JFrame implements ItemListener,ActionListener, MouseListener,MouseMotionListener
{
    private String s="";
	//f2 ff=new f2();
	boolean inAnApplet = true;
    final static String KONTROLPANEL = "Kontrol sayfası";
    final static String    PLOTPANEL = "Plot sayfası   ";
    Plot2D jta;
    double xi1,yi1,xi2,yi2;
    JPanel p1,p2,p3;
    int n;
    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
    JLabel promptXlogon;     // x axis log scale on
    JLabel promptYlogon;     // y axis log scale on
    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
    JCheckBox inputXlogon;  // check field xlogon
    JCheckBox inputYlogon;  // check field ylogon    
    JButton printButton;
	JLabel lab1[];
    JComboBox c1[];
    JTextField red1[];
    JTextField green1[];
    JTextField blue1[];
    JButton col1[];

    public PlotW()
    {
    super("Plot çizimi");
        try{Plot pi=new Plot();plotWkur(pi); } catch(IOException e)
       {
       System.err.println("Error Opening File \n"+e.toString());
       System.exit(1);
       }    
    
    }
    
    public PlotW(Plot pi)
    {
    super(pi.label);
    plotWkur(pi);
    }

    public void plotWkur(Plot pi)
    {
        n=pi.nline;
    
    lab1=new JLabel[n];
    c1=new JComboBox[n];
    red1=new JTextField[n];
    green1=new JTextField[n];
    blue1=new JTextField[n];
    col1=new JButton[n];
    //System.out.println("n="+n);
    for(int i=0;i<n;i++)
    {
	Color color1=new Color(pi.red[i],pi.green[i],pi.blue[i]);
	lab1[i]=new JLabel("eğri "+i+" ");
    c1[i]=new JComboBox(pi.plotst);
    c1[i].setSelectedIndex(pi.plottype[i]);
    red1[i]=new JTextField(""+pi.red[i]);
    green1[i]=new  JTextField(""+pi.green[i]);
    blue1[i]=new  JTextField(""+pi.blue[i]);
    col1[i]=new JButton(" ");
    col1[i].setBackground(color1);
    }
    Container contentPane = getContentPane();
    JTabbedPane tabbedPane = new JTabbedPane();
        promptXmin=new JLabel("Xmin ");
        inputXmin=new JTextField(10);
        promptXmax=new JLabel("Xmax ");
        inputXmax=new JTextField(10);
        promptYmin=new JLabel("Ymin ");
        inputYmin=new JTextField(10);
        promptYmax=new JLabel("Ymax ");
        inputYmax=new JTextField(10);
        //*******
        promptLabel=new  JLabel("           Plot başlığı     : ");
        promptXLabel=new JLabel("           x ekseni başlığı : ");
        promptYLabel=new JLabel("           y ekseni başlığı : ");
        inputLabel=new JTextField(40);
        inputXLabel=new JTextField(40);
        inputYLabel=new JTextField(40);
        //*******
        promptXntic=new JLabel("X tik no");
        inputXntic=new JTextField(10);
        promptYntic=new  JLabel("Y tik no");
        inputYntic=new JTextField(10);
        
        promptXgridon=new JLabel("X grid (küçük kare)");
        if(pi.xgridon!=0) inputXgridon=new JCheckBox("   ",true);
        else              inputXgridon=new JCheckBox("   ",false);
        
        promptYgridon=new JLabel("Y grid (küçük kare)");
        if(pi.ygridon!=0) inputYgridon=new JCheckBox("   ",true);
        else              inputYgridon=new JCheckBox("   ",false);
        
        promptXlogon=new JLabel("x log ");    
        promptYlogon=new JLabel("y log ");   
        if(pi.xlog!=0) inputXlogon=new JCheckBox("   ",true);
        else            inputXlogon=new JCheckBox("   ",false);
        if(pi.ylog!=0) inputYlogon=new JCheckBox("   ",true);
        else            inputYlogon=new JCheckBox("   ",false);
          // check field xlogon
        //*******
        JPanel pane1 = new JPanel()
        {
        public Dimension getPrefferedSize()
        {
            Dimension size=super.getPreferredSize();
            size.width=800;
            return size;
        }
        };
        JPanel mpane=new JPanel();
        mpane.setLayout(new GridLayout(5,4));
        mpane.add(promptXmin);
        mpane.add(inputXmin);
        mpane.add(promptXmax);
        mpane.add(inputXmax);
        mpane.add(promptYmin);
        mpane.add(inputYmin);
        mpane.add(promptYmax);
        mpane.add(inputYmax);
        mpane.add(promptXntic);
        mpane.add(inputXntic);
        mpane.add(promptYntic);
        mpane.add(inputYntic);
        mpane.add(promptXgridon);
        mpane.add(inputXgridon);
        mpane.add(promptYgridon);
        mpane.add(inputYgridon);
        mpane.add(promptXlogon);
        mpane.add(inputXlogon);
        mpane.add(promptYlogon);
        mpane.add(inputYlogon);
        pane1.add(mpane,BorderLayout.NORTH);
        JPanel xpane=new JPanel();
        xpane.setLayout(new GridLayout(n,6));
        for(int i=0;i<n;i++)
        { 
	    //c1.setBackground(new Color(pi.red[i],pi.green[i],pi.blue[i]));    
	    xpane.add(lab1[i]);
        xpane.add(c1[i]);
        xpane.add(red1[i]);
        xpane.add(green1[i]);
        xpane.add(blue1[i]);
        xpane.add(col1[i]);
        }
        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);
        //*********
        for(int i=0;i<n;i++)
        { 
	    //c1.setBackground(new Color(pi.red[i],pi.green[i],pi.blue[i]));    
	     c1[i].addItemListener(this);
	     col1[i].addActionListener(this);
         red1[i].addActionListener(this);
         green1[i].addActionListener(this);
         blue1[i].addActionListener(this);
        }        

        //**********
        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);
        inputXlogon.addItemListener(this);
        inputYlogon.addItemListener(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);
      
        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);
        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;}
    }
    else if(source==inputXlogon)
    {
        if (e.getStateChange() == ItemEvent.DESELECTED)
        {jta.p1.xlog=0;}
        else
        {jta.p1.xlog=1;}
    }
    else if(source==inputYlogon)
    {
       if (e.getStateChange() == ItemEvent.DESELECTED)
       {jta.p1.ylog=0;}
       else
       {jta.p1.ylog=1;}
    }
    else
    {
    for(int i=0;i<n;i++)
    {   if(source==c1[i])
	    {jta.p1.plottype[i]=c1[i].getSelectedIndex();
	     if(jta.p1.plottype[i] == 10) jta.p1.ch[i]='*';//you can change character set by using setPlotType(0,'%');
         if(jta.p1.plottype[i] == 11) jta.p1.ch[i]='#';
         if(jta.p1.plottype[i] == 12) jta.p1.ch[i]='$';
         if(jta.p1.plottype[i] == 13) jta.p1.ch[i]='%';
         if(jta.p1.plottype[i] == 14) jta.p1.ch[i]='&';
         if(jta.p1.plottype[i] == 15) jta.p1.ch[i]='-';
         if(jta.p1.plottype[i] == 16) jta.p1.ch[i]='~';
         if(jta.p1.plottype[i] == 17) jta.p1.ch[i]='+';
         if(jta.p1.plottype[i] == 18) jta.p1.ch[i]='x';
         if(jta.p1.plottype[i] == 19) jta.p1.ch[i]='=';     }
    }    
    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();}
    else if (e.getSource()==inputXmin )
    {Double valXmin=new Double(inputXmin.getText());
    jta.p1.xmin=valXmin.doubleValue();}    
    else if (e.getSource()==inputXmax )
    {Double valXmax=new Double(inputXmax.getText());
    jta.p1.xmax=valXmax.doubleValue();}    
    else if (e.getSource()==inputYmin )
    {Double valYmin=new Double(inputYmin.getText());
    jta.p1.ymin=valYmin.doubleValue();}    
    else if (e.getSource()==inputYmax )
    {Double valYmax=new Double(inputYmax.getText());
    jta.p1.ymax=valYmax.doubleValue();}
    else if (e.getSource()==inputXntic )
    {Integer valXntic=new Integer(inputXntic.getText());
    jta.p1.xntic=valXntic.intValue();}
    else if (e.getSource()==inputYntic )
    {Integer valYntic=new Integer(inputYntic.getText());
    jta.p1.yntic=valYntic.intValue();}
    else if (e.getSource()==inputLabel )
    {jta.p1.label=inputLabel.getText();}
    else if (e.getSource()==inputXLabel )
    {jta.p1.label=inputXLabel.getText();}
    else if (e.getSource()==inputYLabel )
    {jta.p1.label=inputYLabel.getText();}
    //*****

    for(int i=0;i<n;i++)
    {
	Color color1=new Color(jta.p1.red[i],jta.p1.green[i],jta.p1.blue[i]);    
	Color color2;
	if (e.getSource()==red1[i] )
    {Integer valred=new Integer(red1[i].getText());
    jta.p1.red[i]=valred.intValue();}       
    else if (e.getSource()==green1[i] )
    {Integer valgreen=new Integer(green1[i].getText());
    jta.p1.green[i]=valgreen.intValue();} 
    else if (e.getSource()==blue1[i] )
    { Integer valblue=new Integer(blue1[i].getText());}
    else if (e.getSource()==col1[i] )
    { color2=JColorChooser.showDialog(null,"grafik programı eğri "+i,color1 );
    jta.p1.red[i]=color2.getRed();jta.p1.green[i]=color2.getGreen();jta.p1.blue[i]=color2.getBlue();
    red1[i].setText(""+jta.p1.red[i]);green1[i].setText(""+jta.p1.green[i]);blue1[i].setText(""+jta.p1.blue[i]);   
    }         
     color2=new Color(jta.p1.red[i],jta.p1.green[i],jta.p1.blue[i]); 
     col1[i].setBackground(color2);
    }    
    //*****
    jta.yenidenciz();
    }
      public void degerleriGir(String e,int x,int y)
      {
      s=e;
      if(jta.p1.xlog!=0)
      {xi1=jta.p1.xmin+Math.pow(10.0,(double)((x-jta.p1.xabsmin)/(double)jta.p1.abswidth*(double)jta.p1.xminmaxlog+1.0))-10.0; }
      else
      {xi1=jta.p1.xmin+(double)(x-jta.p1.xabsmin)/(double)jta.p1.abswidth*(jta.p1.xmax-jta.p1.xmin);}
      if(jta.p1.ylog!=0)
      {yi1=jta.p1.ymin-10.0+Math.pow(10.0,((double)(1.0-(double)(y-jta.p1.yabsmin-jta.p1.absheight)/(double)jta.p1.absheight*(double)jta.p1.yminmaxlog))); }
      else
      {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+" ["+xi1+" , "+yi1+"] ");
      //jta.yenidenciz();
      }
      
      public void mouseClicked(MouseEvent e)
      { degerleriGir("Kliklendi",e.getX(),e.getY()); }

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

      public void mouseReleased(MouseEvent e)
      { degerleriGir("Bırakıldı",e.getX(),e.getY()); }

      public void mouseEntered(MouseEvent e)
      { setTitle("Fare applet alanı içinde"); }

      public void mouseExited(MouseEvent e)
      { setTitle("Fare applet alanı dışında");
        degerleriGir("Fare applet alanı dışında",e.getX(),e.getY());
      }
      //MouseMotionListener (fare hareket dinleyicisi) metotlari

      public void mouseDragged(MouseEvent e)
      { degerleriGir("basilip cekiliyor",e.getX(),e.getY()); }

      public void mouseMoved(MouseEvent e)
      { degerleriGir("hareket halinde",e.getX(),e.getY()); }      
 
      public static void plot()
      {
	  //takes plot sata from Plot.txt file    
      PlotW pencere= new PlotW();
      pencere.addWindowListener(new BasicWindowMonitor());
      pencere.setSize(1000,800);
      pencere.setVisible(true);
      }
      
      public static void plot(Plot pi)
      {
      //take plot data from pi object
	  PlotW pencere= new PlotW(pi);
      pencere.addWindowListener(new BasicWindowMonitor());
      pencere.setSize(1000,800);
      pencere.setVisible(true);
      }
 
              
      public static void main(String[] args)
      {
      PlotW pencere= new PlotW();
      pencere.addWindowListener(new BasicWindowMonitor());
      pencere.setSize(1000,800);
      pencere.setVisible(true);
      }
      

}

