Hello I am trying to complete a problem for my comp sci cour

Hello!

I am trying to complete a problem for my comp. sci course. I need to extend a program of mine to do the following:

\" When the mouse is ?rst pressed, draw a small circle as above. As the mouse is dragged, draw a line connecting the original point to the current position of the mouse. Erase the previous line ?rst so that your canvas doesn’t get covered in black. When the mouse is released, leave the last line on the canvas.\"

I have most of the code, but I cannot figure out how to erase the previous line and remain with a single line on the screen. Can anyone help me figure out how to erase the line?

   ?Here is the code I have so far:

public class CanvasWithMouseMotionListener extends CanvasWithMouseListener implements MouseMotionListener {

   public CanvasWithMouseMotionListener() {
   super();
    addMouseMotionListener(this);
}

     public void mouseMoved(MouseEvent e) {
      System.out.println(\"CanvasWithMouseMotionListener.mouseMoved\");
   }

     public void mouseDragged(MouseEvent e) {
     System.out.println(\"CanvasWithMouseMotionListener.mouseDragged\");
     int x = e.getX();
     int y = e.getY();
     Graphics g = getGraphics();
     g.drawLine(this.x+20, this.y+15, x, y);

}


}

?The problem with the code is that this is the output:

?whereas I need a single line.

?For reference this is the class CanvasWithMouseMotionListener extends:

?  

    public class CanvasWithMouseListener extends Canvas implements MouseListener {

      protected int x;
     protected int y;

      public CanvasWithMouseListener() {
      super();
       addMouseListener(this);
     }

      @Override
      public void mouseClicked(MouseEvent e) {
      System.out.println(\"CanvasWithMouseListener.mouseClicked\");
      System.out.println(\"canvas width=\" + getWidth() + \", height=\" + getHeight());
      int x = e.getX();
      int y = e.getY();
      Graphics g = getGraphics();
      g.fillOval(x, y, 30, 30);
  
    }

     @Override
     public void mousePressed(MouseEvent e) {
      System.out.println(\"CanvasWithMouseListener.mousePressed\");
      System.out.println(\"CanvasWithMouseListener.mouseClicked\");
      System.out.println(\"canvas width=\" + getWidth() + \", height=\" + getHeight());
      this.x = e.getX();
      this.y = e.getY();
      Graphics g = getGraphics();
      g.fillOval(this.x, this.y, 30, 30);
      }

     @Override
     public void mouseReleased(MouseEvent e) {
      System.out.println(\"CanvasWithMouseListener.mouseReleased\");
     }

     @Override
     public void mouseEntered(MouseEvent e) {
      System.out.println(\"CanvasWithMouseListener.mouseEntered\");
     }

      @Override
     public void mouseExited(MouseEvent e) {
      System.out.println(\"CanvasWithMouseListener.mouseExited\");
     }

     }

Canvas lest 8:43 PM Ask me anything ))) E :] 11/4/2016 3

Solution

//Functions for drawing and deleting lines on canvas

private void DrawLine(MotionEvent e)

    {

        _EndPt.Set(e.GetX(), e.GetY());

_Canvas.DrawLine(_StartPt.X, _StartPt.Y, _EndPt.X, _EndPt.Y, _Paint);

_StartPt.Set(_EndPt);

        Invalidate();

    }

private void eraseCanvas(MotionEvent e)

    {

        _EndPt.Set(e.GetX(), e.GetY());

ErasePaint.Color = Color.Transparent;

        ErasePaint.StrokeWidth = 5;

        ErasePaint.SetXfermode (new PorterDuffXfermode (PorterDuff.Mode.Clear));

        _Canvas.DrawLine(_StartPt.X, _StartPt.Y, _EndPt.X, _EndPt.Y, ErasePaint);

_StartPt.Set(_EndPt);

        Invalidate();

    }

}

Hello! I am trying to complete a problem for my comp. sci course. I need to extend a program of mine to do the following: \
Hello! I am trying to complete a problem for my comp. sci course. I need to extend a program of mine to do the following: \
Hello! I am trying to complete a problem for my comp. sci course. I need to extend a program of mine to do the following: \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site