Refining line drawing

With fields in place to track various points, you can refine an application's line drawing.

Tracking the origin point

When drawing lines, track the point where the line starts with the Origin field. Origin must be set to the point where the mouse-down event occurs, so the mouse-up event handler can use Origin to place the beginning of the line, as in this code:

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

begin

Origin := Point(X, Y);{ record where the line starts } end;

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

begin

Canvas.MoveTo(Origin.X, Origin.Y);{ move pen to starting point } Canvas.LineTo(X, Y); Drawing := False; end;

Those changes get the application to draw the final line again, but they do not draw any intermediate actions—the application does not yet support "rubber banding."

Tracking movement

The problem with this example as the OnMouseMove event handler is currently written is that it draws the line to the current mouse position from the last mouse position, not from the original position. You can correct this by moving the drawing position to the origin point, then drawing to the current point:

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

begin if Drawing then begin

Canvas.MoveTo(Origin.X, Origin.Y);{ move pen to starting point } Canvas.LineTo(X, Y); end; end;

The above tracks the current mouse position, but the intermediate lines do not go away, so you can hardly see the final line. The example needs to erase each line before drawing the next one, by keeping track of where the previous one was. The MovePt field allows you to do this.

MovePt must be set to the endpoint of each intermediate line, so you can use MovePt and Origin to erase that line the next time a line is drawn:

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

begin

Drawing := True; Canvas.MoveTo(X, Y); Origin := Point(X, Y);

MovePt := Point(X, Y);{ keep track of where this move was } end;

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

begin if Drawing then begin

Canvas.Pen.Mode := pmNotXor;{ use XOR mode to draw/erase } Canvas.MoveTo(Origin.X, Origin.Y);{ move pen back to origin } Canvas.LineTo(MovePt.X, MovePt.Y);{ erase the old line } Canvas.MoveTo(Origin.X, Origin.Y);{ start at origin again } Canvas.LineTo(X, Y);{ draw the new line } end;

MovePt := Point(X, Y);{ record point for next move } Canvas.Pen.Mode := pmCopy; end;

Now you get a "rubber band" effect when you draw the line. By changing the pen's mode to pmNotXor, you have it combine your line with the background pixels. When you go to erase the line, you're actually setting the pixels back to the way they were. By changing the pen mode back to pmCopy (its default value) after drawing the lines, you ensure that the pen is ready to do its final drawing when you release the mouse button.

Working with multimedia

Was this article helpful?

0 0

Post a comment