Solved

# Arrow Tool  for drawing on canvas - more points available

Posted on 2002-03-13
251 Views
I want to be able to create an arrow tool like the one in MS Word drawing toolbar - that is full head arrow (always filled) with either 1 or 2 heads. Possibility of dotted or dashed  shaft is an optional extra.

It also needs to be able to stretch like a line would on the canvas, determined by where the user places the mouse on the canvas.

any solutions greatly appreciated.

More points available for fullness of answer.

Vanessa

0
Question by:Vanessa

LVL 9

Expert Comment

ID: 6863626
listening
0

LVL 33

Expert Comment

ID: 6863633
I can not guess what you want? An Arrow Tool? Here is some code to make an arrow shape on a Canvas.

var
ArPoints: array[0..7] of TPoint;

begin
Canvas.Brush.Color := clRed;
Canvas.Pen.Color := clRed;
{these points draw an Arrow with Polygon( )}
ArPoints[0].x := 2;
ArPoints[0].y := 8;
ArPoints[1].x := 14;
ArPoints[1].y := 2;
ArPoints[2].x := 14;
ArPoints[2].y := 6;
ArPoints[3].x := 20;
ArPoints[3].y := 6;
ArPoints[4].x := 20;
ArPoints[4].y := 10;
ArPoints[5].x := 14;
ArPoints[5].y := 10;
ArPoints[6].x := 14;
ArPoints[6].y := 14;
ArPoints[7].x := 2;
ArPoints[7].y := 8;
Canvas.Polygon(ArPoints);
end;

I do not understand "It also needs to be able to stretch like a line would on the canvas" Do lines streach? What is your goal? what are you trying to let the user do with your program, draw an arrow, make a line between 2 points that the user clicks?
0

LVL 6

Expert Comment

ID: 6863810
http://www.blacky.co.nz/free/arrow.zip
It can easily be modified to do what you want.
If you want me to modify it let me know.

There are other sample programs as well:
http://www.blacky.co.nz/free
Most of these were developed in response to questions asked on EE.

Regards
Paul
0

Author Comment

ID: 6866196
Fuller explanation follows

The user has to click on a button then drag from pointA to pointB and on mouse up an arrow appears.  The head on the arrow is at pointB.  There can be multiple arrows on the canvas at anytime. Ideally they should be able to select whether they want a solid line arrow, a dotted or dashed arrow of varying thicknesses.

I am using a paintbox for the canvas, which then coverts to a bitmap and the picture is saved in the database.

Vanessa
0

LVL 33

Expert Comment

ID: 6873472
Have you tried to combine the code I listed for
Canvas.Polygon(ArPoints);
and the code in zebada's arrow zip? that should be enough to get you started. Do you have some code you tried for this?
0

LVL 33

Expert Comment

ID: 6878782
Had time this weekend, so I did an arrow draw, I did it with a 7 point array for Polygon, but you could use a 4 point array and just draw a wide line for the arrow tail

private
{ Private declarations }
DoArrow, ReArrow, Start1: Boolean;
StartArrow, ALPt: TPoint;
ArrowPoints: Array[0..7] of TPoint;

procedure TForm1.Button_DrawArrowClick(Sender: TObject);
begin
Start1 := True;
end;

procedure TForm1.PaintBox5MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Start1 then
begin
DoArrow := True;
Start1 := False;
PaintBox5.Canvas.Brush.Style := bsClear;
PaintBox5.Canvas.Pen.Color := clBlack;
PaintBox5.Canvas.Pen.Style := psSolid;
PaintBox5.Canvas.Pen.Mode := pmNot;
PaintBox5.Canvas.Pen.Width := 1;

StartArrow.x := X;
StartArrow.y := Y;
ALPt.x := X;
ALPt.y := Y;
end;
end;

procedure TForm1.PaintBox5MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var
begin
if DoArrow then
begin
if (abs(StartArrow.x - X) < 14) and
(abs(StartArrow.y - Y) < 14) and
not ReArrow then Exit;
{since this arrow head is 17 pixels long then don't start drawing
until the cursor moves 14 pixels}
if ReArrow then
begin
{erase the arrow}
PaintBox5.Canvas.Polygon(ArrowPoints);
PaintBox5.Canvas.MoveTo(StartArrow.x,StartArrow.y);
PaintBox5.Canvas.LineTo(ALPt.x, ALPt.y);
end;
Angle := 180 * (1 + ArcTan2(Y-StartArrow.y, X-StartArrow.x) / Pi);
{I use a trig function to get the angle from the rise and run (Y-StartArrow.y, X-StartArrow.x)
ArcTan2( )  returns a Radian value from the rise (Cos-Y) and run (Sin-X) values
I convert it to Angle mesurement, because it's easier than radians for me to use}
PaintBox5.Canvas.MoveTo(StartArrow.x,StartArrow.y);
{reverse the angle and move 17 pixels back toward StartPoint for the length of the arrow head}
PaintBox5.Canvas.LineTo(ALPt.x, ALPt.y);
ArrowPoints[0].x := X;
ArrowPoints[0].y := Y;
{now rotate your angle 90 degrees}
HalfHWidth := 7;
HalfTWidth := 2;
{the Cos( ) gives you the X factor to mutiply your X mesurements
and Sin( ) gives you the Y}
{you have rotated the angle 90 so move the point out 7
pixels from ALPt point for arrow head width}
{move the point out 2 pixels for the arrow tail width}
{now do the start point}
{rotate angle 90 other way}

{because all the Radian values are Rounded, small HalfTWidth like
2 pixels may not be consistant}

ArrowPoints[7] := ArrowPoints[0];
{close the polygon}

PaintBox5.Canvas.Polygon(ArrowPoints);
ReArrow := True;
end;
end;

procedure TForm1.PaintBox5MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if DoArrow and ReArrow then
begin
PaintBox5.Canvas.Brush.Style := bsSolid;
PaintBox5.Canvas.Brush.Color := clAqua;
PaintBox5.Canvas.Pen.Color := clBlue;
PaintBox5.Canvas.Pen.Style := psSolid;
PaintBox5.Canvas.Pen.Mode := pmCopy;
PaintBox5.Canvas.Polygon(ArrowPoints);
{PB5Bmp.Canvas.Polygon(ArrowPoints);}

{below are some alternate ways to draw the arrow tail}
{PaintBox5.Canvas.Pen.Width := 3;
PaintBox5.Canvas.MoveTo(StartArrow.x,StartArrow.y);
PaintBox5.Canvas.LineTo(ALPt.x,ALPt.y);
PaintBox5.Canvas.Pen.Width := 1;
PaintBox5.Canvas.Pen.Color := clWhite;
PaintBox5.Canvas.MoveTo(StartArrow.x,StartArrow.y);
PaintBox5.Canvas.LineTo(ALPt.x,ALPt.y);}
end;
ReArrow := False;
DoArrow := False;
end;

- - - - - - - - - - - - - - - -
if you're going to do a graphics program, you might practice using the drawng methods available for TCanvas
0

LVL 33

Accepted Solution

Slick812 earned 100 total points
ID: 6879070
you might look at this About web site for delphi graphics

0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question