3

this is my first question in stackoverflow , i've searched everywhere a cross the web before posting here,so you guys are my last chance

i'm making a little program in Delphi xe5 which consist in drawing lines on a football pitch(TImage) using Canvas and MoveTo(X,Y) method, everything works great

program picture

but my problem is that i want to erase the eralier line just before drawing the next one so i cannnot have two lines at same time, how can i do that ?

this is a snippet of code i'm using for drawing the lines :

 procedure TForm2.Image1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);

 const
Line: Integer = 0;
 begin

if Line = 0 then begin
   Canvas.MoveTo(X,Y);
   Line := 1;
   Label1.Caption := IntToStr(x) ;
   label2.Caption :=  IntToStr(y);
End
else if Line = 1 then begin
   Canvas.LineTo(X,Y);
   Line := 0;
   Label3.Caption := IntToStr(x) ;
   label4.Caption :=  IntToStr(y);

end;
end;
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
MKaiser
  • 45
  • 1
  • 6

2 Answers2

5

If you do not have areas filled with color, you can do all drawing with Pen.Mode set to pmXOR. It will give some odd points where lines cross (for example where the red line crosses the blue circle), but when you re-draw the red line - it will disappear.

Just add:

Canvas.Pen.Mode := pmXOR;

If necessary - remember the existing Pen.Mode and restore it when finish drawing.

TLama
  • 75,147
  • 17
  • 214
  • 392
roumen
  • 563
  • 6
  • 15
3

You cannot easily erase a line on a canvas. How can you know what was there before? When you write on a canvas, whatever was there before is overwritten and lost. Unless you remember what was there before.

So, there's one option, remember what was there before, and restore to that state. Typically you might remember a static background. In this case you'd have a static image with the football pitch pictorial. Whenever you need to draw it, draw the static image, and then the dynamic lines on top.

The other approach would be to draw the entire image, from scratch, whenever you need to.

Whatever your solution, I suggest that a TImage is the wrong control. That's great for static images, but as soon as you need something dynamic, it doesn't really fit. Instead use a TPaintBox. In the OnPaint event handler for the control, paint the entire image. Either by blitting the static pitch pictorial from a pre-prepared bitmap, and then the dynamic lines. Or just by drawing the entire image from scratch.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490