在面板上绘制线条轨迹。 [英] Draw Line trail on a Panel.

查看:67
本文介绍了在面板上绘制线条轨迹。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家晚上好,

我创建了一个面板,我绘制了一些通过Timer1更新的行,间隔为200 ms,并带有Invalidate()。关键是我想要创建一个跟踪线,或者通过更新坐标X,Y来保持创建的线。当Invalidate()重新绘制面板时,只需使用新值更新Line,并且删除其他人。怎么解决这个?我已经阅读了很多,但没有什么可以解决这个问题。



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


私有 void Panel1_Paint(< span class =code-keyword> object sender,PaintEventArgs e)
{
Graphics gi = e.Graphics;
gi.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

angle = Valor3; // 来自微控制器通过串口

Pen pen2 = new Pen(Color.Red,2f);


int r = Uval; // 来自微控制器通过串口
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));

点pEnd = new 点(X2,Y2);
gi.DrawLine(pen2,pStart,pEnd);

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

}





感谢大家。



Rgds



Jose

解决方案

所有问候,



我找到了以下解决方案:



创建一个点列表:



< pre lang =c#> List< point> pointStart = new List< point>();
列表<点> pointEnd = new 列表< point>();





获取新的指向并将其添加到列表中:



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





然后用于表示:



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





也许不是最好的解决方案,但它有效...



我正在使用另一个解决方案,因为这个速度很快但存储的点数有限...



我想感谢Sergey提供的帮助。



Rgds



Jose


首先,你不可能发明比使用计时器更糟糕的事情。如何,请告诉我,计时器事件与您的绘图有关?你的绘图动作已经生成事件,鼠标事件;这就是你所需要的。如果您以编程方式更改图形数据,那么您已经有时间更改它。



基本方法是:您需要拥有模型你的图形存储在内存中。重写的虚方法 System.Windows.Control.OnPaint 或事件的事件处理程序 System.Windows.Control.OnPaint 应该用于在屏幕上渲染模型(或者,实际上,任何其他设备,例如 System.Drawing.Graphics 的位图或打印页面实例。每次 WM_PAINT 生成Windows消息,您的图形渲染。对于动画,您需要在图形模型更改时强制执行此过程或重新渲染。这就是无效确实。



另见我过去的答案:

在面板上捕获图纸 [ ^ ],

什么样的解放者油漆的方法是什么? (DataGridViewImageCell.Paint(...)) [ ^ ],

mdi子表单之间的绘制线 [ ^ ],

如何加速我的vb.net应用程序? [ ^ ]。







你不可能用计时器减少闪烁。而是使用双缓冲:

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.doublebuffered%28v=vs.110%29.aspx [ ^ ]。



-SA


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.

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

解决方案

Greetings all,

I found the following solution:

Create a point List:

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



Get a new point and add it to the list:

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



Then with for cicle:

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


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


这篇关于在面板上绘制线条轨迹。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆