program Rectangle;
uses GraphWPF;
type
TRectangle = class
private
FX, FY: real;
FWidth, FHeight: real;
FColor: GColor;
public
constructor Create(x, y, w, h: real; c: GColor);
function GetArea: real;
function GetPerimeter: real;
procedure Draw;
end;
var
MyRect: TRectangle;
inputW, inputH: string;
stage: integer;
constructor TRectangle.Create(x, y, w, h: real; c: GColor);
begin
FX := x; FY := y;
FWidth := w; FHeight := h;
FColor := c;
end;
function TRectangle.GetArea: real;
begin
Result := FWidth * FHeight;
end;
function TRectangle.GetPerimeter: real;
begin
Result := 2 * (FWidth + FHeight);
end;
procedure TRectangle.Draw;
var
centerX, centerY: real;
begin
Brush.Color := FColor;
Pen.Color := Colors.DarkSlateGray;
Pen.Width := 2;
DrawRectangle(FX, FY, FWidth, FHeight);
Font.Name := 'Arial';
Font.Size := 16;
Font.Color := Colors.Black;
centerX := FX + (FWidth / 2) - 60;
centerY := FY + (FHeight / 2) - 20;
TextOut(centerX, centerY, 'Площадь: ' + GetArea.ToString);
TextOut(centerX, centerY + 25, 'Периметр: ' + GetPerimeter.ToString);
end;
procedure KeyPressHandler(ch: char);
var
w, h: real;
begin
if stage = 0 then
begin
if (ch >= '0') and (ch <= '9') then
inputW := inputW + ch
else if ch = #8 then
begin
if Length(inputW) > 0 then Delete(inputW, Length(inputW), 1);
end
else if ch = #13 then
begin
if inputW <> '' then stage := 1;
end;
end
else if stage = 1 then
begin
if (ch >= '0') and (ch <= '9') then
inputH := inputH + ch
else if ch = #8 then
begin
if Length(inputH) > 0 then Delete(inputH, Length(inputH), 1);
end
else if ch = #13 then
begin
if inputH <> '' then
begin
stage := 2;
w := StrToInt(inputW);
h := StrToInt(inputH);
if w > 100 then w := 100;
if h > 100 then h := 100;
if w < 1 then w := 1;
if h < 1 then h := 1;
MyRect := TRectangle.Create(250, 150, w * 4, h * 4, Colors.LightSkyBlue);
end;
end;
end
else if stage = 2 then
begin
if (ch = 'r') or (ch = 'R') or (ch = 'к') or (ch = 'К') then
begin
stage := 0;
inputW := '';
inputH := '';
end;
end;
Window.Clear;
Font.Name := 'Arial';
if stage = 0 then
begin
Font.Size := 20;
Font.Color := Colors.Black;
TextOut(50, 50, 'Введите ШИРИНУ (нажимайте цифры, затем Enter): ' + inputW + '_');
end
else if stage = 1 then
begin
Font.Size := 16;
Font.Color := Colors.Gray;
TextOut(50, 50, 'Ширина: ' + inputW);
Font.Size := 20;
Font.Color := Colors.Black;
TextOut(50, 100, 'Введите ВЫСОТУ (затем Enter): ' + inputH + '_');
end
else if stage = 2 then
begin
Font.Size := 16;
Font.Color := Colors.Gray;
TextOut(50, 50, 'Ширина: ' + inputW + ' | Высота: ' + inputH);
TextOut(50, 80, 'Нажмите "R" (или "К"), чтобы ввести заново.');
MyRect.Draw;
end;
end;
begin
Window.Title := 'Расчет и рисование прямоугольника';
Window.Width := 800;
Window.Height := 600;
stage := 0;
inputW := '';
inputH := '';
OnKeyPress := KeyPressHandler;
KeyPressHandler(#0);
end.