0
Reply

Bouncing Ball Invalidation

raghupathy

raghupathy

Sep 8 2009 1:53 AM
4.3k

Hi all i have created this code for bouncing ball.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Ball
{
    public partial class Form1 : Form
    {
        bool Y = true;
        int damp = 2;
        int speedY = 10;
        int speedX = 1;
        int newHeight = 35;
        Timer Timer_Location = new Timer();
        Timer Timer_Invalidate = new Timer();
        int posX = 20;
        int posY = 20;
        Rectangle rec = new Rectangle();
       
        public Form1()
        {
            this.ClientSize = new Size(300, 300);
            this.MaximumSize = this.Size;
            this.MinimumSize = this.Size;
            DoubleBuffered = true;
            Timer_Location.Interval = 20;
            Timer_Location.Tick += new EventHandler(Timer_Ball_Location);
            Timer_Location.Enabled = true;
            Timer_Invalidate.Interval = 20;
            Timer_Invalidate.Tick += new EventHandler(Timer_Ball_Invalidate);
            Timer_Invalidate.Enabled = true;
           
            InitializeComponent();
        }
        void Timer_Ball_Location(object sender, EventArgs e)
        {
            update();
        }
        void Timer_Ball_Invalidate(object sender, EventArgs e)
        {
           Invalidate();
           
        }
       
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.Clear(Color.White);
            rec = new Rectangle(posX, posY, 20, 20);
            e.Graphics.FillEllipse(Brushes.Black, rec);
        }
        void update()
        {
            if (Y == true)
            {
                posX += speedX;
                posY += speedY;
                if ((posY + speedY) > 290)
                    posY = 290;
                if (posY >= 290)
                {
                    Y = false;
                }
            }
            if (Y == false)
            {
                newHeight += damp;
                posY -= speedY;
                posX += speedX;
                if (posY < newHeight)
                {
                    Y = true;
                }
            }
        }
        void rectangle()
        {
            Graphics g = this.CreateGraphics();
            Pen p=new Pen(Color.Red,10);
            g.DrawRectangle(p, 100, 100, 50, 50);
            g.FillRectangle(Brushes.Red, rec);
        }
      
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
             rectangle();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
          
        }
    }
}

Am invalidating the entire region.I think thats the reason the rectangle am creating is not shown on the form.I want to invalidate only the ball region.
Maybe the rectangle itself is not displaying.
I tried using
"Invalidate(rec);"
In that case the only half of the ball is visible during motion.You can check that by replacing Invalidate() with Invalidate(rec)
Help me in this.