Drawing and Preparing the VB.Net Windows Form
In this tutorial, you'll learn how to animate shapes in VB.Net by drawing and moving a circle
across a Windows Form using the Graphics object and the Paint event.
This hands-on example will help you understand how to build smooth VB.Net animations with simple code.
We'll start by drawing a circle in VB.Net using the FillEllipse method
from the Graphics class. This will serve as the base for our moving object animation.
Animating the Circle in VB.Net
To make the circle move across the form, we'll repeatedly draw it in new positions
while clearing the previous frame - creating smooth motion.
Steps to Animate a Circle in VB.Net
- Create the main form (Form1)
- Set up the Form in the Facet class. This class handles the window setup, background color, and button layout for your project.
- Set up the Animation Controller in the Dymetric class. This class decides whether to draw or animate the circle based on user input.
-
Animating the Object in the MovingBody class. This is where the actual VB.Net animation occurs.
The circle is drawn, cleared, and redrawn in successive positions to simulate smooth motion across the form.
Create a new Visual Basic Windows Forms Application
project
;
call it Dymetric_VB.
Create 3 new VB.Net classes;
Call them Facet, Dymetric and MovingBody.
Type out the adjoining VB.Net code for animating an image body across the windows form.
Important: To generate the Form1_Paint VB.Net code stub,
select the form in design mode; go to its properties; click on the
symbol and select Paint.
Note: The VB.Net codes for our Form1 and Facet classes were too big to be squeezed in with the others.
They are simply included as links of their own.
How the VB.Net Move Object Animation Code Works
The MovingBody VB.Net class handles the actual drawing and movement. It:
- Draws a circle with
FillEllipse.
- Uses
Clear from Graphics.FromImage to erase old drawings.
- Updates the
x coordinate with a loop.
- Uses
Thread.sleep() to control speed.
This demonstrates a simple but effective VB.Net animation technique.
Summary: VB.Net Moving Object Animation
- Draw and animate a circle using VB.Net
- Use the Graphics object to manage drawing and clearing
- Control animation using the Paint event and classes
- Create a simple Windows Forms animation in VB.Net
Key Takeaway on VB.Net Windows Form and Object Animation
In this tutorial, you've learnt how to animate a circle in VB.Net using the Graphics object and the Paint event.
This example demonstrates how to move an object across a VB.Net form, clearing and redrawing it to create smooth motion.
By combining Thread.Sleep() and a loop, you can easily build simple animations in VB.Net.
So! VB.Net Fun Practice Exercise - Animate Circle
As a fun practice exercise, try changing the speed, direction, or shape. This will help you
understand more about VB.Net graphics drawing and updating positions.
VB.Net Moving Body Window Display Code Stub
VB.Net Moving Body Facet Window Code Stub
VB.Net Moving Body Code for Dymetric Class
Public Class Dymetric
Private move_body As New MovingBody
Private do_simulation = False
Public Sub decideAction(sender As Object, g As Graphics, click_check As Boolean)
If do_simulation And click_check Then
move_body.play(sender, g)
do_simulation = False
Else
move_body.prep(sender, g)
do_simulation = True
End If
End Sub
End Class
VB.Net Animation Code for Moving Body Class
Public Class MovingBody
Private x, y As Integer
Private previous_x As Integer = 0
Private previous_y As Integer = 0
Private Const ballDIAMETER = 80
Dim ball_colour As New System.Drawing.SolidBrush(System.Drawing.Color.Yellow)
Dim bg_colour As New System.Drawing.SolidBrush(System.Drawing.Color.LightGray)
Public Sub prep(sender As Object, g As Graphics)
x = 10
y = Math.Round(sender.Height / 2)
If previous_x > 0 Then
g.FillEllipse(bg_colour, previous_x, previous_y, ballDIAMETER, ballDIAMETER)
End If
g.FillEllipse(ball_colour, x, y, ballDIAMETER, ballDIAMETER)
previous_x = x
previous_y = y
End Sub
Public Sub play(sender As Object, g As Graphics)
Do While x < sender.Width - ballDIAMETER
g.FillEllipse(bg_colour, previous_x, previous_y, ballDIAMETER, ballDIAMETER)
g.FillEllipse(ball_colour, x, y, ballDIAMETER, ballDIAMETER)
previous_x = x
x += 10
Threading.Thread.Sleep(50)
Loop
End Sub
End Class