import java.awt.*; class DrawSim extends java.awt.Canvas { //double-buffering public boolean xplot=false; public boolean vplot=true; private Image image2; private Graphics g2; private Dimension mySize; private int XSIZE,YSIZE,XOFFSET,YOFFSET; private int MAXPOINTS=2000; private double xscale,yscale,XMAX,YMAX,YMIN; private int xpoints[],xvpoints[]; private int ypoints[],yvpoints[]; private int lastXPoint,lastVPoint; DrawSim(){ xpoints = new int[MAXPOINTS]; ypoints = new int[MAXPOINTS]; xvpoints = new int[MAXPOINTS]; yvpoints = new int[MAXPOINTS]; setBackground(Color.white); } public void paint(Graphics g){ // setup offscreen graphics object, setup axes and scale parameters // resize background size if necessary if (g2 == null) {determineSize();paintSetup();} else if ((this.size().width!=mySize.width) || (this.size().height!=mySize.height)) paintSetup(); // paint background g2.setColor(this.getBackground()); g2.fillRect(0,0,mySize.width,mySize.height); // graph axes etc myAxes(); // go through all points and paint if(xplot){ g2.setColor(Color.blue); for(int i=0;i=0) && (ix==xpoints[lastXPoint]))) { // wrap arrays around to avoid out-of-bounds problems if (++lastXPoint>=MAXPOINTS) lastXPoint=0; xpoints[lastXPoint]=ix; ypoints[lastXPoint]=iy; } repaint(); } public void addVToScreen(double x, double y){ int ix, iy; ix=(int)(x*xscale)+XOFFSET; iy=(int)((y-YMIN)*yscale)+YOFFSET; // only plot new pixels to screen if ( !( (lastVPoint>=0) && (ix==xvpoints[lastVPoint]))) { // wrap arrays around to avoid out-of-bounds problems if (++lastVPoint>=MAXPOINTS) lastVPoint=0; xvpoints[lastVPoint]=ix; yvpoints[lastVPoint]=iy; } repaint(); } private double myformat(double x, int n){ double d; int i; d=x*n; i=(int)d; d=(double)i/(double)n; return(d); } public void clearScreen(){ lastXPoint=-1; lastVPoint=-1;} }