0

I am trying to draw/display a geometric shape on a Delphi form given a list lines and arcs at a specific X and Y (Cartesian).

Example:

-Line X0Y0 to X10Y0
-Line X10Y0 to X10Y10
-Line X10Y10 to X0Y10
-Line X0Y10 to X0Y0
-Arc/Circle at X5Y5 diameter of 1

Would draw a 10x10 square with a 1 unit diameter hole in the center. How can I draw this on a form?

I am trying to use this article has a referece, but is there are better ways to do this? http://docwiki.embarcadero.com/CodeExamples/XE4/en/FMXTCanvasDrawFunctions_(Delphi)

Ken White
  • 123,280
  • 14
  • 225
  • 444
ikathegreat
  • 2,311
  • 9
  • 49
  • 80
  • I think it is a VCL form in Delphi 2006. I haven't tried anything because I have no idea what to try. I've never done anything like this. I don't know what component to add to my form to allow me to start something. – ikathegreat May 22 '13 at 19:45
  • 1
    You want to draw lines and arcs. You've found example code of *built-in* functions that draw lines and arcs. Where is the problem? – Rob Kennedy May 22 '13 at 20:18
  • @ikathegreat, you should do `TCanvas.MoveTo` and `LineTo` for all of your line segments. – OnTheFly May 22 '13 at 20:19

1 Answers1

2

In a new VCL Form application (File->New->VCL Form Application), drop a TButton in the middle of the form, double-click it to create a TForm1.Button1Click event handler, and use this code:

procedure TForm1.Button1Click(Sender: TObject);
var
  OldBrushColor, OldPenColor: TColor;
begin
  // I've enlarged the size of the rectangle (box)
  // to 20 x 20 for illustration purposes.
  OldBrushColor := Self.Canvas.Brush.Color;
  Self.Canvas.Brush.Color := clBlack;
  Self.Canvas.Rectangle(10, 10, 30, 30);
  Self.Canvas.Brush.Color := OldBrushColor;
  Self.Canvas.Ellipse(11, 11, 29, 29);

  // Alternative using MoveTo/LineTo and
  // changing pen color
  OldPenColor := Self.Canvas.Pen.Color;
  Self.Canvas.Pen.Color := clRed;
  Self.Canvas.MoveTo(30, 10);
  Self.Canvas.LineTo(50, 10);
  Self.Canvas.MoveTo(50, 10);
  Self.Canvas.LineTo(50, 30);
  Self.Canvas.MoveTo(50, 30);
  Self.Canvas.LineTo(30, 30);
  Self.Canvas.MoveTo(30, 30);
  Self.Canvas.LineTo(30, 10);
  Self.Canvas.Ellipse(31, 11, 49, 29);

  Self.Canvas.Pen.Color := OldPenColor;

end;

Sample of the above:

Screen capture image

You can find other TCanvas drawing methods (such as Arc, Chord, and the combination of MoveTo and LineTo in the documentation. (The link is for XE4's docs, but the Delphi 2006 documentation should have the info as well.)

Ken White
  • 123,280
  • 14
  • 225
  • 444