C#中如何使用 GDI+随机生成若干个圆形?

2015-10-27 20:10:52 +08:00
 ElegantOfKing

本人最近才接触 C#编程,老师布置作业:使用 GDI+画圆,半径和颜色都要随机变化,圆要生成若干个。我参考了网上的许多资料,编写了如下代码,但是点击生成按钮,每次仍然只能生成一个圆形。各位会员能够帮我查找一下错误,或者提醒一下?万分感谢!

private void button1_Click(object sender, EventArgs e)
        {
            Graphics p = this.CreateGraphics();
            PaintEventArgs pe = new PaintEventArgs(p, this.ClientRectangle);
            panel1_Paint(this, pe);
            p.Dispose();
        }
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            var list = new List<Rectangle>();
            var rand = new Random();
            var g = e.Graphics;
             int N = 100; //圆的个数
            for (int i = 0; i < 1000; i++) //限定最多试探次数,以免出现死循环
            {
              Random ran = new Random();
            int wid = Screen.PrimaryScreen.WorkingArea.Width;
            int hei = Screen.PrimaryScreen.WorkingArea.Height;
            int R1 = ran.Next(0,1000);
            int R2 = ran.Next(0,1000);
            int Rnadkey = ran.Next(20, 800);
            int Cran1 = ran.Next(0, 255);
            int Cran2 = ran.Next(0, 255);
            int Cran3 = ran.Next(0, 255);
            Brush brush = new SolidBrush(Color.FromArgb(Cran1, Cran2, Cran3));//三原色
            var rct = new Rectangle(R1, R2, Rnadkey, Rnadkey);
            if ((rct.X + 2 * rct.Width) < wid && (rct.Y + 2 * rct.Width) < hei) //判定是否超出边界
            {               
                list.Add(rct);
                g.FillEllipse(brush, rct);
                continue;
            }
            if (--N == 0) break;
            }
            }
1092 次点击
所在节点    C
7 条回复
stupidcat
2015-10-27 20:31:20 +08:00

Graphics p;
定义为成员变量,将
p = this.CreateGraphics();
放在这个 Form 的构造函数里
stupidcat
2015-10-27 20:42:50 +08:00
务必无视上面这条。。
abelyao
2015-10-27 20:47:52 +08:00
1. 圆形可否叠加?
2. 圆形可否超出画布范围?
如果都不要求… 那不就是随便画吗…
stupidcat
2015-10-27 20:54:28 +08:00
private void button1_Click(object sender, EventArgs e)
{
Graphics p = this.CreateGraphics();
PaintEventArgs pe = new PaintEventArgs(p, this.ClientRectangle);
panel1_Paint(this, pe);
p.Dispose();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
var g = e.Graphics;
var ran = new Random();

int N = 10; //圆的个数

while(true)
{
int R1 = ran.Next(0, this.Width);
int R2 = ran.Next(0, this.Height);
int Randkey = ran.Next(10, 200);

if (R1 + Randkey < this.Width && R2 + Randkey < this.Height) //判定是否超出边界
{
var rct = new Rectangle(R1, R2, Randkey, Randkey);
int Cran1 = ran.Next(0, 255);
int Cran2 = ran.Next(0, 255);
int Cran3 = ran.Next(0, 255);

Brush brush = new SolidBrush(Color.FromArgb(Cran1, Cran2, Cran3));//三原色
Pen pen = new Pen(brush);
g.DrawEllipse(pen, rct);

if (--N == 0)
{
break;
}
}
}
}
xbb7766
2015-10-27 21:53:00 +08:00
4 楼正解
ElegantOfKing
2015-10-28 14:25:12 +08:00
@abelyao 是的,随便画......
ElegantOfKing
2015-10-28 14:51:53 +08:00
@stupidcat 万分感谢!有了您的代码,加上昨天自己的修改,已经完全符合老师的要求了。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/231545

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX