All pastes #819648 Raw Edit

xygrapher

public java v1 · immutable
·published 2007-12-17 02:54 UTC
rendered paste body
package sixteen;import java.awt.Color;import java.awt.Frame;import java.awt.Graphics;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;public abstract class XYGrapher extends Frame implements WindowListener{	private int xpixelStart;	private int ypixelStart;	private int pixelshigh;	private int pixelswide;	abstract protected Coordinate xyStart(); // Where the graph will start (LLC)	abstract protected double xRange(); // Width + height of graphing area	abstract protected double yRange();	abstract protected Coordinate getPoint(int pointNum);		public void init(){		this.setVisible(true);	}		public void windowClosed(WindowEvent e){		//System.exit(0);	}		public void windowClosing(WindowEvent e){		//System.exit(0);	}		public void drawGraph(int xPixelStart, 			int yPixelStart, int pixelsWide, 			int pixelsHigh){		xpixelStart = xPixelStart;		ypixelStart = yPixelStart;		pixelswide = pixelsWide;		pixelshigh = pixelsHigh;	}	public void paint(Graphics gr){		int yPixelStart = ypixelStart;		int xPixelStart = xpixelStart;		int pixelsWide = pixelswide;		int pixelsHigh = pixelshigh;		yPixelStart = yPixelStart + 10;		this.setSize(pixelsWide + xPixelStart, pixelsHigh+ yPixelStart);		pixelsWide = pixelsWide + xPixelStart;		pixelsHigh = pixelsHigh + yPixelStart;		this.setVisible(true);               // Make a new applet pWxpH		Coordinate point = getPoint(0);      // Get the first point		int pointNum = 0;    				double xRange = xRange();            // Range of x and y on the graph		double yRange = yRange();		double xStart = xyStart().getX();    // Starting value of x and y in		double yStart = xyStart().getY();    // lower left hand corner				gr.setColor(Color.green);		if((xStart < 0) && (xStart + xRange > 0)){ //Graph the yAxis if possible			gr.drawLine( // y axis					getInt(xPixelStart-(xStart*(pixelsWide/xRange))), 					getInt(yPixelStart - 10),					getInt(xPixelStart-(xStart*(pixelsWide/xRange))), 					getInt(yPixelStart + pixelsHigh));		}		if((yStart < 0) && (yStart + yRange > 0)){ //Graph the xAxis if possible			gr.drawLine(					getInt(xPixelStart),					getInt(yPixelStart + (yStart+yRange)*(pixelsHigh/yRange)), 					getInt(xPixelStart + pixelsWide), 					getInt(yPixelStart + (yStart+yRange)*(pixelsHigh/yRange)));		}		gr.setColor(Color.black);				int lastx = 0; // initialize last coordinates		int lasty = 0;				while(point != null){               // As long as the point exists...			double X = point.getX();        // X = x coord			double Y = point.getY();        // Y = y coord			int xPixel = getInt(            // map x to xPixel					xPixelStart + (X-xStart)*(pixelsWide/xRange));			int yPixel = getInt(            // map y to yPixel					yPixelStart + (yStart+yRange-Y)*(pixelsHigh/yRange));			//gr.drawLine(xPixel,yPixel,xPixel+1,yPixel+1); //Draw the point			if(pointNum != 0){              // if not first point, draw line				gr.drawLine(lastx, lasty, xPixel, yPixel); 			}									pointNum++;   //update pointNum			point = getPoint(pointNum);			lastx = xPixel;			lasty = yPixel;		}	}	private int getInt(double doub){		int count = 0;		while(doub > 1){			count++;			doub--;		}		return count;	}}