Q. I want to draw many repetitive shapes (using a shape component called TManyShapes) on a canvas (I have am using TImage). Firstly, by using a PROCEDURE or FUNCTION how can I create many (alot of) shapes on that canvas (TImage) without having a conflict a similiar names etc.., and is TImage the best thing to use as a canvas or do you know of something better (and if thats the case - where do I get it?)?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 How does this TManyShapes component work, where did you obtain it? Without knowing more it's hard to give a good answer, however you're perhaps going to need to create a TList of the multiple shapes you will be drawing and reference them by their index in the list. I'll explain this further if I'm right, however the TManyShapes component sounds like it might handle this for you. Let me know how to get a copy and I'll check it out.

  I Prefer to use a TPaintBox as a canvas, since it carries less overheads than the TImage.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HamAuthor Commented:
(To add) The number of shapes that will be drawn is not constant.
 Sorry for the delay, I normally reply quicker, but I was quite busy yesterday. I've written this quick example, using the TManyShapes component. Hopefully it will demonstrate most of the things you're asking. If you've any more querries the don't hesitate to ask. If you wan't me to mail you the full Project of this example then post your EMail address as a comment. Here's the example, hope it helps:

unit ShapeUnit;


  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Gpshape;

  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    ShapeCount : Integer;
    Function GetRandomShape : TGPShapeType;
    { Private declarations }
    ShapeList : TList;

    { Public declarations }

  Form1: TForm1;


{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
  ShapeList := TList.Create;  // This is the list which will hold the shapes
  ShapeCount := 0;

procedure TForm1.FormDestroy(Sender: TObject);

procedure TForm1.Button1Click(Sender: TObject);
  AShape : TManyShape;
  // This Procedure shows how new shapes can be created and added to the list

  AShape := TManyShape.Create(self);
  AShape.Shape := GetRandomShape;
  AShape.Name := 'Shape'+IntToStr(ShapeCount);
  AShape.width := Random(200);
  AShape.height := Random(200);
  AShape.Top := Random((Height-AShape.Height-10));
  AShape.Left := Random((Width-AShape.Width-10));
  AShape.Parent := Self;
  Edit1.Text := AShape.Name;

procedure TForm1.Button2Click(Sender: TObject);
  Counter : Integer;
  Done : Boolean;
  AShape : TManyShape;
  // This Procedure shows how to search the List for a named shape and alter its properties

  Done := False;
  If ShapeList.Count > 0 then begin
    Counter := 0;
      AShape := ShapeList[Counter];
      If AShape.Name = Edit1.Text then begin // Search for shape named in Edit1
        AShape.BackColor := RGB(Random(255),Random(255),Random(255));
        Done := True;
    Until Done or (Counter >= ShapeList.Count);
  If Not Done then ShowMessage('Shape '+Edit1.Text+' does not exist');

Function TForm1.GetRandomShape : TGPShapeType;
  Counter : Integer;
  // Function to simply return a random shape

  Counter := Random(13);
  Case Counter Of
    1: Result := gstCircle;
    2: Result := gstDiamond;
    3: Result := gstEllipse;
    4: Result := gstPolyGon;
    5: Result := gstRectangle;
    6: Result := gstRoundRect;
    7: Result := gstRoundSquare;
    8: Result := gstSquare;
    9: Result := gstStar;
    10: Result := gstTriangleDown;
    11: Result := gstTriangleLeft;
    12: Result := gstTriangleRight;
    13: Result := gstTriangleUp;


Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

HamAuthor Commented:
Ill try it out then Ill get back to you if I run into any problems. Yes please, I would like the full project (EMAIL: z2122497@student.unsw.edu.au), I didnt realise in order to do what I wanted - the code was extensive.
Thankyou, Ham.
HamAuthor Commented:
Thankyou for the source.
I have manipulated the code to read a given set of xy coordinates
but I am stumped on what I believe is something so simple -
Instead of the shapes being drawn on the main (parent) form, I want them to be drawn on a child of the form - a TImage or TPaintBox or any other component that can have other child components other than the main form.

  I got an EMail telling me you'd added a comment, but all the comments seem to have disappeared on my browser so I can't respond. Try retyping your comment.

HamAuthor Commented:
Iteration 2:

Thankyou for the source code.
I manipulted the code to respond to a set of coordinated points.
However I am stuck on what should be a simple problem -

Q. How do I draw the shapes on a child to the main form (as the shapes are being drawn on the main form). ie. I would like the shapes to be drawn on a canvas or more importantly on any other component that can be drawn on other than the main form. Something to do with changing focus/make something active?

Thankyou, Ham.
 The parent of the Shapes can be set to any Windowed Control. There is a line in the code which states : AShape.Parent := self;
simply change the 'Self' to be, for example, 'Panel1' and the Shapes will be displayed on the Panel.
  TImage and TPaintbox are NOT windowed controls. The distinction between windowed controls and NonWindowed controls is quite important and I suggest you check up on it if you havn't already. Essentially for your purposes this means that they cannot be the Parents of the Shapes. I suggest a TPanel as the correct windowed control for your purposes.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.