home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l040 / 11.ddi / WDOCDEMO.ZIP / COLLECT4.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-27  |  4.9 KB  |  206 lines

  1. {************************************************}
  2. {                                                }
  3. {   ObjectWindows Demo                           }
  4. {   Copyright (c) 1992 by Borland International  }
  5. {                                                }
  6. {************************************************}
  7.  
  8. { Create a collection of graphical objects: Points, Circles,
  9.   and Rectangles. Use the ForEach iterator to display each
  10.   object in the collection. }
  11.  
  12. program Collect4;
  13.  
  14. uses Objects, WinTypes, WinProcs, OWindows;
  15.  
  16. const
  17.   NumToDraw = 10;
  18.  
  19. { ********************************** }
  20. { ******  Graphical Objects  ******* }
  21. { ********************************** }
  22.  
  23. type
  24.   PGraphObject = ^TGraphObject;
  25.   TGraphObject = object(TObject)
  26.     Rect: TRect;
  27.     constructor Init(Bounds: TRect);
  28.     procedure Draw(DC: HDC); virtual;
  29.   end;
  30.  
  31.   PGraphEllipse = ^TGraphEllipse;
  32.   TGraphEllipse = object(TGraphObject)
  33.     procedure Draw(DC: HDC); virtual;
  34.   end;
  35.  
  36.   PGraphRect = ^TGraphRect;
  37.   TGraphRect = object(TGraphObject)
  38.     procedure Draw(DC: HDC); virtual;
  39.   end;
  40.  
  41.   PGraphPie = ^TGraphPie;
  42.   TGraphPie = object(TGraphObject)
  43.     ArcStart, ArcEnd: TPoint;
  44.     constructor Init(Bounds: TRect);
  45.     procedure Draw(DC: HDC); virtual;
  46.   end;
  47.  
  48. { TGraphObject }
  49. constructor TGraphObject.Init(Bounds: TRect);
  50. var
  51.   Height, Width: Word;
  52. begin
  53.   inherited Init;
  54.   with Bounds do
  55.   begin
  56.     Height := Random(Bottom - Top) div 2 + 10;
  57.     Width := Random(Right - Left) div 3 + 15;
  58.   end;
  59.   with Rect do
  60.   begin
  61.     Left := Random(Bounds.Right - Bounds.Left - Width);
  62.     Right := Left + Width;
  63.     Top := Random(Bounds.Bottom - Bounds.Top - Height);
  64.     Bottom := Top + Height;
  65.   end;
  66. end;
  67.  
  68. procedure TGraphObject.Draw(DC: HDC);
  69. begin
  70.   Abstract;
  71. end;
  72.  
  73. { TGraphEllipse }
  74. procedure TGraphEllipse.Draw(DC: HDC);
  75. begin
  76.   with Rect do
  77.     Ellipse(DC, Left, Top, Right, Bottom);
  78. end;
  79.  
  80. { TGraphRect }
  81. procedure TGraphRect.Draw(DC: HDC);
  82. begin
  83.   with Rect do
  84.     Rectangle(DC, Left, Top, Right, Bottom);
  85. end;
  86.  
  87. { TGraphPie }
  88. constructor TGraphPie.Init(Bounds: TRect);
  89. var Height, Width: Word;
  90. begin
  91.   inherited Init(Bounds);
  92.   with Bounds do
  93.   begin
  94.     Height := Random(Bottom - Top);
  95.     Width := Random(Right - Left);
  96.  
  97.     ArcStart.X := Random(Right - Left - Width);
  98.     ArcEnd.X := ArcStart.X + Width;
  99.     ArcStart.Y := Random(Bottom - Top - Height);
  100.     ArcEnd.Y := ArcStart.Y + Height;
  101.   end;
  102. end;
  103.  
  104. procedure TGraphPie.Draw;
  105. begin
  106.   with Rect do
  107.     Pie(DC, Left, Top, Right, Bottom, ArcStart.X, ArcStart.Y, ArcEnd.X, ArcEnd.Y);
  108. end;
  109.  
  110. { ********************************** }
  111. { *********  Graph Window  ********* }
  112. { ********************************** }
  113. type
  114.   { Define a TApplication descendant }
  115.   TGraphApp = object(TApplication)
  116.     procedure InitMainWindow; virtual;
  117.   end;
  118.  
  119.   PGraphWindow = ^TGraphWindow;
  120.   TGraphWindow = object(TWindow)
  121.     GraphicsList: PCollection;
  122.     destructor Done; virtual;
  123.     procedure Paint(PaintDC: HDC; var PaintInfo: TPaintStruct); virtual;
  124.     procedure SetupWindow; virtual;
  125.   end;
  126.  
  127.  
  128. { TGraphApp }
  129. procedure TGraphApp.InitMainWindow;
  130. begin
  131.   MainWindow := New(PGraphWindow,
  132.     Init(nil, 'Collection of Graphical Objects'));
  133. end;
  134.  
  135. { TGraphWindow }
  136. procedure TGraphWindow.SetupWindow;
  137. var
  138.   Bounds: TRect;
  139.   I: Integer;
  140.   P: PGraphObject;
  141. begin
  142.   TWindow.SetupWindow;
  143.   GetClientRect(HWindow, Bounds);
  144.  
  145.   { Instantiate a collection of objects }
  146.  
  147.   { Initialize collection to hold 10 elements first, then grow by 5's }
  148.   GraphicsList := New(PCollection, Init(10, 5));
  149.  
  150.   for I := 1 to NumToDraw do
  151.   begin
  152.     case I mod 3 of                      { Create it }
  153.       0: P := New(PGraphRect, Init(Bounds));
  154.       1: P := New(PGraphEllipse, Init(Bounds));
  155.       0..2: P := New(PGraphPie, Init(Bounds));
  156.     end;
  157.     GraphicsList^.Insert(P);                     { Add it to collection }
  158.   end;
  159. end;
  160.  
  161. destructor TGraphWindow.Done;
  162. begin
  163.   Dispose(GraphicsList, Done);         { Delete collection }
  164.   inherited Done;
  165. end;
  166.  
  167. procedure TGraphWindow.Paint(PaintDC: HDC; var PaintInfo: TPaintStruct);
  168.  
  169. { Nest the iterator method inside Paint so it can access the DC }
  170. procedure DrawAll(C: PCollection); far;
  171.  
  172. { Nested, far procedure. Receives one
  173.   collection element--a GraphObject, and
  174.   calls that elements Draw method.
  175. }
  176.  
  177. procedure CallDraw(P : PGraphObject); far;
  178. begin
  179.   P^.Draw(PaintDC);                            { Call Draw method }
  180. end;
  181.  
  182. begin { DrawAll }
  183.   C^.ForEach(@CallDraw);              { Draw each object }
  184. end;
  185.  
  186. begin
  187.   if GraphicsList <> nil then DrawAll(GraphicsList);
  188. end;
  189.  
  190.  
  191. { ********************************** }
  192. { **********  Main Program ********* }
  193. { ********************************** }
  194.  
  195. { Declare a variable of type TGraphApp }
  196. var
  197.   GraphApp: TGraphApp;
  198.  
  199. { Run the GraphApp }
  200. begin
  201.   GraphApp.Init('GraphApp');
  202.   GraphApp.Run;
  203.   GraphApp.Done;
  204. end.
  205.  
  206.