Solved

Drawing

Posted on 1997-09-08
8
391 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
  • 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

803 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