Search This Blog

Most Viewed

Thursday, March 3, 2011

Begin Animation with VB.Net








It is Very Easy to Begin Animation on VB.net.....


1.Make Windows Form Application.
2.Add reference Microsoft Script control version 5.5
3.Rename the form as Movin_curve
4.Add a picture box resize it as you want on your form, name it CurvePic
5.Add three buttons name as DrawPixBtn, DrawLinBtn, AbortBtn
6.Add Text box and name it FunctionTxtBox


now double click the form and past code below, and run the application... 






Imports System.Math
Imports VBScript_RegExp_55




Public Class Movin_curve


    Dim abort As Boolean = False




    Private Sub Movin_curve_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.BackColor = Color.FromArgb(red:=100, blue:=10, green:=25)
        CurvePic.BackColor = Color.White
        FunctionTxtBox.Text = 10*sin(t)*cos(x)


    End Sub


    Private Sub DrawPixBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DrawPixBtn.Click




        Dim drawScript As New MSScriptControl.ScriptControl
        drawScript.Language = "VBscript"


        Dim curvegraph As Graphics


        Dim posx, posy As Long
        Dim ox, oy As Integer
        Dim x, y, t As Double


        Dim tacc As Double = 0.1
        Dim xacc As Double = 0.5
        Dim scl As Double = 10


        ox = CurvePic.Width / 2
        oy = CurvePic.Height / 2


        Dim rectwidth As Integer = 1
        Dim rectheight As Integer = 1


        curvegraph = Graphics.FromHwnd(CurvePic().Handle)
        For t = -2 * PI To 2 * PI Step tacc


            My.Application.DoEvents()
            If abort Then
                abort = False
                Exit For
            End If


            For x = -CurvePic.Width / (2 * scl) To CurvePic.Width / (2 * scl) Step xacc


                drawScript.ExecuteStatement("x=" & x)
                drawScript.ExecuteStatement("t=" & t)
                drawScript.ExecuteStatement("y=" & y)
                y = drawScript.Eval(FunctionTxtBox.Text)


                posx = (ox + x * scl)
                posy = (oy - y * scl)


                If posx >= CurvePic.Width Then posx = CurvePic.Width - 1
                If posy >= CurvePic.Height Then posy = CurvePic.Height - 1
                If posx <= 0 Then posx = 1
                If posy <= 0 Then posy = 1


                curvegraph.DrawRectangle(pen:=New Pen(Brushes.Blue), x:=posx, y:=posy, width:=rectwidth, height:=rectheight)


            Next


            For x = -CurvePic.Width / (2 * scl) To CurvePic.Width / (2 * scl) Step xacc


                drawScript.ExecuteStatement("x=" & x)
                drawScript.ExecuteStatement("t=" & t)
                drawScript.ExecuteStatement("y=" & y)
                y = drawScript.Eval(FunctionTxtBox.Text)


                posx = (ox + x * scl)
                posy = (oy - y * scl)


                If posx >= CurvePic.Width Then posx = CurvePic.Width - 1
                If posy >= CurvePic.Height Then posy = CurvePic.Height - 1
                If posx <= 0 Then posx = 1
                If posy <= 0 Then posy = 1


                curvegraph.DrawRectangle(pen:=New Pen(Brushes.White), x:=posx, y:=posy, width:=rectwidth, height:=rectheight)


            Next




        Next
        MsgBox("Pushpkant Yadav")


    End Sub


    Private Sub DrawLinBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DrawLinBtn.Click
        Dim drawScript As New MSScriptControl.ScriptControl
        drawScript.Language = "VBscript"


        Dim curvegraph As System.Drawing.Graphics


        Dim posx, posy, dposx, dposy As Long
        Dim ox, oy As Integer
        Dim x, y, t As Double


        Dim tacc As Double = 0.1
        Dim xacc As Double = 0.5
        Dim scl As Double = 10


        ox = CurvePic.Width / 2
        oy = CurvePic.Height / 2


        curvegraph = Graphics.FromHwnd(CurvePic().Handle)
        For t = -2 * PI To 2 * PI Step tacc


            My.Application.DoEvents()
            If abort Then
                abort = False
                Exit For
            End If


            dposx = dposy = 0


            For x = -CurvePic.Width / (2 * scl) To CurvePic.Width / (2 * scl) Step xacc


                drawScript.ExecuteStatement("x=" & x)
                drawScript.ExecuteStatement("t=" & t)
                drawScript.ExecuteStatement("y=" & y)
                y = drawScript.Eval(FunctionTxtBox.Text)


                posx = (ox + x * scl)
                posy = (oy - y * scl)


                If posx >= CurvePic.Width Then posx = CurvePic.Width - 1
                If posy >= CurvePic.Height Then posy = CurvePic.Height - 1
                If posx <= 0 Then posx = 1
                If posy <= 0 Then posy = 1


                curvegraph.DrawLine(pen:=New Pen(Brushes.Blue), x1:=posx, y1:=posy, x2:=dposx, y2:=dposy)




                dposx = posx
                dposy = posy
            Next


            For x = -CurvePic.Width / (2 * scl) To CurvePic.Width / (2 * scl) Step xacc


                drawScript.ExecuteStatement("x=" & x)
                drawScript.ExecuteStatement("t=" & t)
                drawScript.ExecuteStatement("y=" & y)
                y = drawScript.Eval(FunctionTxtBox.Text)




                posx = (ox + x * scl)
                posy = (oy - y * scl)


                If posx >= CurvePic.Width Then posx = CurvePic.Width - 1
                If posy >= CurvePic.Height Then posy = CurvePic.Height - 1
                If posx <= 0 Then posx = 1
                If posy <= 0 Then posy = 1


                curvegraph.DrawLine(pen:=New Pen(Brushes.White), x1:=posx, y1:=posy, x2:=dposx, y2:=dposy)


                dposx = posx
                dposy = posy
            Next




        Next
        MsgBox("Pushpkant Yadav")


    End Sub


    Private Sub AbortBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AbortBtn.Click
        abort = True
    End Sub
End Class




'DrawLinBtn will draw curve in connected line formate
'DrawPixBtn will draw curve in discreat point formate
'AbortBtn will abort the drawing of curve, in the middle of drawing
'The Text written in FunctionTxtBox is must be a function of x and t, and it will drawn on the CurvePic.

No comments:

Post a Comment

If the contents is insufficient or if there any error, please write here....


Suggestion are welcome:

Popular Posts