动画不工作,因为 picbox 不重置且任务不休眠
Animation isn't working because the picbox doesn't reset and the task doesn't sleep
我这里有这段代码:
编辑:源代码:
private void btnAnimate_Click(object sender, EventArgs e)
{
Bitmap[] circle = new Bitmap[300];
for (int i = 0; i < circle.Length; i++)
{
circle[i] = new Bitmap(260, 266);
}
double r = 25; // radius
double rr = Math.Pow(r, 2); // r^2
int h = 25;
// x value of centre of circle is represented with h and y value is represented with k
for (int k = 25; k <= 100; k += 25) // y value of center moves down 1 pixel every iteration
{
for (int x = -h; x <= h + r; x++)
{
for (int y = -k; y <= k + r; y++)
{
if (Math.Abs(Math.Pow(x - h, 2) + Math.Pow(y - k, 2)) <= rr)
// if: |(x-h)^2 + (y - h)^2| <= r^2, then draw the pixel
{
circle[k - 25].SetPixel(x, y, Color.Red);
}
}
}
}
for (int l = 25; l <= 100; l+= 25)
{
picBox.Image = circle[l - 25];
btnAnimate.Text = "circle" + (l/25);
System.Threading.Thread.Sleep(1000);
}
}
(原来是k++,后来我改成了k+=25,这样更容易排错,它只会画4个圆圈,而不是原来的75个。)
我想让它在图片框上画一个圆圈(我测试过它可以做到),但之后我想让它休眠一会儿,然后清除图片框,画一个新的仅在其下方 1 个像素处圈出并重复,直到圆圈触及地面。这个问题是程序正在休眠,但它没有显示在两次休眠之间完成的操作。即它说显示圆圈 1 然后休眠直到它到达圆圈 4,但它不会显示圆圈 1 到 3 虽然它会休眠,它只会显示最后一个圆圈。不只是图片框
你可以看到我写的
btnAnimate.Text = "circle" + (l/25);
System.Threading.Thread.Sleep(1000);
我希望它将按钮文本更改为 1、2、3 和 4 以计算每次迭代,但它也没有这样做。它只会在最后显示 4。
您认为问题出在哪里?
解决方案:
double r = 25; // radius
double rr = Math.Pow(r, 2); // r^2
int h = 25;
// x value of centre of circle is represented with h and y value is represented with k
for (int k = 25; k <= 100; k += 25) // y value of center moves down 1 pixel every iteration
{
Bitmap circle = new Bitmap(260, 266);
for (int x = -h; x <= h + r; x++)
{
for (int y = -k; y <= k + r; y++)
{
if (Math.Abs(Math.Pow(x - h, 2) + Math.Pow(y - k, 2)) <= rr)
// if: |(x-h)^2 + (y - h)^2| <= r^2, then draw the pixel
{
circle.SetPixel(x, y, Color.Red);
}
}
}
picBox.Image = circle;
picBox.Update();
System.Threading.Thread.Sleep(100);
}
你有两个问题:
您没有在每次迭代中清除 Bitmap circle
绘制的圆圈,因此您是在之前绘制的圆圈的基础上构建,所以它看起来像一个带有轨迹的下落圆圈。您可以通过在第一个 for 循环内部而不是外部定义 Bitmap circle
来解决此问题。
无法在线程休眠之间显示新图像,因为您没有告诉控件以视觉方式刷新自身。通过在设置 picBox.Image = circle
.
后调用 picBox.Update()
或 picBox.Refresh()
来执行此操作
我这里有这段代码: 编辑:源代码:
private void btnAnimate_Click(object sender, EventArgs e)
{
Bitmap[] circle = new Bitmap[300];
for (int i = 0; i < circle.Length; i++)
{
circle[i] = new Bitmap(260, 266);
}
double r = 25; // radius
double rr = Math.Pow(r, 2); // r^2
int h = 25;
// x value of centre of circle is represented with h and y value is represented with k
for (int k = 25; k <= 100; k += 25) // y value of center moves down 1 pixel every iteration
{
for (int x = -h; x <= h + r; x++)
{
for (int y = -k; y <= k + r; y++)
{
if (Math.Abs(Math.Pow(x - h, 2) + Math.Pow(y - k, 2)) <= rr)
// if: |(x-h)^2 + (y - h)^2| <= r^2, then draw the pixel
{
circle[k - 25].SetPixel(x, y, Color.Red);
}
}
}
}
for (int l = 25; l <= 100; l+= 25)
{
picBox.Image = circle[l - 25];
btnAnimate.Text = "circle" + (l/25);
System.Threading.Thread.Sleep(1000);
}
}
(原来是k++,后来我改成了k+=25,这样更容易排错,它只会画4个圆圈,而不是原来的75个。)
我想让它在图片框上画一个圆圈(我测试过它可以做到),但之后我想让它休眠一会儿,然后清除图片框,画一个新的仅在其下方 1 个像素处圈出并重复,直到圆圈触及地面。这个问题是程序正在休眠,但它没有显示在两次休眠之间完成的操作。即它说显示圆圈 1 然后休眠直到它到达圆圈 4,但它不会显示圆圈 1 到 3 虽然它会休眠,它只会显示最后一个圆圈。不只是图片框
你可以看到我写的
btnAnimate.Text = "circle" + (l/25);
System.Threading.Thread.Sleep(1000);
我希望它将按钮文本更改为 1、2、3 和 4 以计算每次迭代,但它也没有这样做。它只会在最后显示 4。
您认为问题出在哪里?
解决方案:
double r = 25; // radius
double rr = Math.Pow(r, 2); // r^2
int h = 25;
// x value of centre of circle is represented with h and y value is represented with k
for (int k = 25; k <= 100; k += 25) // y value of center moves down 1 pixel every iteration
{
Bitmap circle = new Bitmap(260, 266);
for (int x = -h; x <= h + r; x++)
{
for (int y = -k; y <= k + r; y++)
{
if (Math.Abs(Math.Pow(x - h, 2) + Math.Pow(y - k, 2)) <= rr)
// if: |(x-h)^2 + (y - h)^2| <= r^2, then draw the pixel
{
circle.SetPixel(x, y, Color.Red);
}
}
}
picBox.Image = circle;
picBox.Update();
System.Threading.Thread.Sleep(100);
}
你有两个问题:
您没有在每次迭代中清除
Bitmap circle
绘制的圆圈,因此您是在之前绘制的圆圈的基础上构建,所以它看起来像一个带有轨迹的下落圆圈。您可以通过在第一个 for 循环内部而不是外部定义Bitmap circle
来解决此问题。无法在线程休眠之间显示新图像,因为您没有告诉控件以视觉方式刷新自身。通过在设置
后调用picBox.Image = circle
.picBox.Update()
或picBox.Refresh()
来执行此操作