Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
2.64/5 (4 votes)
See more:
Good evening everyone,
I created a panel where I drew some lines that update via a Timer1 with an interval of 200 ms, and with a Invalidate (). The point is that I wanted to create a trail lines, or to keep the lines that are created, ranging through updating of coordinates X, Y. As the Invalidate () does repaint the panel, just updates the Line with the new value, and delete the others. How to solve this? I've read a lot, but nothing can solve this problem.

C#
private void RefreshTimer_Tick(object sender, EventArgs e)
        {
            RefreshTimer.Interval = 200;
            Panel1.Invalidate();
        }


private void Panel1_Paint(object sender, PaintEventArgs e)
      {
                Graphics gi = e.Graphics;
                gi.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                angle = Valor3;//Comes from a microcontroller via serial port
                
                Pen pen2 = new Pen(Color.Red, 2f);
                
               
                int r = Uval;//Comes from a microncontroller via serial port
                int X1 = Panel1.Size.Width/2;
                int Y1 = Panel1.Size.Height/2;
                Point pStart = new Point(X1, Y1);

                int X2 = (int)(X1 + r * Math.Cos((double)angle * Math.PI/180));
                int Y2 = (int)(Y1 + r * Math.Sin((double)angle * Math.PI/180));
               
                Point pEnd = new Point(X2, Y2);
                gi.DrawLine(pen2, pStart, pEnd);

                gi.DrawEllipse(new Pen(Color.Green, 2f), 0, 0, Panel1.Size.Width,      Panel1.Size.Height);

       }


Thank for all.

Rgds

Jose
Posted
Updated 19-Jan-14 15:38pm
v2
Comments
BillWoodruff 19-Jan-14 21:01pm    
Is it the case that the lines once drawn are fixed, or is the case that you need to clear them from time to time and draw new ones using different co-ordinates; I'm not totally clear reading your post what your intent is.
Member 10369986 19-Jan-14 21:09pm    
Greetings BillWoodruff

Thank you for your kind reply. Need to update every +/-200ms with new co-ordinates.and would like to keep the other lines already created, for a few seconds at least, and with this created a Line trail. I hope I am clear now.

Rgds

Jose

Regards

Jose
BillWoodruff 19-Jan-14 21:11pm    
So, "trail" here means a kind of animation effect ... the previous line persist for an interval ?
Member 10369986 19-Jan-14 21:28pm    
Exactly. Need to maintain the old Lines.
Sergey Alexandrovich Kryukov 20-Jan-14 0:48am    
You need to maintain the lines, so do exactly this. Please see my answer.
—SA

Greetings all,

I found the following solution:

Create a point List:

C#
List<point> pointStart = new List<point>();
List<point> pointEnd = new List<point>();


Get a new point and add it to the list:

C#
pointStart.Add(pStart);
pointEnd.Add(pEnd);


Then with for cicle:

C#
for (int x = 0; x < pointEnd.Count; x++)
 {
 g.DrawLine(pen2, pointStart[x], pointEnd[x]);
}


Maybe not the best solution, but it works...

I am working in another solution since this is fast but limited in points stored ...

I wish to acknowledge the assistance provided by Sergey.

Rgds

Jose
 
Share this answer
 
v2
First of all, you could not possibly invent something worse than using a timer. How, tell me please, timer events are related to the drawing you make? You drawing actions already generate events, mouse events; and this is all you need. If you change the graphical data programmatically, then again, you already have moment of time where you change it.

The basic approach is this: you need to have the model of your graphics stored in memory. The overridden virtual method System.Windows.Control.OnPaint or the event handler of the event System.Windows.Control.OnPaint should be used to render the model on screen (or, actually, any other device, such as a bitmap or print page instance of System.Drawing.Graphics. Each time WM_PAINT Windows message is generated, your graphics rendering. For animation, you need to force this process or re-rendering as soon as your graphic model changes. This is what Invalidate does.

See also my past answers:
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
Drawing Lines between mdi child forms[^],
How to speed up my vb.net application?[^].

[EDIT]

You cannot possibly reduce flicker with the timer. Instead use double-buffering:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.doublebuffered%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
v2
Comments
Member 10369986 20-Jan-14 11:06am    
Greetings Sergey

Thank you for your help. The Timer idea was to reduce flicker and then stay like that.
Anyway, I read what you have posted, and it would be great if you could post an idea of the code for this, becoming this way clearer, and pedagogical. I have and I got an idea how to do, but is a little tricky to implement.

Rgds

Jose
Sergey Alexandrovich Kryukov 20-Jan-14 11:14am    
As I say, this is a bad idea. Please see my update to the answer.

As to the "way clearer, and pedagogical", you might need to wait for my big article, but don't hold your breath: it waits in a line of few more unpublished articles...
—SA
Member 10369986 20-Jan-14 12:12pm    
Greetings Sergey

regarding your update, I sort out my flicker problem with:

protected override CreateParams CreateParams
{
get
{
CreateParams handleParam = base.CreateParams;
handleParam.ExStyle |= 0x02000000; // WS_EX_COMPOSITED
// handleParam.ExStyle |= 0x04000000L; //
return handleParam;
}
}

But my problem is now how to create a trail effect on my Drawline (x1,y1,x2,y2) in a Panel.

Rgds

Jose
Sergey Alexandrovich Kryukov 20-Jan-14 13:09pm    
Good; as to the second question, the main part of my answer explains that. Is anything unclear? What part of it?
—SA
Member 10369986 20-Jan-14 13:47pm    
Greetings

From a concept to the practical side of the model, goes a certain distance. You explained (and I appreciate), but the problem is that without a code example ("Hello World"), is complicated. I understand that you have no time, but only with what you explained I still cannot implement. However, I’m trying other ways to resolve this issue. I've seen and read many interesting things, tried customizing for my code, but does not work!
See this -http://stackoverflow.com/questions/3714146/how-to-draw-a-single-line-using-mousemove-event -
(I already eliminated the Timer).

Rgds

Jose

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900