1. Tech

Your suggestion is on its way!

An email with a link to:

http://delphi.about.com/library/bluc/text/uc052102b.htm

was emailed to:

Thanks for sharing About.com with others!

RTL referenceGlossary|Tips/Tricks|FREE App/VCL|Best'O'Net|Books|Link To
 
GDI Graphics In Delphi
Page 2: Drawing: Lines
 More of this Feature
• Page 1: GDI Jargon
• Page 3: Drawing: Shapes
• Page 4: Draw vs. Paint
• Page 5: Handles and Stuff
• Page 6: Pictures: TBitmap
• Page 7: Ways To Kill Flicker
• Page 8: GDI, The Hard Way
• Page 9: API Drawings
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
Discuss!
 Related Resources
• Graphics programming in Delphi
• Screen zooming
• Double buffering
• Win API in Delphi

   Let's Draw Now
As you've been introduced to the basics, we can get on with the business of drawing stuff now. We'll start with simple lines and shapes, and move onto more useful bitmap drawing functions. Eventually, you'll know about off-screen drawing and other neat techniques.

First of all, it should be noted that (0,0) is the screen top-left. This might confuse you if you have a mathematical background, but remember - (0, 0) is the TOP-LEFT and the y axis is positive down the screen. Thus, (0, 50) is 50 pixels down from the top of the screen, not 50 pixels up from the bottom. Remember this, or your first few drawing attempts will confuse the hell out of you ;-).

   Lines and Shapes
Drawing lines and shapes is not difficult, thankfully. The main thing to know about here is the difference between Pens and Brushes. This is simply that Pens are used to draw lines, which might include a line or the outside border of a rectangle, while Brushes fill the interiors.

There are two functions you should know about for line drawing, both belonging to TCanvas:

NAME DESCRIPTION EXAMPLE USE
MoveTo Tells the canvas to draw the next line starting at the given x and y points Canvas.MoveTo(50, 100);
LineTo Draws a line from the current x and y positions (see MoveTo) to the new x and y position. Canvas.LineTo(50, 100);

You can achieve the same effect as MoveTo by setting the Canvas's PenPos... for example, "Canvas.PenPos.x := 20;", "Canvas.PenPos.y := 50", or "Canvas.PenPos := Point(20,50);".

The Canvas uses the current pen position when drawing lines. The pen position defaults to (0,0), which is the screen top-left (remember?). So, for example, after the call "Canvas.LineTo(100,100);" there would be a line from (0,0) to (100, 100). The next line starts from the last co-ordinates, so if the next call was "Canvas.LineTo(200, 100);" another line would be drawn from (100, 100) to (200, 100). This is why the MoveTo method is useful - you usually don't want connected lines.

Lines drawn with LineTo use the current Canvas pen (of type TPen). This is meant to be a metaphor for your common real-world pen, and is used for line drawing and outlining shapes. You can change the Canvas pen to change the line drawn. The most common use is changing the width of the pen - "Canvas.Pen.Width := 4";, which will give different sized lines (in this case, larger ones). Another useful pen property is its Color (yuck, American spelling) - "Canvas.Pen.Color := clLime;".

Let's get busy then:

procedure TForm1.FormCreate(Sender: TObject);
begin
  // initialise the random
  // number generator
  Randomize;
end;

const NUM_LINES = 2000;

procedure TForm1.DrawLines;
var
  i: Integer;
begin
  for i := 0 to NUM_LINES - 1 do
  begin
    Canvas.Pen.Color :=
         RGB(Random(256),
             Random(256),
             Random(256)
             );
    Canvas.LineTo
       (Random(ClientWidth),
        Random(ClientHeight));
  end;
end;

Call the DrawLines procedure in a button's OnClick handler. The code simply draws 2000 lines to random locations, each time using a random colour. The function RGB, by the way, takes values for the red, green and blue components of a color (from 0 to 255) and gives you back a TColor. I'll talk about colors later. Here's the screen:

delphi draw line

   My Lines Are Missing!
Now, after the lines are drawn, move your form off the screen a bit and back on. You'll notice that the sections moved off the screen have had their lines erased! This is not a bug, and can be resolved quite easily. However, not yet ;-). First, I want to show you some more drawing - this time shapes.

   Question, Suggestions...
If you have any questions or comments to this (huge) article, please post them on the Delphi Programming Forum. Discuss!

Next page > Drawing Shapes > Page 1, 2, 3, 4, 5, 6, 7, 8, 9

All graphics (if any) in this feature created by Zarko Gajic.

 More Delphi
· Learn another routine every day - RTL Quick Reference.
· Download free source code applications and components.
· Talk about Delphi Programming, real time.
· Link to the Delphi Programming site from your Web pages.
· Tutorials, articles, tech. tips by date: 2001|2000|1999|1998 or by TOPIC.
· NEXT ARTICLE: Articles.
More Delphi articles
 Stay informed with all new and interesting things about Delphi (for free).
Subscribe to the Newsletter
Name
Email

 Got some code to share? Got a question? Need some help?

©2014 About.com. All rights reserved.