How To Develop Graphics And Game Application Using VB.NET
Rate this article
0 out of 5

In this video tutorial, we will show you how to develop a basic graphics application in VB.NET. After viewing the video tutorial, download the source code and try to modify the code so as to get a feel of what is learned in this video tutorial.

Source Code
Form1.vb
Public Class Form1
    Inherits System.Windows.Forms.Form
    Public gridWidth As Integer
    Public gridHeight As Integer
    Public gridNoOfRows As Integer
    Public gridNoOfCols As Integer
    Public gridXOffset As Integer
    Public gridYOffset As Integer
    Public Const DEFAULT_X_OFFSET As Integer = 100
    Public Const DEFAULT_Y_OFFSET As Integer = 50
    Public Const DEFAULT_NO_ROWS As Integer = 3
    Public Const DEFAULT_NO_COLS As Integer = 3
    Public Const DEFAULT_WIDTH As Integer = 60
    Public Const DEFAULT_HEIGHT As Integer = 60

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        Initialize() 'calling Initialize() function from the constructor to initialize the variables
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads 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

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
    Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem50 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem100 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem200 As System.Windows.Forms.MenuItem
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.MainMenu1 = New System.Windows.Forms.MainMenu
        Me.MenuItem1 = New System.Windows.Forms.MenuItem
        Me.MenuItem50 = New System.Windows.Forms.MenuItem
        Me.MenuItem100 = New System.Windows.Forms.MenuItem
        Me.MenuItem200 = New System.Windows.Forms.MenuItem
        '
        'MainMenu1
        '
        Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1})
        '
        'MenuItem1
        '
        Me.MenuItem1.Index = 0
        Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem50, Me.MenuItem100, Me.MenuItem200})
        Me.MenuItem1.Text = "View"
        '
        'MenuItem50
        '
        Me.MenuItem50.Index = 0
        Me.MenuItem50.Text = "50%"
        '
        'MenuItem100
        '
        Me.MenuItem100.Index = 1
        Me.MenuItem100.Text = "100%"
        '
        'MenuItem200
        '
        Me.MenuItem200.Index = 2
        Me.MenuItem200.Text = "200%"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.SystemColors.ControlLightLight
        Me.ClientSize = New System.Drawing.Size(408, 310)
        Me.Menu = Me.MainMenu1
        Me.Name = "Form1"
        Me.Text = "Matrix Grid"

    End Sub

#End Region
    Public Sub Initialize()
        gridNoOfRows = DEFAULT_NO_ROWS
        gridNoOfCols = DEFAULT_NO_COLS
        gridWidth = DEFAULT_WIDTH
        gridHeight = DEFAULT_HEIGHT
        gridXOffset = DEFAULT_X_OFFSET
        gridYOffset = DEFAULT_Y_OFFSET
    End Sub
    Public Sub drawGrid()
        Dim boardLayout As Graphics = Me.CreateGraphics 'creating the graphics object 
        Dim layoutPen As New Pen(System.Drawing.Color.Red) 'creating the pen
        layoutPen.Width = 5 'setting the pen width
        Dim X As Integer = DEFAULT_X_OFFSET
        Dim Y As Integer = DEFAULT_Y_OFFSET
        'drawing the raws
        For i As Integer = 0 To gridNoOfRows
            boardLayout.DrawLine(layoutPen, X, Y, X + gridWidth * gridNoOfCols, Y)
            Y = Y + gridHeight
        Next
        X = DEFAULT_X_OFFSET
        Y = DEFAULT_Y_OFFSET
        'drawing the columns
        For j As Integer = 0 To gridNoOfCols
            boardLayout.DrawLine(layoutPen, X, Y, X, Y + gridHeight * gridNoOfRows)
            X = X + gridWidth
        Next
    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        drawGrid() 'draw grid will be called in each painting events
    End Sub

    Private Sub MenuItem50_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem50.Click
        gridWidth = DEFAULT_WIDTH / 2
        gridHeight = DEFAULT_HEIGHT / 2
        Me.Refresh() 'Refresh() will call the paint function of the Form Me is equal to the this object in C++
    End Sub
    'draws the grid in actual size
    Private Sub MenuItem100_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem100.Click
        gridWidth = DEFAULT_WIDTH
        gridHeight = DEFAULT_HEIGHT
        Me.Refresh()
    End Sub
    'draws the grid in double size
    Private Sub MenuItem200_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem200.Click
        gridWidth = DEFAULT_WIDTH * 2
        gridHeight = DEFAULT_HEIGHT * 2
        Me.Refresh() '
    End Sub
End Class



Download Source Code

About The Author
KTS Training

KTS InfoTech Training division offers various short-term programming courses for Students and Professionals who are looking for a bright career in the field of Software development.

The programming video tutorials presented in this web site are taken from some of the training sessions conducted by KTS InfoTech.

You can also learn the Programming packages through Internet directly form our English Speaking Technical experts at an affordable rate.

For more details click here and submit your training requirements.



Number Of articles Posted
60
Number Of Messages Posted
3
Joined Date
14/11/2012
   
Author
Messages
Posted:
Post Your Comments
Name (Max 50 Chars)
Comments

 
TekTipsDownload
GateExam
Academic Projects
TekTipsExperts
Android