通过排序重画线 [英] redraw lines by sorting

查看:54
本文介绍了通过排序重画线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写下面的代码:

I write below code:

private void button1_Click(object sender, EventArgs e)
        {
            Random RandomClass = new Random();
            int height = 1;
            
            for (int i = 0; i < 200; i++)
            {
                height += 2;
                int RandomNumber = RandomClass.Next(16, 215);
                System.Drawing.Graphics my_graph = this.CreateGraphics();
                System.Drawing.Graphics my_graph2 = this.CreateGraphics();
                System.Drawing.Graphics my_graph3 = this.CreateGraphics();
                Point my_point_s1 = new System.Drawing.Point(15, height);
                Point my_point_f1 = new System.Drawing.Point(RandomNumber, height);
                arrayToSort[i] = RandomNumber - 15;
                Point my_point_s2 = new System.Drawing.Point(245, height);
                Point my_point_f2 = new System.Drawing.Point(RandomNumber+245, height);
                Point my_point_s3 = new System.Drawing.Point(500, height);
                Point my_point_f3 = new System.Drawing.Point(RandomNumber+500, height);
                my_graph.DrawLine(System.Drawing.Pens.Blue, my_point_s1, my_point_f1);
                my_graph2.DrawLine(System.Drawing.Pens.Purple, my_point_s2, my_point_f2);
                my_graph3.DrawLine(System.Drawing.Pens.Red, my_point_s3, my_point_f3);
                my_graph.Dispose();
            }
        }


现在我要对它进行排序.我的意思是重新绘制所有升序或降序的行.


now I want to sort it. I mean that redraw all the lines sorted ascending or descending.

推荐答案

原则上,您所做的是错误的. 您永远不应创建Graphics的实例.您只需要一个实例,该实例将始终出现在OnPaint方法的事件参数或Paint事件的处理程序中.

这是您应该执行的操作:
在面板上捕获图形 [
What you do is wrong in principle. You should never create an instance of Graphics. You need only one instance which you will always get in the event arguments of your OnPaint method or your handler of the Paint event.

This is what you should do:
capture the drawing on a panel[^].

—SA


几点:
1)您不需要创建三个图形上下文,实际上这样做是一个坏主意,因为图形上下文是稀缺的资源.创建一个,然后使用三次:
A couple of points:
1) You don''t need to create three graphics contexts, in fact it is a bad idea to do that as Graphics contexts are scarce resources. Create one, and use that three times:
int RandomNumber = RandomClass.Next(16, 215);
System.Drawing.Graphics my_graph = this.CreateGraphics();
Point my_point_s1 = new System.Drawing.Point(15, height);
Point my_point_f1 = new System.Drawing.Point(RandomNumber, height);
arrayToSort[i] = RandomNumber - 15;
Point my_point_s2 = new System.Drawing.Point(245, height);
Point my_point_f2 = new System.Drawing.Point(RandomNumber+245, height);
Point my_point_s3 = new System.Drawing.Point(500, height);
Point my_point_f3 = new System.Drawing.Point(RandomNumber+500, height);
my_graph.DrawLine(System.Drawing.Pens.Blue, my_point_s1, my_point_f1);
my_graph.DrawLine(System.Drawing.Pens.Purple, my_point_s2, my_point_f2);
my_graph.DrawLine(System.Drawing.Pens.Red, my_point_s3, my_point_f3);
my_graph.Dispose();

2)处理第一个问题可以解决第二个问题:您负责处理所创建的 all 图形对象:如果不这样做,将会遇到无法预料的问题以后...
3)不要在按钮单击事件中绘制它们:相反,请处理Paint事件并在那里绘制它们.只需使用Invalidate方法,它将强制重绘.这也摆脱了上面1和2中的两个问题,因为paint事件将您作为Graphics对象作为PaintEventArgs参数的一部分交给了您,因此您根本不必确定和处置它们中的任何一个.
如果您没有在Paint事件中绘制它们,则如果任何东西覆盖了窗口或将窗口最小化了,它们将不会被重新绘制.

2) Dealing with the first point solves the second: you are responsible for disposing of all Graphics object you create: If you do not, you will get unpredictable problems later...
3) Don''t draw these in a button click event: instead, handle the Paint event and draw them there. Just use the Invalidate method, and it will force a redraw. This also gets rid of both problems from 1 and 2 above, because the paint event hands you the Graphics object as part of the PaintEventArgs parameter, so you don''t have to cerate and dispose any of them at all.
If you don''t draw these in the Paint event, they will not be redrawn if anything covers the window, or if it is minimised.


首先将所有行添加到数组中,接下来对数组进行排序.
您可以使用my_graph.DrawLines(color,array)绘制(排序的)数组中的所有线条.
First add all lines to an array, next sort the array.
You can use my_graph.DrawLines(color, array) to draw all lines in the (sorted) array.


这篇关于通过排序重画线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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