import java.awt.*;

class DrawSim extends java.awt.Canvas {

//double-buffering

public boolean xplot=true;
public boolean vplot=true;

private Image image2;
private Graphics g2;

private Dimension mySize;

private int XSIZE,YSIZE,XOFFSET,YOFFSET;
private int MAXPOINTS=10000;
private double xscale,yscale,XMAX,YMAX,YMIN,XMIN;
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);

g2.setColor(Color.yellow);
g2.fillOval((int)(xscale*(-XMIN)+XOFFSET)-10,(int)(yscale*(-YMIN)+YOFFSET)-10,20,20);

// go through all points and paint
  if(lastXPoint>=0){
  g2.setColor(Color.red);
  g2.fillOval(xpoints[lastXPoint]-6,ypoints[lastXPoint]-6,12,12);}

  g2.setColor(Color.black);

  for(int i=0;i<lastVPoint;i++)
  g2.drawLine(xvpoints[i],yvpoints[i],xvpoints[i+1],yvpoints[i+1]);
  if(lastVPoint>=0){
  g2.setColor(Color.blue);
  g2.fillOval(xvpoints[lastVPoint]-4,yvpoints[lastVPoint]-4,8,8);}
 
// copy image to live version

  g.drawImage(image2,0,0,this); 
}
 

private void paintSetup() {

  mySize=this.size();

// Create second image space 

  image2 = createImage(mySize.width,mySize.height);
  g2=image2.getGraphics();
}

private void determineSize(){

  XOFFSET=20;
  YOFFSET=15;
  YSIZE=this.size().height-2*YOFFSET;
  YOFFSET=YSIZE+YOFFSET;
  XSIZE=this.size().width-2*XOFFSET;

  XMAX=1.5;
  XMIN=-1.5;
  YMIN=-1.5;
  YMAX=1.5;
  
  xscale=(double)XSIZE/(XMAX-XMIN);
  yscale=-(double)YSIZE/(YMAX-YMIN);

}


public void update(Graphics g){
  paint(g);
}

public void addXToScreen_J(double x, double y){
  int ix, iy;
  ix=(int)((x-XMIN)*xscale)+XOFFSET;
  iy=(int)((y-YMIN)*yscale)+YOFFSET;


// only plot new pixels to screen

  if ( !( (lastXPoint>=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 addXToScreen_E(double x, double y){
  int ix, iy;
  ix=(int)((x-XMIN)*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();
}


public void clearScreen(){
lastXPoint=-1;
lastVPoint=-1;
determineSize();}

} 

