import java.util.*;
import java.awt.*;

public class PlotComponent extends Panel {
	static int NORMAL = 0;
	static int TIME = 1;

	String curTitle, curXlabel, curYlabel;
	int curXtype, curYtype;
	Vector curData;
	double curXScale, curYScale, curXOffset, curYOffset;
	int height, width;

	PlotComponent() {
		height = 100;
		width = 120;
	}

	public void setData(Vector data) {
		curData = data;
	}

	public void setTitle(String title) {
		curTitle = title;
	}

	public void setLabels(String xlabel, String ylabel){
		curXlabel = xlabel;
		curYlabel = ylabel;
	}

	public void setDataTypes(int xtype, int ytype){
		curXtype = xtype;
		curYtype = ytype;
	}

	public void scale(double xscale, double yscale){
		scale(xscale, yscale, 0, 0);
	}

	public void resize(int w, int h){
		height = h;
		width = w;
	}

	public void scale(double xscale, double yscale,
			  double xoffset, double yoffset){
		curXScale = xscale;
		curYScale = yscale;
		curXOffset = xoffset;
		curYOffset = yoffset;
		System.out.println("Scaling to: "+xscale+", "+yscale+" ("+xoffset+" "+yoffset+")");
	}

	public void autoscale() {
		Enumeration points;
		DataPair d;
		double xmax, xmin, ymax, ymin;
		boolean initialized = false;

		xmin = xmax = ymin = ymax = 0;
		points = curData.elements();
		while(points.hasMoreElements()){
			d = (DataPair) points.nextElement();
			if(initialized){
				xmax = (d.x > xmax) ? d.x:xmax;
				xmin = (d.x < xmin) ? d.x:xmin;
				ymax = (d.yplus > ymax) ? d.yplus:ymax;
				ymin = (d.yminus < ymin) ? d.yminus:ymin;
			}
			else{
				xmax = xmin = d.x;
				ymax = d.yplus;
				ymin = d.yminus;
				initialized = true;
			}
		}
		scale(100.0/(xmax-xmin), 100.0/(ymax-ymin), xmin, ymin);
	}

	public void update(Graphics g){
		System.out.println("Update (conmponent)");
		paint(g);
	}

	public void paint(Graphics g){
		Enumeration points;
		DataPair d;
		double nextX, nextY, lastX, lastY;
		boolean firstpoint;

		height = width = 100;
		System.out.println("Paint (component)");
		lastX = lastY = 0.0;
		firstpoint = true;
		points = curData.elements();
		while(points.hasMoreElements()){
			d = (DataPair) points.nextElement();
			nextX = d.x;
			nextY = d.y;
			if(!firstpoint)
				plotLine(g, lastX, lastY, nextX, nextY);
			firstpoint = false;
			lastX = nextX;
			lastY = nextY;
		}
		points = curData.elements();
		while(points.hasMoreElements()){
			d = (DataPair) points.nextElement();
		}
		System.out.println("Paint (component/done)");
	}

	public void plotLine(Graphics g, double x1, double y1,
			     double x2, double y2){
		
		System.out.println("Plotting a line "+x1+" "+y1+", "+x2+" "+y2); 
		System.out.println("Width: "+width+" Height: "+height);
System.out.println("Plotting in frame "+
(int) (((x1-curXOffset)*curXScale)*width/100.0)+" "+
(int) ((100.0-((y1-curYOffset)*curYScale))*height/100.0)+" "+
(int) (((x2-curXOffset)*curXScale)*width/100.0)+" "+
(int) ((100.0-((y2-curYOffset)*curYScale))*height/100.0));

		g.drawLine((int) (((x1-curXOffset)*curXScale)*width/100.0),
		(int) ((100.0-((y1-curYOffset)*curYScale))*height/100.0),
			(int) (((x2-curXOffset)*curXScale)*width/100.0),
		(int) ((100.0-((y2-curYOffset)*curYScale))*height/100.0));
	}

}

class DataPair {
	double x, y, yplus, yminus;

	DataPair(double xv, double yv, double yp, double ym){
		x = xv;
		y = yv;
		yplus = yp;
		yminus = ym;
	}
}