用计时器c#在椭圆内绘制椭圆

时间:2013-06-13 12:23:31

标签: c# winforms timer

我在winforms中有一个带有工具条菜单的程序,其中包含按钮:+, - 。 每次我点击+它绘制一个不断增长的椭圆,当我舔它 - 它消失了。 现在,我设法绘制了不断增长的椭圆,但是当我再次点击+时,它会绘制新的椭圆。我需要新的椭圆从我画的前一个椭圆内部开始。 所以当我点击3次时,另一个内部会有3个椭圆形。 当我点击 - 我绘制的最后一个椭圆将消失。我有约束使用draImage()。 我的问题是:当我再次单击+时,如何在前一个内部创建一个新的椭圆? 当我点击+它只是擦除前一个并开始绘制新的椭圆。  我的代码看起来像这样:

Form.cs:

public partial class TheBalls : Form
    {
        private Ball yourBall;

        public TheBalls()
        {
            InitializeComponent();
            DoubleBuffered = true;
            yourBall = new Ball(this);
        }

        Graphics flagGraphics;
        List<Ball> list = new List<Ball> { };

        private void plusButton_MouseDown(object sender, MouseEventArgs e)
        {
            yourBall = new Ball(this);
            list.Add(yourBall);
            yourBall.Growing();
        }

        private void minusButton_MouseDown(object sender, MouseEventArgs e)
        {
            if (list.Count > 0)
            {
                list[list.Count - 1].clr = Color.Transparent;
                list.RemoveAt(list.Count -1);
            }
        }

        private void TheBalls_Paint(object sender, PaintEventArgs e)
        {

            Bitmap bitmap = new Bitmap(yourBall.Center.X * 2, yourBall.Center.Y * 2);
            flagGraphics = Graphics.FromImage(bitmap);
            yourBall.Update(flagGraphics);
            e.Graphics.DrawImage(bitmap, 0, 0);
        }


    }

球类:(椭圆)

class Ball
    {
        private Control canvas;
        private Timer t = new Timer();
        private int dir = 1;

        public Point Center { get; set; }
        public Color clr;
        public int Radius { get; set; }

        public Control Canvas
        {
            get { return canvas; }
            set
            {
                canvas = value;
                if (canvas != null)
                {
                    //canvas.SizeChanged -= Canvas_SizeChanged;
                    //canvas.SizeChanged += Canvas_SizeChanged;
                    Center = new Point(canvas.ClientSize.Width / 2, canvas.ClientSize.Height/ 2);
                }
            }
        }
        private static readonly Random rand = new Random();

        public Ball(Control canvas)
        {
            clr = Color.FromArgb(rand.Next(255), rand.Next(255), rand.Next(255));
            if (canvas != null)
            {
                this.canvas = canvas;
                canvas.SizeChanged -= Canvas_SizeChanged;
                canvas.SizeChanged += Canvas_SizeChanged;
                Center = new Point(canvas.ClientSize.Width / 2, canvas.ClientSize.Height / 2);

            }

            t.Interval = 10;
            t.Tick += timer_Tick;
        }
        private void Canvas_SizeChanged(object sender, EventArgs e)
        {
            Center = new Point(canvas.ClientSize.Width / 2, canvas.ClientSize.Height / 2);
        }
        public void Growing()
        {
            dir = 1;
            t.Enabled = true;

        }

        public void timer_Tick(object sender, EventArgs e)
        {
            if (canvas == null)
            {
                //t.Stop();
                clr = Color.Transparent;
                return;
            }
            Radius += dir;

            if (Radius > Math.Min(canvas.ClientSize.Width, canvas.ClientSize.Height) / 2)
            {
                Radius = Math.Min(canvas.ClientSize.Width, canvas.ClientSize.Height) / 2;
                clr = Color.Transparent;
            }
            canvas.Invalidate();
        }

        public void Update(Graphics g)
        {
            g.FillEllipse(new SolidBrush(clr), new Rectangle(Center.X - Radius, Center.Y - Radius, Radius * 2, Radius * 2));
        }
    }

2 个答案:

答案 0 :(得分:1)

现在更明确,是的。纠正我,如果我错了,但看起来你在绘画时根本没有使用list变量,那就是你每次只画一个球的原因。

答案 1 :(得分:1)

更改你的TheBalls_Paint事件:

 private void TheBalls_Paint(object sender, PaintEventArgs e)
 {
     Bitmap bitmap = new Bitmap(yourBall.Center.X * 2, yourBall.Center.Y * 2);
     flagGraphics = Graphics.FromImage(bitmap);
     foreach (var ball in list)
     {
         ball.Update(flagGraphics);
     }
     e.Graphics.DrawImage(bitmap, 0, 0);
 }