Link to home
Start Free TrialLog in
Avatar of borgo
borgo

asked on

A program to draw graphs

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 ?
Avatar of kretzschmar
kretzschmar
Flag of Germany image

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
Avatar of borgo
borgo

ASKER

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

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

what is your goal

meikl
Avatar of borgo

ASKER

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.

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=-..
Avatar of borgo

ASKER

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.

ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of borgo

ASKER


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

My best greetings from Italy.