Solved

Drawing

Posted on 1997-09-08
8
395 Views
Last Modified: 2010-04-04
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?)?
0
Comment
Question by:Ham
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 4

Accepted Solution

by:
StevenB earned 40 total points
ID: 1344335
 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.

  Steven.
0
 

Author Comment

by:Ham
ID: 1344338
(To add) The number of shapes that will be drawn is not constant.
0
 
LVL 4

Expert Comment

by:StevenB
ID: 1344339
 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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Ham
ID: 1344340
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.
0
 

Author Comment

by:Ham
ID: 1344341
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.  
0
 
LVL 4

Expert Comment

by:StevenB
ID: 1344336
 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.
0
 

Author Comment

by:Ham
ID: 1344337
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.
0
 
LVL 4

Expert Comment

by:StevenB
ID: 1344342
 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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question