Solved

Drawing

Posted on 1997-09-08
8
389 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
Comment Utility
 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
Comment Utility
(To add) The number of shapes that will be drawn is not constant.
0
 
LVL 4

Expert Comment

by:StevenB
Comment Utility
 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
 

Author Comment

by:Ham
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:Ham
Comment Utility
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
Comment Utility
 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
Comment Utility
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
Comment Utility
 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now