FMXTCanvasFillFunctions (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example shows how to use the TCanvas fill functions and their results.

The TCanvas fill functions are: Fill, FillArc, FillEllipse, FillPath, FillPolygon, FillRect, FillText.

To build and test this example, create a Multi-Device Application - Delphi and add the following controls on the form:

The example draws on the canvas of the bitmap. The bitmap is displayed on the TImage.

Code

Add the following code to the OnCreate event handler of the form.

procedure TForm1.FormCreate(Sender: TObject);
begin
 Image1.Bitmap.Clear($FFFFFF);
  // initializes the image bitmap canvas
  Image1.Bitmap := TBitmap.Create(400, 400);
end;

Add the following code, which makes the drawing area white, to the OnClick event handler of the button.

procedure TForm1.Button1Click(Sender: TObject);
begin
  // clears the canvas
  Image1.Bitmap.Canvas.Clear($FFFFFF);
end;

Add the following code, which changes the fill color of the canvas, to the OnExit event handler of the ColorComboBox.

procedure TForm1.ColorComboBox1Exit(Sender: TObject);
begin
  // sets the fill color of the canvas to the value from the ColorComboBox
  Image1.Bitmap.Canvas.Fill.Color := ColorComboBox1.Color;
end;

Add the following codes to the OnClick event handlers of each radio button.

Code

procedure TForm3.RadioButton11Click(Sender: TObject);
var
  p1, p2: TPointF;
begin
 Image1.Bitmap.Clear($FFFFFF);
  // sets the center of the arc
  p1 := TPointF.Create(200, 200);
  // sets the radius of the arc
  p2 := TPointF.Create(150, 150);
  // fills and draws the arc on the canvas
  Image1.Bitmap.Canvas.BeginScene;
  Image1.Bitmap.Canvas.FillArc(p1, p2, 90, 230, 20);
  Image1.Bitmap.Canvas.EndScene;
 end;

procedure TForm3.RadioButton12Click(Sender: TObject);
var
  MyRect: TRectF;
begin
Image1.Bitmap.Clear($FFFFFF);
  // sets the circumscribed rectangle of the ellipse
  MyRect := TRectF.Create(50, 40, 200, 270);
  // fills and draws the ellipse on the canvas
  Image1.Bitmap.Canvas.BeginScene;
  Image1.Bitmap.Canvas.FillEllipse(MyRect, 40);
  Image1.Bitmap.Canvas.EndScene;
end;

procedure TForm3.RadioButton13Click(Sender: TObject);
var
  path: TPathData;
  MyRect1, MyRect2: TRectF;
begin
 Image1.Bitmap.Clear($FFFFFF);
  // sets the circumscribed rectangle of the ellipse to be added to the path
  MyRect1 := TRectF.Create(90, 100, 230, 300);
  /// sets the rectangle to be added to the path
  MyRect2 := TRectF.Create(70, 90, 220, 290);
  // initializes and creates the path to be drawn
  path := TPathData.Create;
  path.AddEllipse(MyRect1);
  path.AddRectangle(MyRect2, 0, 0, AllCorners);
  // fills and draws the path on the canvas
  Image1.Bitmap.Canvas.BeginScene;
  Image1.Bitmap.Canvas.FillPath(path, 200);
  Image1.Bitmap.Canvas.EndScene;
end;

procedure TForm3.RadioButton14Click(Sender: TObject);
var
  MyRect: TRectF;
begin
 Image1.Bitmap.Clear($FFFFFF);
  // sets the rectangle to be drawn
  MyRect := TRectF.Create(50, 40, 200, 270);
  // fills and draws the rectangle on the canvas
  Image1.Bitmap.Canvas.BeginScene;
  Image1.Bitmap.Canvas.FillRect(MyRect, 30, 60, AllCorners, 100);
  Image1.Bitmap.Canvas.EndScene;
end;

procedure TForm3.RadioButton15Click(Sender: TObject);
var
  p1, p2, p3, p4, p5: TPointF;
  MyPolygon: TPolygon;
begin
 Image1.Bitmap.Clear($FFFFFF);
  // sets the points that define the polygon
  p1 := TPointF.Create(210, 220);
  p2 := TPointF.Create(330, 360);
  p3 := TPointF.Create(380, 260);
  p4 := TPointF.Create(200, 180);
  p5 := TPointF.Create(140, 160);
  // creates the polygon
  SetLength(MyPolygon, 5);
  MyPolygon[0] := p1;
  MyPolygon[1] := p2;
  MyPolygon[2] := p3;
  MyPolygon[3] := p4;
  MyPolygon[4] := p5;
  // fills and draws the polygon on the canvas
  Image1.Bitmap.Canvas.BeginScene;
  Image1.Bitmap.Canvas.FillPolygon(MyPolygon, 50);
  Image1.Bitmap.Canvas.EndScene;
end;

procedure TForm3.RadioButton16Click(Sender: TObject);
var
  MyRect: TRectF;
begin
 Image1.Bitmap.Clear($FFFFFF);
  // sets the rectangle where the text will be displayed
  MyRect := TRectF.Create(50, 40, 200, 270);
  // fills and draws the text in the specified rectangle area of the canvas
  Image1.Bitmap.Canvas.BeginScene;
  Image1.Bitmap.Canvas.FillText(MyRect, 'FillText', false, 100,
    [TFillTextFlag.RightToLeft], TTextAlign.Center, TTextAlign.Center);
  Image1.Bitmap.Canvas.EndScene;
end;

Uses

See Also