public boolean handleEvent(Event e) {
	switch (e.id) {
	  case Event.MOUSE_DOWN:
	    // How we handle a MOUSE_DOWN depends on the action mode
	    switch (action) {
	      case ADD:
		// Add a new control point at the specified location
                int np=points.size();
		ControlPoint pnt;
                // keep points sorted by x: insert in correct place
                int i;
                for (i=0;
                    i<=np-1 && ((ControlPoint) points.elementAt(i)).x<e.x;i++) 
                { }
                pnt = new ControlPoint(e.x,e.y);
                if (i<=np-1) {
		  points.insertElementAt(pnt,i);
                } else {
                  points.addElement(pnt);
                } 
		repaint();
		break;
	      case MOVE:
		// Attempt to select the point at the location specified.
		// If there is no point at the location, findPoint returns
		// -1 (i.e. there is no point to be moved)
		moving_point = findPoint(e.x, e.y);
		break;
	      case DELETE:
		// Delete a point if one has been clicked
		int delete_pt = findPoint(e.x, e.y);
		if(delete_pt >= 0) {
		   points.removeElementAt(delete_pt);
		   repaint();
		}
		break;
	      default:
	        throw new IllegalArgumentException();
	    }
	    return true;
	  case Event.MOUSE_UP:
	    // We only care about MOUSE_UP's if we've been moving a control
	    // point.  If so, drop the control point.
	    if (moving_point >=0) {
		moving_point = -1;
	        repaint();
	    }
	    return true;
	  case Event.MOUSE_DRAG:
	    // We only care about MOUSE_DRAG's while we are moving a control
	    // point.  Otherwise, do nothing.
	    if (moving_point >=0) {
               int np=points.size();
            // test if e.x is between x of points.elementAt(moving_point+1)
           //                         and  points.elementAt(moving_point-1)
               if ( (moving_point==np-1 || 
                     e.x<=((ControlPoint)points.elementAt(moving_point+1)).x) &&
                    (moving_point==0 ||
                     e.x>=((ControlPoint)points.elementAt(moving_point-1)).x) )
               {    
                 ControlPoint pnt = (ControlPoint) points.elementAt(moving_point);
	         pnt.x = e.x;
                 pnt.y = e.y;
               } else {
                 // otherwise find correct slot (or just always do this?)
                 points.removeElementAt(moving_point);
                 int i;
                 for (i=0;
                    i<=np-2 && ((ControlPoint) points.elementAt(i)).x<e.x;i++) 
                 { }
                 ControlPoint pnt2 = new ControlPoint(e.x,e.y);
                 if (i<=np-1) {
		   points.insertElementAt(pnt2,i);
                 } else {
                   points.addElement(pnt2);
                 }
                 moving_point=i;
               }
	       repaint();
	    }
	    return true;
	  case Event.WINDOW_DESTROY:
	    System.exit(0);
	    return true;
	  default:
	    return false;
	}
    }