Advertisement
Guest User

Untitled

a guest
Mar 13th, 2012
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 11.52 KB | None | 0 0
  1. unit Main;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Buttons, Vcl.ComCtrls, Vcl.ExtCtrls,
  8.   Vcl.StdCtrls, Vcl.Samples.Spin, Vcl.Menus, JPEG;
  9.  
  10. type
  11.   TFurniture = class(TObject)
  12.   protected
  13.     FX,FY,FHeight,FLength,FWidth: integer;
  14.     FColor: TColor;
  15.     FCanvas: TCanvas;
  16.     procedure Draw(x,y,length,width,height: integer; color: TColor; Canvas: TCanvas); virtual; abstract;
  17.     procedure SetX(value: integer);
  18.     procedure SetY(value: integer);
  19.     procedure SetHeight(value: integer);
  20.     procedure SetLength(value: integer);
  21.     procedure SetWidth(value: integer);
  22.     procedure SetColor(value: TColor);
  23.   public
  24.     constructor Create(x,y,length,width,height: integer; color: TColor; canvas: TCanvas);
  25.   published
  26.     property x: integer read FX write SetX;
  27.     property y: integer read FY write SetY;
  28.     property height: integer read FHeight write SetHeight;
  29.     property length: integer read FLength write SetLength;
  30.     property width: integer read FWidth write SetWidth;
  31.     property color: TColor read FColor write SetColor;
  32.     property canvas: TCanvas read FCanvas;
  33.   end;
  34.  
  35.   TPic = class(TFurniture)
  36.   protected
  37.     FPic: TImage;
  38.     procedure Draw(x,y,length,width,height: integer; color: TColor; Canvas: TCanvas); override;
  39.     procedure SetPic(value: TImage);
  40.   published
  41.     property pic: TImage read FPic write SetPic;
  42.   end;
  43.  
  44.   TStand = class(TFurniture)
  45.   protected
  46.     procedure Draw(x,y,length,width,height: integer; color: TColor; Canvas: TCanvas); override;
  47.   end;
  48.  
  49.   TTable = class(TFurniture)
  50.   protected
  51.     procedure Draw(x,y,length,width,height: integer; color: TColor; Canvas: TCanvas); override;
  52.   end;
  53.  
  54.   TForm1 = class(TForm)
  55.     MainMenu1: TMainMenu;
  56.     N1: TMenuItem;
  57.     N2: TMenuItem;
  58.     N3: TMenuItem;
  59.     N4: TMenuItem;
  60.     SpinEditX: TSpinEdit;
  61.     SpinEditY: TSpinEdit;
  62.     Label1: TLabel;
  63.     Label2: TLabel;
  64.     ColorBox1: TColorBox;
  65.     Label4: TLabel;
  66.     StatusBar1: TStatusBar;
  67.     PaintBox1: TPaintBox;
  68.     ComboBox1: TComboBox;
  69.     Label5: TLabel;
  70.     Label7: TLabel;
  71.     Label8: TLabel;
  72.     SpinEditLength: TSpinEdit;
  73.     SpinEditWidth: TSpinEdit;
  74.     SpinEditHeight: TSpinEdit;
  75.     Button2: TButton;
  76.     GroupBox1: TGroupBox;
  77.     GroupBox2: TGroupBox;
  78.     Label3: TLabel;
  79.     Button1: TButton;
  80.     OpenDialog1: TOpenDialog;
  81.     procedure PaintBox1Paint(Sender: TObject);
  82.     procedure N2Click(Sender: TObject);
  83.     procedure Button2Click(Sender: TObject);
  84.     procedure SpinEditXChange(Sender: TObject);
  85.     procedure SpinEditYChange(Sender: TObject);
  86.     procedure SpinEditLengthChange(Sender: TObject);
  87.     procedure SpinEditWidthChange(Sender: TObject);
  88.     procedure SpinEditHeightChange(Sender: TObject);
  89.     procedure ColorBox1Change(Sender: TObject);
  90.     procedure ComboBox1Change(Sender: TObject);
  91.   private
  92.     { Private declarations }
  93.   public
  94.     { Public declarations }
  95.   end;
  96.  
  97. var
  98.   Form1: TForm1;
  99.   Obj: Array[0..2] of TFurniture;
  100.  
  101. const x0=192;
  102.       y0=250;
  103.  
  104. implementation
  105.  
  106. {$R *.dfm}
  107.  
  108. constructor TFurniture.Create(x,y,length,width,height: integer; color: TColor; canvas: TCanvas);
  109. begin
  110.   FX:=x;
  111.   FY:=y;
  112.   FLength:=length;
  113.   FWidth:=width;
  114.   FHeight:=height;
  115.   FColor:=color;
  116.   FCanvas:=canvas;
  117.   Draw(x,y,length,width,height,color,canvas);
  118. end;
  119.  
  120. procedure TFurniture.SetX(value: Integer);
  121. begin
  122.   self.Draw(FX,FY,FLength,FWidth,FHeight,clBlack,FCanvas);
  123.   FX:=value;
  124.   self.Draw(FX,FY,FLength,FWidth,FHeight,FColor,FCanvas);
  125. end;
  126.  
  127. procedure TFurniture.SetY(value: Integer);
  128. begin
  129.   self.Draw(FX,FY,FLength,FWidth,FHeight,clBlack,FCanvas);
  130.   FY:=value;
  131.   self.Draw(FX,FY,FLength,FWidth,FHeight,FColor,FCanvas);
  132. end;
  133.  
  134. procedure TFurniture.SetHeight(value: integer);
  135. begin
  136.   self.Draw(FX,FY,FLength,FWidth,FHeight,clBlack,FCanvas);
  137.   FHeight:=value;
  138.   self.Draw(FX,FY,FLength,FWidth,FHeight,FColor,FCanvas);
  139. end;
  140.  
  141. procedure TFurniture.SetLength(value: Integer);
  142. begin
  143.   self.Draw(FX,FY,FLength,FWidth,FHeight,clBlack,FCanvas);
  144.   FLength:=value;
  145.   self.Draw(FX,FY,FLength,FWidth,FHeight,FColor,FCanvas);
  146. end;
  147.  
  148. procedure TFurniture.SetWidth(value: Integer);
  149. begin
  150.   self.Draw(FX,FY,FLength,FWidth,FHeight,clBlack,FCanvas);
  151.   FWidth:=value;
  152.   self.Draw(FX,FY,FLength,FWidth,FHeight,FColor,FCanvas);
  153. end;
  154.  
  155. procedure TFurniture.SetColor(value: TColor);
  156. begin
  157.   FColor:=value;
  158.   self.Draw(FX,FY,FLength,FWidth,FHeight,FColor,FCanvas);
  159. end;
  160.  
  161. procedure TPic.SetPic(value: TImage);
  162. begin
  163.   FPic:=value;
  164. end;
  165.  
  166. procedure TPic.Draw(x: Integer; y: Integer; length: Integer; width: Integer; height: Integer; color: TColor; Canvas: TCanvas);
  167. begin
  168.   with Canvas do
  169.   begin
  170.     Pen.Color:=color;
  171.     Pen.Width:=1;
  172.     MoveTo(x,y);
  173.     LineTo(x+length,y);
  174.     LineTo(x+length,y+height);
  175.     LineTo(x,y+height);
  176.     LineTo(x,y);
  177.     LineTo(x-width,y-width);
  178.     LineTo(x+width+length,y-width);
  179.     LineTo(x+length,y);
  180.     MoveTo(x+width+length,y-width);
  181.     LineTo(x+width+length,y+width+height);
  182.     LineTo(x+length,y+height);
  183.     MoveTo(x+width+length,y+width+height);
  184.     LineTo(x-width,y+width+height);
  185.     LineTo(x,y+height);
  186.     MoveTo(x-width,y+width+height);
  187.     LineTo(x-width,y-width);
  188.   end;
  189. end;
  190.  
  191. procedure TStand.Draw(x: Integer; y: Integer; length: Integer; width: Integer; height: Integer; color: TColor; Canvas: TCanvas);
  192. var AngWidth,BoxLength,BoxHeight,PadX,PadY: integer;
  193. begin
  194.   AngWidth:=round(width/1.4); //Переходная величина от квадрата к параллелограмму на sqrt(2)~1.4 меньше ширины(по т.Пифагора)
  195.   PadX:=round(length/10); //Отступы для ящиков тумбы составляют 0.1 от длины/высоты
  196.   PadY:=round(height/10);
  197.   BoxLength:=length-PadX*2; //Существует 2 отсупа по горизонтали
  198.   BoxHeight:=round((height-PadY*3)/2); //И 3 отсупа по вертикали, т.к. мы рисуем 2 ящика
  199.   with Canvas do
  200.   begin
  201.     Pen.Color:=color;
  202.     Pen.Width:=1;
  203.     //Плоскость
  204.     MoveTo(x,y);
  205.     LineTo(x-AngWidth,y+AngWidth);
  206.     LineTo(x+length-AngWidth,y+AngWidth);
  207.     LineTo(x+length,y);
  208.     LineTo(x,y);
  209.     //Основание
  210.     MoveTo(x-AngWidth,y+AngWidth);
  211.     LineTo(x-AngWidth,y+AngWidth+height);
  212.     MoveTo(x+length-AngWidth,y+AngWidth);
  213.     LineTo(x+Length-AngWidth,y+AngWidth+height);
  214.     MoveTo(x+length,y);
  215.     LineTo(x+length,y+height);
  216.     LineTo(x+length-AngWidth,y+AngWidth+height);
  217.     LineTo(x-AngWidth,y+AngWidth+height);
  218.     //Первый ящик
  219.     MoveTo(x-AngWidth+PadX,y+AngWidth+PadY);
  220.     LineTo(x-AngWidth+PadX,y+AngWidth+PadY+BoxHeight);
  221.     LineTo(x-AngWidth+PadX+BoxLength,y+AngWidth+PadY+BoxHeight);
  222.     LineTo(x-AngWidth+PadX+BoxLength,y+AngWidth+PadY);
  223.     LineTo(x-AngWidth+PadX,y+AngWidth+PadY);
  224.     MoveTo(x-AngWidth+round(length/2)-PadX,y+AngWidth+PadY*2);
  225.     LineTo(x-AngWidth+round(length/2)+PadX,y+AngWidth+PadY*2);
  226.     //Второй ящик
  227.     MoveTo(x-AngWidth+PadX,y+AngWidth+PadY*2+BoxHeight);
  228.     LineTo(x-AngWidth+PadX,y+AngWidth+PadY*2+BoxHeight*2);
  229.     LineTo(x-AngWidth+PadX+BoxLength,y+AngWidth+PadY*2+BoxHeight*2);
  230.     LineTo(x-AngWidth+PadX+BoxLength,y+AngWidth+PadY*2+BoxHeight);
  231.     LineTo(x-AngWidth+PadX,y+AngWidth+PadY*2+BoxHeight);
  232.     MoveTo(x-AngWidth+round(length/2)-PadX,y+AngWidth+PadY*3+BoxHeight);
  233.     LineTo(x-AngWidth+round(length/2)+PadX,y+AngWidth+PadY*3+BoxHeight);
  234.   end;
  235. end;
  236.  
  237. procedure TTable.Draw(x: Integer; y: Integer; length: Integer; width: Integer; height: Integer; color: TColor; Canvas: TCanvas);
  238. var AngWidth: integer;
  239. begin
  240.   AngWidth:=round(width/1.4); //Переходная величина от квадрата к параллелограмму на sqrt(2)~1.4 меньше ширины(по т.Пифагора)
  241.   with Canvas do
  242.   begin
  243.     Pen.Color:=color;
  244.     Pen.Width:=1;
  245.     //Плоскость
  246.     MoveTo(x, y);
  247.     LineTo(x-AngWidth,y+AngWidth);
  248.     LineTo(x+length-AngWidth,y+AngWidth);
  249.     LineTo(x+length,y);
  250.     LineTo(x,y);
  251.     //Ножки
  252.     MoveTo(x-AngWidth,y+AngWidth);
  253.     LineTo(x-AngWidth,y+AngWidth+height);
  254.     MoveTo(x+length-AngWidth,y+AngWidth);
  255.     LineTo(x+Length-AngWidth,y+AngWidth+height);
  256.     MoveTo(x+length,y);
  257.     LineTo(x+length,y+height);
  258.   end;
  259. end;
  260.  
  261. procedure TForm1.PaintBox1Paint(Sender: TObject);
  262. begin
  263.   with PaintBox1.Canvas do
  264.   begin
  265.     Brush.Color:=clBlack; //Черный цвет кисти
  266.     FillRect(ClientRect); //Заливка фона
  267.     Pen.Color:=clWhite; //Цвет пера
  268.     Pen.Width:=2; //Толщина пера
  269.     MoveTo(x0,y0); //Перемещаемся в начало координат
  270.     LineTo(PaintBox1.Width,y0); //Ось X
  271.     MoveTo(x0,y0);
  272.     LineTo(x0,0); //Ось Y
  273.     MoveTo(x0,y0);
  274.     LineTo(0,PaintBox1.Height); //Ось Z
  275.   end;
  276. end;
  277.  
  278. procedure TForm1.Button2Click(Sender: TObject);
  279. begin
  280.   if Obj[ComboBox1.ItemIndex]=nil then
  281.     case ComboBox1.ItemIndex of
  282.       0: Obj[0]:=TPic.Create(SpinEditX.Value,SpinEditY.Value,SpinEditLength.Value,SpinEditWidth.Value,SpinEditHeight.Value,ColorBox1.Selected,PaintBox1.Canvas);
  283.       1: Obj[1]:=TTable.Create(SpinEditX.Value,SpinEditY.Value,SpinEditLength.Value,SpinEditWidth.Value,SpinEditHeight.Value,ColorBox1.Selected,PaintBox1.Canvas);
  284.       2: Obj[2]:=TStand.Create(SpinEditX.Value,SpinEditY.Value,SpinEditLength.Value,SpinEditWidth.Value,SpinEditHeight.Value,ColorBox1.Selected,PaintBox1.Canvas);
  285.     end
  286.     else ShowMessage('Создание запрещено, т.к. данный объект уже был создан. Вы можете изменить его параметры.');
  287. end;
  288.  
  289. procedure TForm1.N2Click(Sender: TObject);
  290. begin
  291.   Close;
  292. end;
  293.  
  294. procedure TForm1.SpinEditXChange(Sender: TObject);
  295. begin
  296.   if Obj[ComboBox1.ItemIndex]<>nil then //Данная проверка предназначена для исключения краша программы из-за неправильного распределения памяти
  297.     Obj[ComboBox1.ItemIndex].x:=SpinEditX.Value;
  298. end;
  299.  
  300. procedure TForm1.SpinEditYChange(Sender: TObject);
  301. begin
  302.   if Obj[ComboBox1.ItemIndex]<>nil then
  303.     Obj[ComboBox1.ItemIndex].y:=SpinEditY.Value;
  304. end;
  305.  
  306. procedure TForm1.SpinEditLengthChange(Sender: TObject);
  307. begin
  308.   if Obj[ComboBox1.ItemIndex]<>nil then
  309.     Obj[ComboBox1.ItemIndex].length:=SpinEditLength.Value;
  310. end;
  311.  
  312. procedure TForm1.SpinEditWidthChange(Sender: TObject);
  313. begin
  314.   if Obj[ComboBox1.ItemIndex]<>nil then
  315.     Obj[ComboBox1.ItemIndex].width:=SpinEditWidth.Value;
  316. end;
  317.  
  318. procedure TForm1.SpinEditHeightChange(Sender: TObject);
  319. begin
  320.   if Obj[ComboBox1.ItemIndex]<>nil then
  321.     Obj[ComboBox1.ItemIndex].height:=SpinEditHeight.Value;
  322. end;
  323.  
  324. procedure TForm1.ColorBox1Change(Sender: TObject);
  325. begin
  326.   if Obj[ComboBox1.ItemIndex]<>nil then
  327.     Obj[ComboBox1.ItemIndex].color:=ColorBox1.Selected;
  328. end;
  329.  
  330. procedure TForm1.ComboBox1Change(Sender: TObject);
  331. begin
  332.   if Obj[ComboBox1.ItemIndex]<>nil then
  333.   begin
  334.     SpinEditX.Value:=Obj[ComboBox1.ItemIndex].x;
  335.     SpinEditY.Value:=Obj[ComboBox1.ItemIndex].y;
  336.     SpinEditLength.Value:=Obj[ComboBox1.ItemIndex].length;
  337.     SpinEditWidth.Value:=Obj[ComboBox1.ItemIndex].width;
  338.     SpinEditHeight.Value:=Obj[ComboBox1.ItemIndex].height;
  339.     ColorBox1.Selected:=Obj[ComboBox1.ItemIndex].color;
  340.   end;
  341.   if ComboBox1.ItemIndex=0 then
  342.   begin
  343.     GroupBox2.Show;
  344.     Label3.Show;
  345.     Button1.Show;
  346.   end
  347.   else
  348.   begin
  349.     GroupBox2.Hide;
  350.     Label3.Hide;
  351.     Button1.Hide;
  352.   end;
  353. end;
  354.  
  355. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement