FMXTCanvasDrawArc (Delphi)

From RAD Studio XE2 Code Examples
Jump to: navigation, search

Language:

Versions:

Contents

Description

This example shows how to use the DrawArc and FillArc functions and their results.

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

  • A TImage
  • Two TEdit objects to set the coordinates for the center of the arc. Name them CenterXEdit and CenterYEdit.
  • Two TEdit objects to set the rays of the arc. Name them RadiusXEdit and RadiusYEdit.
  • Two TEdit objects to set the start angle and sweep angle. Name them StartAngleEdit and SweepAngleEdit.
  • A TEdit object to set the opacity. Name it OpacityEdit.
  • Two TTColorComboBoxes to set the colors for drawing the stroke and filling. Name them StrokeColorComboBox and FillColorComboBox.
  • Two TButtons for drawing and filling the arc. Name them DrawButton and FillButton.

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
  //initializes the bitmap
  Image1.Bitmap.Create(Round(Image1.Width), Round(Image1.Height));
end;

Add the following code to the OnClick event handler of the DrawButton and FillButton:

procedure TForm1.DrawButtonClick(Sender: TObject);
var
  Center, Radius: TPointF;
  Opacity, StartAngle, SweepAngle: Single;
begin
  // takes the information from the edits
  // checks whether all the values are valid
  if (TryStrToFloat(CenterXEdit.Text, Center.X) and
    TryStrToFloat(CenterYEdit.Text, Center.Y) and
    TryStrToFloat(RadiusXEdit.Text, Radius.X) and
    TryStrToFloat(RadiusYEdit.Text, Radius.Y) and
    TryStrToFloat(StartAngleEdit.Text, StartAngle) and
    TryStrToFloat(SweepAngleEdit.Text, SweepAngle) and
    TryStrToFloat(OpacityEdit.Text, Opacity)) then
  begin
    Image1.Bitmap.Canvas.BeginScene;
    Image1.Bitmap.Canvas.Stroke.Color := StrokeColorComboBox.Color;
    // draws the arc
    Image1.Bitmap.Canvas.DrawArc(Center, Radius, StartAngle,
      SweepAngle, Opacity);
    Image1.Bitmap.BitmapChanged;
    Image1.Bitmap.Canvas.EndScene;
  end
  else
    // displays a message if not all edits have numerical values
    ShowMessage('All Edits text should be numbers')
end;
 
procedure TForm1.FillButtonClick(Sender: TObject);
var
  Center, Radius: TPointF;
  Opacity, StartAngle, SweepAngle: Single;
begin
  // takes the information from the edits
  // checks whether all the values are valid
  if (TryStrToFloat(CenterXEdit.Text, Center.X) and
    TryStrToFloat(CenterYEdit.Text, Center.Y) and
    TryStrToFloat(RadiusXEdit.Text, Radius.X) and
    TryStrToFloat(RadiusYEdit.Text, Radius.Y) and
    TryStrToFloat(StartAngleEdit.Text, StartAngle) and
    TryStrToFloat(SweepAngleEdit.Text, SweepAngle) and
    TryStrToFloat(OpacityEdit.Text, Opacity)) then
  begin
    Image1.Bitmap.Canvas.BeginScene;
    Image1.Bitmap.Canvas.Fill.Color := FillColorComboBox.Color;
    // fills the arc
    Image1.Bitmap.Canvas.FillArc(Center, Radius, StartAngle,
      SweepAngle, Opacity);
    Image1.Bitmap.BitmapChanged;
    Image1.Bitmap.Canvas.EndScene;
  end
  else
    // displays a message if not all edits have numerical values
    ShowMessage('All Edits text should be numbers')
end;

This is an example of how the example form should look:

FMXTCanvasDrawArc.PNG

Uses

See Also

Personal tools
Previous Versions