Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# A program to draw graphs

Posted on 1999-07-24
Medium Priority
427 Views
Hi Expert
I'd like make a program to draw graphs.
Please can you give me an example (some code) on how to draw a little circle (the node of the graph) and move it with the mouse ?
0
Question by:borgo
[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
• 4
• 3
• 2

LVL 27

Expert Comment

ID: 1390838
hi borgo,

use a tshape, property shape = stCircle

and use this code for moving

var xx,yy : Integer;

procedure TForm1.Shape1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Button = mbLeft then
begin
xx := x;
yy := y;
end;
end;

procedure TForm1.Shape1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if ssLeft in shift then
begin
shape1.Top := shape1.Top + y-yy;
shape1.left := shape1.left + x-xx;
end;
end;

meikl
0

LVL 10

Expert Comment

ID: 1390839
:)
0

Author Comment

ID: 1390840
Hi kretzschmar,
you're right but I'd like to use a Canvas, becuase I think it's more flexible, isn't it ? What do you think about ?
I have just draw a circle on the Form Canvas.
Now I'd like to code the OnClick event, but I don't know how to do it ? Any ideas ?
Thank you

0

LVL 27

Expert Comment

ID: 1390841
hi borgo,

>What do you think about ?
nothing, because to complex,
you need much code to control your circle on the canvas,
you must allways know, where your circle resists on the canvas,
and calculate the mousecoords,
then you must clear the canvas or a part of the canvas,
calculate the new position and paint new.
well, with one circle it could be manageable

i would recommend to use shapes,
easy to assign events,
easy to control the position,
provides also a canvas

but this depends on what you will do

meikl
0

Author Comment

ID: 1390842
Hi kretzschmar
I'm just trying to code some programs to learn some basic delphi features (I'm a student). So I think I'll try both the solutions: canvas and shape.
You said "shapes...provides also a canvas", what does it mean ?
I think you have helped me a lots and you can get my points.
I have a last question.
How Can I calculate the mouse position inside a canvas and associate an event to a left click on it. (ex: I click on the canvas and I get the x,y position)
( Raised point to 115 )
Thank you very much.

Andrea.

0

LVL 10

Expert Comment

ID: 1390843
Well, the circle actually has been drawn on a canvas.... but because it is a COMPONENT, you can move it around, and it will draw itself on a new place.... but if you were to draw a circle on the form's canvas for example, there is no way to find out that when you click on the circle it is a component or not... TShape should do what you want.. at least the basics as you want..

..-=ViKtOr=-..
0

Author Comment

ID: 1390844
Hi ViKtOr,
If I know where the circles are on the canvas (2 dimensional array with its coordinates) then I can try to handle the onclick event on the canvas.
But I don't know how to find the mouse position and how to handle the onclick event on the canvas. :-(
Bye and thank you.

0

LVL 27

Accepted Solution

kretzschmar earned 440 total points
ID: 1390845
hi borgo,

a sample to move a on the canvas drawn circle

unit move_circle_u;

interface

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

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
Procedure DrawCircle(P : TPoint);
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;

var
Form1: TForm1;

implementation

{\$R *.DFM}

var
InDrag : Boolean = False;
MP : TPoint;
S : Integer;
R : TRect;

Procedure TForm1.DrawCircle(P : TPoint);
begin
R := Rect(P.x,p.y,p.x+s,p.y+s);
Canvas.Ellipse(r.left,r.top,r.right,r.bottom);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
S := 20;
canvas.Brush.Color := clBlue;
MP := Point(10,10);
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
DrawCircle(MP);
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if (Button = mbLeft) and
(X > R.Left) and (X < R.Right) and
(Y > R.Top) and (Y < R.Bottom) then
InDrag := True;
end;

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if (ssleft in shift) and InDrag then
begin
MP := Point(X,Y);
Repaint;
end;
end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
InDrag := False;
end;

end.

have fun

meikl
0

Author Comment

ID: 1390846

T H A N K Y O U A L O T S :-)

My best greetings from Italy.

0

## Featured Post

Question has a verified solution.

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

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 Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGridâ€¦
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastrâ€¦
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Prâ€¦
###### Suggested Courses
Course of the Month7 days, 1 hour left to enroll