Description of the
Article
Overriding the
OnPaint method and using the Timer class allows us to create a simple animation
where we rotate a line around a central point.
Anyone want to create an asteroids clone in VB.NET?
Source Code:
Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.Drawing.Drawing2D
Namespace Rotationb2
' <summary>
' Summary description for Form1.
' </summary>
Public Class Form1
Inherits System.Windows.Forms.Form
Private timer1 As System.Windows.Forms.Timer
Private components As System.ComponentModel.IContainer
Public f As Single =
0
Public Sub New()
'
' Required for Windows Form Designer support
'
InitializeComponent()
End Sub 'New
'
' TODO: Add any constructor code after InitializeComponent
'
' <summary>
' Clean up any resources being used.
' </summary>
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub 'Dispose
' Required method for Designer support - do not modify
' the contents of this method with the code editor.
' </summary>
Private Sub InitializeComponent()
Me.components
= New System.ComponentModel.Container
Me.timer1
= New System.Windows.Forms.Timer(Me.components)
'
' timer1
'
Me.timer1.Enabled
= True
Me.timer1.Interval
= 50
'
' Form1
'
Me.AutoScaleBaseSize
= New System.Drawing.Size(5,
13)
Me.ClientSize
= New System.Drawing.Size(240,
221)
Me.Name
= "Form1"
Me.Text
= "Rotationb2"
End Sub 'InitializeComponent
'
<summary>
' </summary>
Shared<STAThread()>
Sub Main()
Application.Run(New Form1)
End Sub 'Main
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
e.Graphics.DrawEllipse(Pens.Red, 0, 0, 220, 220)
Dim gp As New GraphicsPath
gp.AddLine(30, 30, 110, 110)
Dim RotationTransform As New Matrix(1,
0, 0, 1, 1, 1)
Dim matrix As rotation
Dim TheRotationPoint As New PointF(110.0F,
110.0F)
Dim point As rotation
RotationTransform.RotateAt(f, TheRotationPoint)
gp.Transform(RotationTransform)
e.Graphics.DrawPath(Pens.Blue, gp)
f = f + 10
If f
= 360 Then
f = 0
End If
End Sub 'OnPaint
Private Sub timer1_Tick(sender As Object,
e As System.EventArgs) Handles Me.timer1.Tick
'force client window to be refreshed
Me.Refresh()
End Sub 'timer1_Tick
End Class 'Form1
End Namespace 'Rotationb2