//======================================================
// Numerical Analysis package in java
// PlotApplet class to plot 2D data
// Dr. Turhan Coban
// =====================================================

import java.lang.Integer;
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.*;
import PlotShapes;
import Plot;


public class PlotAppletSW extends JApplet implements ActionListener
{

  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
  JTextField inputXmin;  // input field Xmin
  JTextField inputXmax;  // input field Xmax
  JTextField inputYmin;  // input field Ymin
  JTextField inputYmax;  // input field Ymax
  Plot p1;

  public void init()
  {   
    //adding max-min prompts and input fields
    //label
    //xaxis
    //yaxis
    // 4 10 10 1 1
    // b.dat 22 0 0 0
    // a.dat 21 0 255 0
    // a.dat 0 0 255 0
    // b.dat 0 0 0 255
    int xnt=10;
    int ynt=10;
    int xgo=1;
    int ygo=1;
    String il=new String("label");
    String xl=new String("x axis");
    String yl=new String("y axis");
    String ifn[]=new String[4];
    ifn[0]="b.dat";
    ifn[1]="a.dat";
    ifn[2]="a.dat";
    ifn[3]="b.dat";
    int ipt[]=new int[4];
    int ib[]=new int[4];
    int ir[]=new int[4];
    int ig[]=new int[4];
    ipt[0]=22;
    ipt[1]=21;
    ipt[2]=0;
    ipt[3]=0;
    ir[0]=0;
    ig[0]=0;
    ib[0]=0;
    ir[1]=0;
    ig[1]=255;
    ib[1]=0;
    ir[2]=0;
    ig[2]=255;
    ib[2]=0;
    ir[3]=0;
    ig[3]=255;
    ib[3]=0;
    try{
       //p1=new Plot(il,xl,yl,xnt,ynt,xgo,ygo,ifn,ipt,ir,ig,ib);
       p1=new Plot();
       } catch(IOException ioe) {System.err.println("IOException in opening Plot");}
    promptXmin=new Label("Xmin ");
    inputXmin=new TextField(5);
    promptXmax=new Label("Xmax ");
    inputXmax=new TextField(5);
    promptYmin=new Label("Ymin ");
    inputYmin=new TextField(5);
    promptYmax=new Label("Ymax ");
    inputYmax=new TextField(5);
    inputXmin.addActionListener(this);
    inputXmax.addActionListener(this);
    inputYmin.addActionListener(this);
    inputYmax.addActionListener(this);
    add(promptXmin);
    add(inputXmin);
    add(promptXmax);
    add(inputXmax);
    add(promptYmin);
    add(inputYmin);
    add(promptYmax);
    add(inputYmax);
  }

  public void paint(Graphics g)
  {
    inputXmin.setText(Double.toString(p1.xmin));
    inputXmax.setText(Double.toString(p1.xmax));
    inputYmin.setText(Double.toString(p1.ymin));
    inputYmax.setText(Double.toString(p1.ymax));
    PlotShapes ps=new PlotShapes(g,p1.xabsmin,p1.yabsmin,
    p1.absheight,p1.abswidth,p1.xmin,p1.xmax,p1.ymin,p1.ymax);
    g.drawRect(p1.xabsmin,p1.yabsmin,p1.abswidth,p1.absheight);
    ps.drawXTic(p1.xntic,(p1.ymax-p1.ymin)/80.0,0);
    ps.drawYTic(p1.yntic,(p1.xmax-p1.xmin)/80.0,0);
    ps.drawXNumbers(p1.xntic);
    ps.drawYNumbers(p1.xntic);
    ps.drawLabel(p1.label);
    ps.drawXLabel(p1.xlabel);
    ps.drawYLabel(p1.ylabel);
    if(p1.xgridon!=0)
      ps.drawXGrid(p1.xntic);
    if(p1.ygridon!=0)
      ps.drawYGrid(p1.yntic);
    int i,j;
    for(i=0;i<p1.nline;i++)
    {
    //  Select plot colors
    g.setColor(new Color(p1.red[i],p1.green[i],p1.blue[i]));
    ps.drawPlotLines(i,p1.plottype,p1.x,p1.y,p1.n,p1.ch);
      }//end of for(i=0

    }//end of method

public void actionPerformed( ActionEvent e)
{
  Double valXmin=new Double(inputXmin.getText());
  p1.xmin=valXmin.doubleValue();
  Double valXmax=new Double(inputXmax.getText());
  p1.xmax=valXmax.doubleValue();
  Double valYmin=new Double(inputYmin.getText());
  p1.ymin=valYmin.doubleValue();
  Double valYmax=new Double(inputYmax.getText());
  p1.ymax=valYmax.doubleValue();
  repaint();
}

}
