0

我到处都在寻找面板的双重缓冲,但它都使用了对我来说太混乱的术语。我正在绘制生物节律,并希望有一个移动功能来缓慢地向前移动生物节律。但是,它闪烁很多,所以我需要弄清楚如何阻止它。我试图穿线它,但无济于事,因为穿线仍然会导致闪烁。

以下内容进入计时器:您可以跳到底部查看它在哪里绘制图形。基本上,它会刷新、计算和绘制点。

  Panel p = panel1;
        p.Refresh();
        double physical = Math.Sin(2 * Math.PI * t / 23) * 100;
        double emotional = Math.Sin(2 * Math.PI * t / 28) * 100;
        double intellectual = Math.Sin(2 * Math.PI * t / 33) * 100;
        double physicalint = int.Parse(Math.Round(physical).ToString());
        double emotionalint = int.Parse(Math.Round(emotional).ToString());
        double intellectualint = int.Parse(Math.Round(intellectual).ToString());

        Console.WriteLine(physical + " Physical, " + emotional + " Emotional, " + intellectual + " Intellectual.");
        Console.WriteLine(physicalint + " Physical, " + emotionalint + " Emotional, " + intellectualint + " Intellectual.");

        int midpt = p.Width / 2;
        double Mastery = (physicalint + intellectualint) / 2;
        double Passion = (physicalint + emotionalint) / 2;
        double Wisdom = (emotionalint + intellectualint) / 2;
        int mastery = int.Parse(Math.Round(Mastery).ToString());
         int passion = int.Parse(Math.Round(Passion).ToString());
         int wisdom = int.Parse(Math.Round(Wisdom).ToString());
        Results.Text = "Primary Biorhythms\nPhysical: " + physicalint.ToString() + "\nEmotional: " + emotionalint.ToString() + "\nIntellectual: " + intellectualint.ToString();
        Results.Text += "\nSecondary Biorhythms\nWisdom: " + wisdom.ToString() + "\nPassion: " + passion.ToString() + "\nMastery: " + mastery.ToString();


        int eanum = int.Parse(Spread.Text);

        if (mode == 0)
        {


            for (int i = -eanum; i <= eanum; i++)
            {
                double actualheightphy = p.Height / 2;
                double physical2 = Math.Sin(2 * Math.PI * (t + i) / 23) * 100;
                double emotional2 = Math.Sin(2 * Math.PI * (t + i) / 28) * 100;
                double intellectual2 = Math.Sin(2 * Math.PI * (t + i) / 33) * 100;
                if (physical2 < 0) { physical2 = physical2 * 1.45; }
                if (physical2 > 0) { physical2 = physical2 * 1.5; }
                if (emotional2 < 0) { emotional2 = emotional2 * 1.45; }
                if (emotional2 > 0) { emotional2 = emotional2 * 1.5; }
                if (intellectual2 < 0) { intellectual2 = intellectual2 * 1.45; }
                if (intellectual2 > 0) { intellectual2 = intellectual2 * 1.5; }
                actualheightphy -= physical2;
                double actualheightemo = p.Height / 2;
                double actualheightint = p.Height / 2;
                actualheightemo -= emotional2;
                actualheightint -= intellectual2;
                int distancebetween = p.Width / eanum;

                p.CreateGraphics().FillEllipse(Brushes.Red, i * distancebetween + midpt - 5, int.Parse(Math.Round(actualheightphy).ToString()) - 5, 10, 10);
                p.CreateGraphics().FillEllipse(Brushes.Blue, i * distancebetween + midpt - 5, int.Parse(Math.Round(actualheightemo).ToString()) - 5, 10, 10);
                p.CreateGraphics().FillEllipse(Brushes.Green, i * distancebetween + midpt - 5, int.Parse(Math.Round(actualheightint).ToString()) - 5, 10, 10);
                if (i % 7 == 0)
                {
                    p.CreateGraphics().DrawString(i.ToString(), new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular), Brushes.White, new RectangleF(i * distancebetween + midpt - 5, p.Height - 12, 30, 30));
                }
                if (eanum == 7)
                {
                    p.CreateGraphics().DrawString(i.ToString(), new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular), Brushes.White, new RectangleF(i * distancebetween + midpt - 5, p.Height - 12, 30, 30));

                }
            }
            p.CreateGraphics().DrawLine(Pens.White, 0, (p.Height) / 2, p.Width, (p.Height) / 2);
            p.CreateGraphics().DrawLine(Pens.White, p.Width / 2, 0, p.Width / 2, p.Height);

            this.CreateGraphics().DrawString("100", new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular), Brushes.Black, new RectangleF(p.Width / 2 + p.Left - 15, -1, 100, 100));
            this.CreateGraphics().DrawString("-100", new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular), Brushes.Black, new RectangleF(p.Width / 2 + p.Left - 15, p.Height + p.Top + 1, 100, 100));
        }
4

1 回答 1

0

一个可能的原因可能是,根据该CreateGraphics方法的性质,您每次绘制某些东西时都会创建一个新的 Graphics 对象。抽到哪一个就靠运气了,所以我想它一定会显着闪烁。

因此,获取 p.CreateGraphics() 的返回值并将重用于所有绘图,而不是多次调用它;例如:

p.CreateGraphics().FillEllipse(...);
p.CreateGraphics().FillEllipse(...);
p.CreateGraphics().FillEllipse(...);

应该:

Graphics graphics=p.CreateGraphics();
...
graphics.FillEllipse(...);
graphics.FillEllipse(...);
graphics.FillEllipse(...);
于 2013-10-12T23:48:36.333 回答