TGraphicObject = class
public
X1: integer;
Y1: integer;
X2: integer;
Y2: integer;
procedure Draw(ACanvas: TCanvas); virtual; abstract;
constructor Create(AX1, AY1, AX2, AY2: integer);
end;
TLine = class(TGraphicObject)
public
procedure Draw(ACanvas: TCanvas); override;
end;
TEllipse = class(TGraphicObject)
public
procedure Draw(ACanvas: TCanvas); override;
end;
constructor TGraphicObject.Create(AX1, AY1, AX2, AY2: integer);
begin
X1 := AX1;
Y1 := AY1;
X2 := AX2;
Y2 := AY2;
end;
procedure TLine.Draw(ACanvas: TCanvas);
begin
ACanvas.MoveTo(X1, Y1);
ACanvas.LineTo(X2, Y2);
end;
procedure TEllipse.Draw(ACanvas: TCanvas);
begin
ACanvas.Ellipse(X1, Y1, X2, Y2);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FGraphObjs := TList.Create;
FGraphObjs.Add(TLine.Create(10, 10, 50, 50));
FGraphObjs.Add(TEllipse.Create(10, 60, 100, 120));
end;
procedure TForm1.MagicButtonClick(Sender: TObject);
var i: integer;
begin
for i := 0 to FGraphObjs.Count-1 do begin
TGraphicObject(FGraphObjs[i]).Draw(Image2.Canvas);
end;
end;