1. Home
  2. Computing & Technology
  3. Delphi Programming
RTL referenceGlossary|Tips/Tricks|FREE App/VCL|Best'O'Net|Books|Link To
 
GDI Graphics In Delphi
Page 3: Drawing: Shapes
 More of this Feature
• Page 1: GDI Jargon
• Page 2: Drawing: Lines
• 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

There are many shape drawing functions in TCanvas, and all are easy to use. Here are some useful ones:

NAME DESCRIPTION EXAMPLE USE
Ellipse Draws an ellipse from the top-left x and y given to the bottom-right x and y... this could be a circle if you give the right co-ordinates Canvas.Ellipse(0,0,50,50);
FillRect Fills a rectangle with the current brush color, but doesn't outline it Canvas.FillRect( Bounds(0,0,100,100));
FloodFill Fills a given area with the brush color until an edge is reached... like the paint bucket in most paint packages Canvas.FloodFill(10, 10, clBlack, fsBorder);
Rectangle Draws a rectangle (or square), filled using the brush color and outlined with the pen color Canvas.Rectangle( Bounds(20, 20, 50, 50));
RoundRect Same as Rectangle, but with curved corners Canvas.RoundRect( 20, 20, 50, 50, 3, 3);

The above list should help you create some reasonable drawing. There is one more useful function: TextOut. This allows you to draw text to the screen using the current Canvas font.

NAME DESCRIPTION EXAMPLE USE
TextOut Draws the given string to the canvas at (x,y) - background filled with the current brush color Canvas.TextOut(10, 10, 'Some text');

This is a very useful function. You can draw transparent text too, without the background filling. Nip along to the tips page (drawing transparent text). If you want to change the font used in TextOut, set details for the Canvas's Font property (of type TFont) - for example "Canvas.Font.Name := 'Verdana';", "Canvas.Font.Size := 24;" or "Canvas.Font.Color := clRed;".

I'd better quickly point out the Bounds function. This gives a TRect record, which just stores left, right, top and bottom integers. TRects are very useful (in the Windows API they are called RECTs, by the way). Bounds lets you specify a left and top position, and a width and height, and you will get a TRect with (left, top, left + width, top + height) in return. There is another function, Rect(), which does much the same thing but accepts the left, right, top and bottom coordinates directly. Finally, you could use SetRect from the Windows API if you wanted.

Here's some example code that randomly draws shapes:

const NUM_SHAPES = 200;

procedure TForm1.DrawShapes;
var
  i,
  ShapeLeft,
  ShapeTop: Integer;
begin
  for i := 0 to NUM_SHAPES - 1 do
  begin
    Canvas.Brush.Color :=
       RGB(Random(256),
           Random(256),
           Random(256));
    ShapeLeft := Random(ClientWidth);
    ShapeTop := Random(ClientHeight);
    // now randomly decide what to draw
    case Random(3) of
      0: Canvas.Rectangle(ShapeLeft,
                          ShapeTop,
                          ShapeLeft + Random(50),
                          ShapeTop + Random(50));
      1: Canvas.Ellipse(ShapeLeft,
                        ShapeTop,
                        ShapeLeft + Random(50),
                        ShapeTop + Random(50));
      2: begin
           Canvas.Font.Size := 10 + Random(7); // from 10 to 16
           Canvas.TextOut ( ShapeLeft, ShapeTop, 'Some text');
         end;
    end;
  end;
end;

Delphi Shape draw

You might have noticed some shapes have a different outline color from their insides? This is what I mentioned above. The brush is used to fill the shape and the pen is used to outline it. Since the brush color is set randomly but the pen color is not, the difference between the two should be obvious.

That's enough about this - you should be able to draw some simple shapes and text now. However, the issue of the disappearing graphics needs to be resolved.

   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 vs. Painting > 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?
Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.