【发布时间】:2014-08-30 00:07:17
【问题描述】:
所以我正在尝试编写一个程序,该程序通过每 1 度绘制一条线来绘制一个圆,并通过 RGB 递增(由随机开始)来改变颜色。
这是我目前的代码
public static void drawCircle(int iRandR, int iRandG, int iRandB)
{
for (int x = 0; x < 359; ++x)
{
double dXAngle = 0;
double dYAngle = 0;
dXAngle = 300 + Math.Cos(x) + 200;
dYAngle = 300 + Math.Sin(x) + 200;
Pen pPen = new Pen(Color.FromArgb(255, iRandR, iRandG, iRandB));
Graphics gDraw;
int iXAngle = Convert.ToInt32(dXAngle);
int iYAngle = Convert.ToInt32(dYAngle);
gDraw.DrawLine(pPen, 300, 300, iXAngle, iYAngle); //Error called here
}
}
private void drawCircleButton_Click(object sender, EventArgs e)
{
int genRandR = 0;
int genRandG = 0;
int genRandB = 0;
Random rRand = new Random();
genRandR = rRand.Next(0, 255);
genRandG = rRand.Next(0, 255);
genRandB = rRand.Next(0, 255);
drawCircle(genRandR, genRandG, genRandB);
drawCircleButton.Hide();
}
唯一的问题是编译器对上述行有问题,它抛出“使用未分配的局部变量'gDraw'”我做了一些谷歌搜索,很多其他例子看起来像我的,但我不知道为什么我的会抛出这个错误。
任何帮助将不胜感激。
【问题讨论】:
-
gDraw(具体来说是Graphics)是一个引用类型。它没有默认值。因此,需要初始化(可能使用new关键字?) -
你在画什么? WinForms 控件?
Bitmap?
标签: c# graphics compiler-errors drawing