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

public class PathCanvas
  extends Canvas
{
  public Vector V;
  private double xc, yc, rad;
  public Vertex vMouse = new Vertex( 2 );

  public PathCanvas()
  {
    V = new Vector();
  }

  public void setLimits( double xc, double yc, double rad )
  {
    this.xc = xc;
    this.yc = yc;
    this.rad = rad;
  }

  public void addPoint( double x[] )
  {
    Vertex v = new Vertex( x );
    V.addElement( v.copy() );
  }

  public void clear()
  {
    V.removeAllElements();
    repaint();
  }

  public void paint( Graphics g )
  {
    if( V == null )
      return;
    int w, h, ir, id;
    int i, j;
    int x0, y0, x1, y1;

    double nxc, nyc, nrad;
    double dx, dy;

    w = size().width;
    h = size().height;
    id = ( ( w < h ) ? w : h );
    ir = id / 2;

    int nVertex = V.size();

    g.setColor( Color.blue );
    for( i = 1; i < nVertex; i++ ) {
      Vertex v0 = (Vertex)(V.elementAt( i - 1 ) );
      Vertex v1 = (Vertex)(V.elementAt( i ) );
      x0 = (int)( w/2 + ( ( v0.coord[0] - xc ) / rad ) * ir );
      y0 = (int)( h/2 - ( ( v0.coord[1] - yc ) / rad ) * ir );
      x1 = (int)( w/2 + ( ( v1.coord[0] - xc ) / rad ) * ir );
      y1 = (int)( h/2 - ( ( v1.coord[1] - yc ) / rad ) * ir );
      g.drawLine( x0, y0, x1, y1 );
    }

    g.setColor( Color.black );
    // g.drawRect( w/2-ir, h/2-ir, id, id );
    String txt = "Center:( " + xc + ", " + yc + " ); Radius:" + rad;
    g.drawString( txt, 2, h-5 );
  }

  public Vertex xyToVertex( int ix, int iy )
  {
    int w, h, ir;
    w = size().width;
    h = size().height;
    ir = ( ( w < h ) ? w : h ) / 2;
    vMouse.coord[0] = xc + ( rad * ( ix - w/2 ) ) / ir;
    vMouse.coord[1] = yc + ( rad * ( h/2 - iy ) ) / ir;
    return vMouse;
  }
  public boolean handleEvent( Event evt )
  {
    if( evt.id == Event.MOUSE_MOVE ) {
      if( evt.target == this ) {
        postEvent( new Event( this, Event.ACTION_EVENT,
                                xyToVertex( evt.x, evt.y ) ) );
      }
    }
    return super.handleEvent( evt );
  }
  public Vertex getMouseVertex()
  {
    return vMouse;
  }
}
