Chapter 30 Key Takeaways

  1. TCanvas is the drawing surface. Coordinates start at (0,0) in the top-left, with X increasing rightward and Y increasing downward. All persistent drawing must occur inside OnPaint.

  2. Drawing primitives: MoveTo/LineTo for lines, Rectangle/RoundRect for rectangles, Ellipse for ellipses and circles, Polygon for arbitrary shapes, Arc/Pie for arcs and wedges.

  3. TPen controls outlines (Color, Width, Style). TBrush controls fills (Color, Style). Set Pen.Style := psClear for no outline or Brush.Style := bsClear for no fill.

  4. TColor uses BGR byte ordering internally. Use RGBToColor(R, G, B) for custom colors, or predefined constants like clRed, clNavy, clBlack.

  5. Text rendering uses TextOut(X, Y, Text). Measure text dimensions with TextWidth and TextHeight. Control appearance through Canvas.Font properties.

  6. Images are loaded with TImage.Picture.LoadFromFile or created with TBitmap. Draw bitmaps on canvas with Canvas.Draw or Canvas.StretchDraw.

  7. Charts from scratch: Pie charts use the Pie method with angle calculations. Bar charts use scaled rectangles. Line charts use MoveTo/LineTo with data-point markers. All require calculating scales from data to pixel coordinates.

  8. Custom controls: Use TPaintBox for one-off drawing. Create reusable components by descending from TGraphicControl or TCustomControl and overriding the Paint method.

  9. Animation uses TTimer for periodic updates and Invalidate to request repaints. Double buffering (drawing to an off-screen bitmap first) prevents flicker.

  10. Call Invalidate, not draw methods, when data changes. Invalidate requests a repaint; OnPaint does the actual drawing. This separation keeps the drawing consistent and avoids painting outside the paint cycle.