| RTL referenceGlossary|Tips/Tricks|FREE App/VCL|Best'O'Net|Books|Link To |
| GDI Graphics In Delphi | |||||||||||||||||||||||||
| Page 2: Drawing: Lines | |||||||||||||||||||||||||
Let's Draw Now 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 There are two functions you should know about for line drawing, both belonging to TCanvas:
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:
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:
![]() My Lines Are Missing! Question, Suggestions... |
|||||||||||||||||||||||||
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). |
|
|
| Got some code to share? Got a question? Need some help? |

