Hi there
I want to use c++ to draw a path
The path is based on two variables, x and y
and x and y will change automatically every second

So I think the flow should be like this:
t=0, (x,y)=(0,0) ->draw a point on the graph
t=1, (x,y)=(2,2)->draw another point on the graph
....
repeat the above steps until the user stop the program.

But I have no idea how can I ask the program to draw point every second and keep all the points in the graph? I tried to use a timer

private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) 	 
		 { 		 ShowGraphics=true;	 }

so when ShowGraphics is true,

if (IsShowGraphics==false)
		return;
	
	Color Black=Color::FromArgb(250, Color::Black);
	
	SolidBrush^ Brush_A = gcnew SolidBrush( Black );

	if (x!=0 && y!=0)
	{		CallerGDI->FillRectangle(Brush_A,x,y,10,10); //draw point
		IsShowGraphics=false;
	}

But the program only draws one point at each time AND replaces the previous one. How can I keep the "old" points in the graph?

Thanks for yr time, I really appreciate your help~

Recommended Answers

All 2 Replies

This is a frustrating thing to deal with. I looked at it as such:
Make a class that holds your data points in a list and has a member method Draw, drawing each point up until time.

void Points::Draw(Graphics^ g , int time)
	{				
			Pen ^ mypen = gcnew Pen(Color::Black);
		for(int i = 0;i<time;i++)
			g->DrawRectangle(mypen,listpts[i].X,listpts[i].Y,1,1);
	}

In form1.h have 2 event handlers

private: System::Void Form1_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e) {
				 pts->Draw(e->Graphics,time);
				 time++;
				 
				 }
	private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {
				 Invalidate();
				 }

Use an interval on the timer of 1000 and start it in the Form1 constructor. Each time the timer ticks (1 sec) it will paint all of the dots up until that time. I'm sure there's a better way to do it but there may be no escaping the need to repaint all of them on each new dot.

thank you jonsca, it works^^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.