Link to home
Start Free TrialLog in
Avatar of Ham
Ham

asked on

Drawing

Hello,

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?)?
ASKER CERTIFIED SOLUTION
Avatar of StevenB
StevenB

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ham
Ham

ASKER

(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;

interface

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

type
  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);
  private
    ShapeCount : Integer;
    Function GetRandomShape : TGPShapeType;
    { Private declarations }
  public
    ShapeList : TList;

    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

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

procedure TForm1.FormDestroy(Sender: TObject);
begin
  ShapeList.Free;
end;

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

  AShape := TManyShape.Create(self);
  Inc(ShapeCount);
  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;
  ShapeList.Add(AShape);
  Edit1.Text := AShape.Name;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  Counter : Integer;
  Done : Boolean;
  AShape : TManyShape;
begin
  // 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;
    Repeat
      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;
      end;
      Inc(Counter);
    Until Done or (Counter >= ShapeList.Count);
  end;
  If Not Done then ShowMessage('Shape '+Edit1.Text+' does not exist');
end;

Function TForm1.GetRandomShape : TGPShapeType;
var
  Counter : Integer;
begin
  // 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;
  end;
end;


end.


  Steven.
Avatar of Ham

ASKER

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.
Avatar of Ham

ASKER

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.

Ham.  
 Ham,
  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.

  Steven.
Avatar of Ham

ASKER

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.

  Steven.