Solved

TImage.Canvas Polygon

Posted on 1998-12-18
2
646 Views
Last Modified: 2010-05-19
Hi, how do I draw a user defined polygon ? The user clicks
with the mouse on some points. Then a polygon should be drawn. How do I add the points to the polygon and how do I create an array for them ? Source ?
0
Comment
Question by:ItsMe
2 Comments
 
LVL 8

Accepted Solution

by:
ZifNab earned 30 total points
ID: 1351761
Hi ItsMe,

This is a way :

unit Unit1;

interface

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

const
  MaxPoints = 10;

type
  TForm1 = class(TForm)
    PaintBox1: TPaintBox;
    StorePoly: TButton;
    DrawPoly: TButton;
    Memo1: TMemo;
    procedure StorePolyClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure DrawPolyClick(Sender: TObject);
    procedure PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
    StoringMode : Boolean;
    PolygonArray : Array[0..MaxPoints-1] of TPoint;
    PointNumber : 0..MaxPoints-1;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.StorePolyClick(Sender: TObject);
begin
 {clear canvas}
 with paintbox1.canvas do begin
  Brush.Color := clWhite;
  FillRect(clipRect);
 end;
 memo1.lines.clear;
{clear polygonarray}
 FillChar(PolygonArray,SizeOf(PolygonArray),0);
 PointNumber := 0;
{start storing polygon  }
 storingMode := true;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 StoringMode := false;
end;

procedure TForm1.DrawPolyClick(Sender: TObject);
begin
 {draw the polygon}
 storingMode := false;
 memo1.Lines.Add('Now drawing Polygon');
 with paintbox1.canvas do begin
  Brush.Color := clBlack;
  Polygon(Slice(PolygonArray,PointNumber));
 end;
end;

procedure TForm1.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
 {Store X,Y value in grid}
 if StoringMode then begin
  if PointNumber < MaxPoints then begin
   PolygonArray[PointNumber].X := X;
   PolygonArray[PointNumber].Y := Y;
   Inc(PointNumber);
   {show point}
   PaintBox1.Canvas.Pixels[X,Y] := clRed;
   {put point in memo}
   memo1.Lines.Add('Pointnr. : '+IntToStr(PointNumber)+
                   ' at position [X:'+IntToStr(X)+', Y:'+IntToStr(Y)+']');
  end
   else showmessage('sorry polygon points are limited to: '+IntToStr(MaxPoints));
  end
   else showmessage('please press StorePoly button to start defining the polygon');
end;

end.

Regards, Zif.
0
 

Author Comment

by:ItsMe
ID: 1351762
Adjusted points to 30
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

762 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

21 Experts available now in Live!

Get 1:1 Help Now