首页 > 解决方案 > WinForms中的等离子效果,如何创建连续循环

问题描述

我正在尝试使用 C# 和 Winforms 创建一些简单的图形(“等离子”效果)。我的代码中有两个类,(main)Form1 和 Plasmadraw。

在 Plasmadraw 我有以下设置:

class Plasmadraw
{

    int y;
    int x;
    double i;
   
    double pii = 3.1415;

    public void Draw(Graphics gfx, int addition)
    {

        
        for (int i = 0; i < 23040; i++)
        {

           
            x = x + 10;

            if (x == 1920)
            {
                x = 0;
                y = y + 10;
            }

            if (y == 1200)
            {
                y = 0;
            }

            double v = Math.Sin((x * 0.5) + i * 0.001 * addition);
            double c = v * pii;
            double d = c + (2 * pii / 3);
            double f = c + (6 * pii / 3);
            double r = 255 * Math.Abs(Math.Sin(c));
            double g = 255 * Math.Abs(Math.Sin(d));
            double b = 255 * Math.Abs(Math.Sin(f));

            int r1 = (int)r;
            int g1 = (int)g;
            int b1 = (int)b;

            Color e = Color.FromArgb(r1, g1, b1);

            SolidBrush brush = new SolidBrush(e);
            Rectangle rect = new Rectangle(x, y, 10, 10);
            gfx.FillRectangle(brush, rect);

        }
        

    }
}

然后在 Form1 我有 Paint 事件:

 private void Form1_Paint(object sender, PaintEventArgs e)
 {
     Plasmadraw plasmaeffect = new Plasmadraw();

     for (int a = 0; a < 30; a++)
     {
          
         plasmaeffect.Draw(e.Graphics, a);

         Invalidate();
     }

这是构建逻辑的某种完全错误的方法吗?我设法通过创建一个列表然后在 Paint 事件中使用 Foreach (& Invalidate()) 运行该列表来创建一些图形效果(移动精灵等)。但是,我想学习一些做事的新方法,而不是照搬旧方法。

标签: c#winformsgraphics

解决方案


推荐阅读