Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Animate Circle on Frame using Graphics and Thread class vb.net – C#.net.

Author: Shehzad Hemani
Download Source Code : 1039_source.zip

In this simple article you will learn that how we can animate circle on form using graphics and thread class in VB and C# .net.

 

Draw Circle:
To draw any shape on form it necessary to override onpaint method. In this method we will draw circle by using graphics class. To draw circle we have to use DrawEllipse method of graphics class this function takes object of pen class, x, y coordinates and width and height as parameter. It returns nothing.   
C#
protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen green = new Pen(Color.DarkRed, 3);
            Brush red = new SolidBrush(Color.DarkSlateGray);
            g.DrawEllipse(green, x, y, width, height);
            base.OnPaint(e);
        }
VB
Protected Overrides Sub OnPaint(ByVal As PaintEventArgs)
        
Dim As Graphics e.Graphics
        
Dim green As Pen = New Pen(Color.DarkRed, 3)
        
Dim red As Brush = New SolidBrush(Color.DarkSlateGray)
        g.DrawEllipse(green, x, y, width, height)
        
MyBase.OnPaint(e)
    
End Sub
This is simple code to draw a circle on form.
Animate Circle:
To animate circle we have to use thread. We will calculate x, y, width and height values again and again in thread and then will draw circle again and again on form by using current values. Make a function with name start. This function will call in thread and will calculate new values to draw the circle.
C#
public void Start()
        {
            int dx = 9, dy = 9;
            while (true)
            {
                for (int i = 0; i < 30; i++)
                {
                    x += dx;
                    y += dy;
                    width -= dx;
                    height -= dy;
                    Invalidate();
                    Thread.Sleep(90);
                }
                dx = -dx; dy = -dy;
            }
        }
VB
Public Sub Start()
        
Dim dy As Integer = 9
        
Dim dx As Integer = 9
        
        
While true
            Dim 
As Integer = 0
            
Do While (i < 30)
                x 
(x + dx)
                y 
(y + dy)
                width 
(width - dx)
                height 
(height - dy)
                Invalidate
                Thread.Sleep(
90)
                i 
(i + 1)
            
Loop
            
dx (dx * -1)
            dy 
(dy * -1)
            
        
End While
    End Sub
All above shown code is to animate a circle.
Suspend Animation:
To suspend the animation we have to use suspend function of thread class. This function takes and returns nothing and suspends the thread.
To demonstrate make a window application. Drag one button on form.
Now write the following code on Button click event:
C#
private void btn_suspend_Click(object sender, EventArgs e)
        {
            t.Suspend();
        }
VB
Private Sub btn_suspend_Click(ByVal sender As ObjectByVal As EventArgs)
        t.Suspend
    
End Sub
 
This is simple code to suspend the thread or animation.
Resume Animation:
To resume the animation we have to use resume function of thread class. This function takes and returns nothing and resumes the thread.
To demonstrate make a window application. Drag one button on form.
Now write the following code on Button click event:
C#
private void btn_resume_Click(object sender, EventArgs e)
        {
            t.Resume();
        }
VB
Private Sub btn_resume_Click(ByVal sender As ObjectByVal As EventArgs)
        t.
Resume
    End Sub
This is simple code to resume the thread or animation.
Abort Animation:
To abort the animation we have to use abort function of thread class. This function takes and returns nothing and aborts the thread.
To demonstrate make a window application. Drag one button on form.
Now write the following code on Button click event:
C#
private void btn_abort_Click(object sender, EventArgs e)
        {
            t.Abort();
        }
VB
Private Sub btn_abort_Click(ByVal sender As ObjectByVal As EventArgs)
        t.Abort
    
End Sub
This is simple code to abort the thread or animation.
Now write the following code on FORM LOAD event:
C#
private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "Devasp Circle Animation Application";             
        }
VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
        Me.Text = "Devasp Circle Animation Application" 
    End Sub

This simple article tells that how we can animate circle on form using graphics and thread class in VB and C# .net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Check intersection of rectangles and inflate rectangle using Drawing.RectangleF class vb.net – C#.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net