unit StammbaumFrm;
interface
uses
Winapi.Windows,
Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs, System.Generics.Collections;
type
TPerson =
class
public
Name:
string;
X, Y: Integer;
constructor Create(
const AName:
string);
procedure SetKoordinaten(AX, AY: Integer);
end;
TFamilie =
class
public
Vater: TPerson;
Mutter: TPerson;
Kinder: TList<TPerson>;
constructor Create(AVater: TPerson; AMutter: TPerson);
end;
TGraphBuilder =
class
public
Familie: TFamilie;
Picture: TCanvas;
constructor Create(AFamilie: TFamilie);
procedure Draw;
end;
const
Abstand: Integer = 50;
Breite: Integer = 100;
Hoehe: Integer = 40;
type
TForm2 =
class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure FormResize(Sender: TObject);
private
Familie: TFamilie;
graph: TGraphBuilder;
FBitmap: TBitmap;
public
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
// TPerson
constructor TPerson.Create(
const AName:
string);
begin
inherited Create;
name := AName;
end;
procedure TPerson.SetKoordinaten(AX, AY: Integer);
begin
X := AX;
Y := AY;
end;
// TFamilie
constructor TFamilie.Create(AVater: TPerson; AMutter: TPerson);
begin
inherited Create;
Vater := AVater;
Mutter := AMutter;
Kinder := TList<TPerson>.Create;
end;
// TGraphBuilder
constructor TGraphBuilder.Create(AFamilie: TFamilie);
begin
inherited Create;
Familie := AFamilie;
end;
procedure TGraphBuilder.Draw;
var
i: Integer;
TextSize: TSize;
Vater, Mutter, Kind: TPerson;
X, Y, W, H: Integer;
PersonName:
string;
begin
Vater := Familie.Vater;
Mutter := Familie.Mutter;
Familie.Vater.SetKoordinaten(0, 0);
X := Vater.X + Breite + Abstand;
Familie.Mutter.SetKoordinaten(X, 0);
// Picture.Font.Name := 'Tahoma';
// Picture.Font.Size := 12;
// Picture.Font.Color := clBlack;
// Vater/Mutter zeichnen
PersonName := Vater.
Name;
TextSize := Picture.TextExtent(PersonName);
X := Vater.X;
Y := Vater.Y;
W := X + Breite;
H := Y + Hoehe;
Picture.Rectangle(X, Y, W + TextSize.cx
div 2, H);
X := X + 5;
Y := Y + TextSize.cy
div 2;
Picture.Textout(X, Y, PersonName);
PersonName := Mutter.
Name;
TextSize := Picture.TextExtent(PersonName);
X := Mutter.X + TextSize.cx
div 2;
Y := Mutter.Y;
W := X + Breite;
H := Y + Hoehe;
Picture.Rectangle(X, Y, W + TextSize.cx
div 2, H);
X := X + 5;
Y := Y + TextSize.cy
div 2;
Picture.Textout(X, Y, PersonName);
// Kinder
for i := 0
to Familie.Kinder.Count - 1
do
begin
Kind := Familie.Kinder[i];
PersonName := Kind.
Name;
TextSize := Picture.TextExtent(PersonName);
if i = 0
then
begin
X := i * Breite;
Y := Vater.Y + Hoehe + Abstand;
Kind.SetKoordinaten(X, Y);
end
else
begin
X := i * (Breite + (2 * Abstand));
Y := Vater.Y + Hoehe + Abstand;
Kind.SetKoordinaten(X, Y);
end;
Y := Kind.Y;
W := X + TextSize.cx + 10;
H := Y + Hoehe;
Picture.Rectangle(X, Y, W, H);
X := X + 5;
Y := Y + TextSize.cy
div 2;
Picture.Textout(X, Y, PersonName);
end;
end;
procedure TForm2.FormCreate(Sender: TObject);
var
Vater, Mutter, proband, kind2, kind3, kind4: TPerson;
begin
// Eltern
Vater := TPerson.Create('
Vater Mustermann ');
Mutter := TPerson.Create('
Mutter Musterfrau ');
// Proband
proband := TPerson.Create('
Max Mustermann ');
// Geschwister
kind2 := TPerson.Create('
Heidi Mustermann ');
kind3 := TPerson.Create('
Friedrich Mustermann ');
kind4 := TPerson.Create('
Jonas Mustermann ');
// Familie
Familie := TFamilie.Create(Vater, Mutter);
Familie.Kinder.Add(proband);
Familie.Kinder.Add(kind2);
Familie.Kinder.Add(kind3);
Familie.Kinder.Add(kind4);
FBitmap := TBitmap.Create;
// Familie zeichnen
graph := TGraphBuilder.Create(Familie);
end;
procedure TForm2.FormPaint(Sender: TObject);
begin
graph.Picture := FBitmap.Canvas;
// Canvas1;
graph.Draw;
Self.Canvas.Draw(0, 0, FBitmap);
end;
procedure TForm2.FormResize(Sender: TObject);
begin
FBitmap.SetSize(Self.ClientWidth, Self.ClientHeight);
end;
end.