home *** CD-ROM | disk | FTP | other *** search
/ Delphi 5 for Professionals / DELPHI5.iso / AddOns / Components / TEECHART / Delphi1_And_Delphi2 / EXAMPLES / EXTENDED / UDRAW3D.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-10-24  |  6.6 KB  |  244 lines

  1. {*********************************************}
  2. { TeeChart Delphi Component Library           }
  3. { Draw3D component Demo                       }
  4. { Copyright (c) 1995-1998 by David Berneda    }
  5. { All rights reserved                         }
  6. {*********************************************}
  7. unit udraw3d;
  8.  
  9. interface
  10.  
  11. uses
  12.   WinProcs,WinTypes, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  13.   TeeProcs, TeeComma, StdCtrls, ExtCtrls;
  14.  
  15. type
  16.   TDraw3DForm = class(TForm)
  17.     Panel1: TPanel;
  18.     Memo1: TMemo;
  19.     Button1: TButton;
  20.     TeeCommander1: TTeeCommander;
  21.     Draw3D1: TDraw3D;
  22.     CheckBox1: TCheckBox;
  23.     CheckBox2: TCheckBox;
  24.     CheckBoxDrawAxes: TCheckBox;
  25.     CheckBoxDrawRoom: TCheckBox;
  26.     CheckBoxGradient: TCheckBox;
  27.     procedure Button1Click(Sender: TObject);
  28.     procedure FormCreate(Sender: TObject);
  29.     procedure Draw3D1Paint(Sender: TObject; const Rect: TRect);
  30.     procedure CheckBox1Click(Sender: TObject);
  31.     procedure CheckBox2Click(Sender: TObject);
  32.     procedure CheckBoxDrawAxesClick(Sender: TObject);
  33.     procedure CheckBoxDrawRoomClick(Sender: TObject);
  34.     procedure CheckBoxGradientClick(Sender: TObject);
  35.   private
  36.     { Private declarations }
  37.   public
  38.     { Public declarations }
  39.   end;
  40.  
  41. implementation
  42.  
  43. {$R *.DFM}
  44.  
  45. { This unit is necessary to access the Canvas constants }
  46. Uses TeCanvas;
  47.  
  48. procedure TDraw3DForm.Button1Click(Sender: TObject);
  49. begin
  50.   Close;
  51. end;
  52.  
  53. procedure TDraw3DForm.FormCreate(Sender: TObject);
  54. begin
  55.   { Disable "ortho" perspective. We want full 3D }
  56.   Draw3D1.View3DOptions.Orthogonal:=False;
  57.  
  58.   { Disable the toolbar "normal" button }
  59.   TeeCommander1.ButtonNormal.Enabled:=False;
  60.  
  61.   { and select the "rotate" button }
  62.   TeeCommander1.ButtonRotate.Down:=True;
  63.  
  64.   { Nice rotation parameters... }
  65.   With Draw3D1.View3DOptions do
  66.   begin
  67.     Rotation:=330;
  68.     Elevation:=325;
  69.   end;
  70. end;
  71.  
  72. { This small function returns a random color }
  73. Function RandomColor:TColor;
  74. begin
  75.   Case Random(8) of
  76.     0: result:=clRed;
  77.     1: result:=clYellow;
  78.     2: result:=clGreen;
  79.     3: result:=clBlue;
  80.     4: result:=clNavy;
  81.     5: result:=clAqua;
  82.     6: result:=clLime;
  83.   else
  84.     result:=clWhite;
  85.   end;
  86. end;
  87.  
  88. procedure TDraw3DForm.Draw3D1Paint(Sender: TObject; const Rect: TRect);
  89. Var CenterX     : Integer;
  90.     CenterY     : Integer;
  91.     CenterZ     : Integer;
  92.     WithShadows : Boolean;
  93.     t           : Integer;
  94.     tmpX        : Integer;
  95.     tmpY        : Integer;
  96. begin
  97.   { We should use this event to "Paint" shapes onto a Draw3D
  98.     component Canvas.
  99.   }
  100.  
  101.   { We can for example, use the "Center" position in the 3D space }
  102.   CenterX:=Draw3D1.ChartXCenter;
  103.   CenterY:=Draw3D1.ChartYCenter;
  104.   CenterZ:=0;
  105.  
  106.   { Determine if draw dark shape shadows or not }
  107.   WithShadows:=CheckBox1.Checked;
  108.  
  109.   { Using the Canvas, let's draw some shapes... }
  110.   With Draw3D1.Canvas do
  111.   begin
  112.  
  113.     { Draw a "room". Notice only 2D drawing methods are used here }
  114.     if CheckBoxDrawRoom.Checked then
  115.     begin
  116.       { use a gradient to simulate a "room" ... }
  117.       if CheckBoxGradient.Checked then
  118.          GradientFill(Draw3D1.ChartBounds,clWhite,clGray,gdFromCenter)
  119.       else
  120.       { or use polygons... }
  121.       begin
  122.         Pen.Style:=psSolid;
  123.         Pen.Color:=clBlack;
  124.         Brush.Color:=clWhite;
  125.         With Draw3D1.ChartBounds do
  126.         begin
  127.           tmpX:=(Right-Left) div 4;
  128.           tmpY:=(Bottom-Top) div 4;
  129.           Polygon([ Point(Left,Bottom),Point(Left,Top),
  130.                     Point(Left+tmpX,Top+tmpY),Point(Left+tmpX,Bottom-tmpY)]);
  131.           Polygon([ Point(Right,Bottom),Point(Right,Top),
  132.                     Point(Right-tmpX,Top+tmpY),Point(Right-tmpX,Bottom-tmpY)]);
  133.           Brush.Color:=clGray;
  134.           Rectangle(Left+tmpX,Top+tmpY,Right-tmpX,Bottom-tmpY);
  135.         end;
  136.       end;
  137.     end;
  138.  
  139.     if CheckBoxDrawAxes.Checked then
  140.     begin
  141.       { Draw the "axis" }
  142.       Pen.Color:=clNavy;
  143.       Pen.Style:=psDot;
  144.       BackMode:=cbmTransparent;
  145.  
  146.       VertLine3D(CenterX,CenterY-150,CenterY+150,CenterZ);
  147.       HorizLine3D(CenterX-150,CenterX+150,CenterY,CenterZ);
  148.       ZLine3D(CenterX,CenterY,CenterZ-150,CenterZ+150);
  149.     end;
  150.  
  151.     { Draw 100 small random dots, to simulate "dust" }
  152.     for t:=0 to 100 do
  153.         Pixels3D[200+Random(50),120+Random(100),20+Random(100)]:=RandomColor;
  154.  
  155.     { Draw several shapes, planes, pyramids, cubes, etc }
  156.     Pen.Color:=clBlack;
  157.     Pen.Style:=psSolid;
  158.  
  159.     Brush.Color:=clAqua;
  160.     Plane3D(Point(5,200),Point(90,240), -10,30);
  161.  
  162.     Brush.Color:=clYellow;
  163.     Pyramid(False,5,150,130,200,5,30,WithShadows);
  164.  
  165.     Brush.Color:=clWhite;
  166.     Cube(5,80,80,120,5,30,WithShadows);
  167.  
  168.     Brush.Color:=clLime;
  169.     Cylinder(False,150,50,190,200,5,50,WithShadows);
  170.  
  171.     Brush.Color:=clRed;
  172.     EllipseWithZ(40,100,80,140,-20);
  173.  
  174.     Brush.Color:=clBlue;
  175.     Pen.Color:=clYellow;
  176.     Cube(CenterX+130,CenterX+170,120,190,70,100,WithShadows);
  177.  
  178.     Brush.Color:=clRed;
  179.     Pen.Color:=clWhite;
  180.     Cube(CenterX+30,CenterX+70,120,190,70,100,WithShadows);
  181.  
  182.     Brush.Color:=clRed;
  183.     Pen.Color:=clYellow;
  184.     Cube(CenterX+130,CenterX+170,120,190,10,30,WithShadows);
  185.  
  186.     Brush.Color:=clBlue;
  187.     Pen.Color:=clWhite;
  188.     Cube(CenterX+30,CenterX+70,120,190,10,30,WithShadows);
  189.  
  190.     Pen.Style:=psClear;
  191.     Brush.Color:=clRed;
  192.     Cylinder(False,CenterX+30,70,CenterX+170,100,70,100,WithShadows);
  193.     Cylinder(False,CenterX+30,70,CenterX+170,100,10,40,WithShadows);
  194.     Pen.Style:=psSolid;
  195.  
  196.     { Draw a pyramid on top of the cylinder }
  197.     Brush.Color:=clWhite;
  198.     Pen.Color:=clSilver;
  199.     Pyramid(True,CenterX+30,40,CenterX+170,60,10,100,WithShadows);
  200.  
  201.     { Draw a black line on top of the pyramid }
  202.     Pen.Color:=clBlack;
  203.     Pen.Width:=2;
  204.     MoveTo3D( CenterX+100, 40, 45 );
  205.     LineTo3D( CenterX+100, 20, 45 );
  206.  
  207.     { Draw text at 3D xyz position, on top of the previous line }
  208.     Font.Color:=clBlack;
  209.     TextAlign:=ta_Center;
  210.     BackMode:=cbmTransparent;
  211.     TextOut3D(CenterX+100,0,45,'Hello!');
  212.   end;
  213. end;
  214.  
  215. procedure TDraw3DForm.CheckBox1Click(Sender: TObject);
  216. begin
  217.   Draw3D1.Repaint;
  218. end;
  219.  
  220. procedure TDraw3DForm.CheckBox2Click(Sender: TObject);
  221. begin
  222.   { Switch 2D / 3D mode }
  223.   Draw3D1.View3D:=not CheckBox2.Checked;
  224. end;
  225.  
  226. procedure TDraw3DForm.CheckBoxDrawAxesClick(Sender: TObject);
  227. begin
  228.   Draw3D1.Repaint;
  229. end;
  230.  
  231. procedure TDraw3DForm.CheckBoxDrawRoomClick(Sender: TObject);
  232. begin
  233.   { repaint the draw3d component to remove the room }
  234.   Draw3D1.Repaint;
  235.   CheckBoxGradient.Enabled:=CheckBoxDrawRoom.Checked;
  236. end;
  237.  
  238. procedure TDraw3DForm.CheckBoxGradientClick(Sender: TObject);
  239. begin
  240.   Draw3D1.Repaint;
  241. end;
  242.  
  243. end.
  244.