随机绘制200条线 [英] Draw 200 lines with random width

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

问题描述

如何创建200条随机宽度的线?
我的意思是如下图所示:
http://www.4shared.com/photo/wkRfD4B-/Form.html

how to create 200 lines with random width?
I mean something like below picture:
http://www.4shared.com/photo/wkRfD4B-/Form.html

推荐答案

像这样使用Paint事件
use the Paint event like this
private void form_Paint(object sender, PaintEventArgs e)
{
    Random RandomClass = new Random();
    int height = 1;
    
    for (int i = 0; i < 200; i++)
    {
        height += 2;
        int RandomNumber = RandomClass.Next(11, 111);
        System.Drawing.Graphics my_graph = e.Graphics;
        Point my_point_one = new System.Drawing.Point(10, 2*(i+1));
        Point my_point_two = new System.Drawing.Point(RandomNumber, 2 * (i + 1));
        my_graph.DrawLine(System.Drawing.Pens.Blue, my_point_one, my_point_two);                
    }
}


1)您创建了一个小的Windows窗体应用程序
2)创建一个简单的随机数生成器方法
3)您循环播放200次
a)从随机数生成器中获取一个值
b)使用随机数生成器的值作为长度画一条线.
4)完成.

尝试一些代码.如果您当时遇到问题,请寻求与代码相关的更具体的内容.我实际上想看看我能以多快的速度将表单拍打成匹配您提供的图像链接中的表单,并且花费了不到1/2小时的时间,因此不需要太多代码.

干杯.
1) You create a small windows form application
2) You create a simple random number generator method
3) You loop 200 times
a) Retrieve a value from the random number generator
b) Draw a line using the value from the random number generator as the length.
4) You''re done.

Try some code. If you have issues at that point ask for something more specific related to the code. I actually wanted to see how quickly I could slap a form together to match that in the image link you provided and it took under 1/2 hour so there isn''t much code required.

Cheers.


尝试将其添加到您的班级.正如SAKryukov所说,最好重写OnPaint事件.

Try adding this to your class. As SAKryukov said it would be better to override the OnPaint event.

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    Random RandomClass = new Random();
    int height = 1;
    for (int i = 0; i < 200; i++)
    {
        height += 2;
        int RandomNumber = RandomClass.Next(11, 111);
        System.Drawing.Graphics my_graph = e.Graphics;
        Point my_point_one = new System.Drawing.Point(10, 2 * (i + 1));
        Point my_point_two = new System.Drawing.Point(RandomNumber, 2 * (i + 1));
        my_graph.DrawLine(System.Drawing.Pens.Blue, my_point_one, my_point_two);
    }
}


这篇关于随机绘制200条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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