VB Tutorial
Tools
Free Downloads
Free Link Directory

Basics
Data Types
Windows Forms
Buttons
Check Box
Text Box
Combo Box
Radio Buttons
Progress Bar
Color & Font Dialogs
Open & Save Dialogs
Process Control

More Complex Examples
Audio Player
Rotating Rectangle


Visual Basic Tutorial - Graphics

Rotating Rectangle

In this example we are going to make use of the system drawing 2D graphics method. We are going to use the draw command to draw a rectangle 200x200, color the inside walls of the rectangle and put text on it. We will show you how to create custom pens with color and line width. This example will also demonstrate how to use brushes to fill objects with color. We will show you how to create a transform matrix for rotating the rectangle and how to control it with a simple timer. Please pay close attention to the methods for calculating the centre of the object and the rotating point as this can be a headache. I think the best way for you to understand the application is to copy and paste the sample code below into a new project. After you've done that run the application, do some tweaks and run it again. Basically just play by changing code here and there to see how it affects the example. Do not forget to add a timer called Timer1 to the project.

Imports System.Drawing.Drawing2D

Public Class Form1
Dim g As Graphics
Dim bm As Bitmap = New Bitmap(Me.Width, Me.Height)
Dim myAngle As Integer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.DoubleBuffered = True
g = Graphics.FromImage(bm)
Me.BackColor = Color.Black
Me.Width = 400 : Me.Height = 400
Timer1.Interval = 10
Timer1.Enabled = True
End Sub


Sub mydraw()

Dim myPen As New Pen(Color.Blue, 2)
Dim myPen2 As New Pen(Color.Red, 2)

Dim xMid, yMid As Integer
Dim
myW, myH As Integer
myW = 200 : myH = 200

'''Place object in centre
xMid = myW / 4 : yMid = myH / 4

'''get rotation point in centre
Dim Mypoint As New Point(xMid * 3, yMid * 3)

' Create transform matrix.
Dim myMatrix As New Matrix

g.DrawRectangle(New Pen(Color.Black, 7), xMid, yMid, myW, myH)
myAngle = myAngle + 1
myMatrix.RotateAt(myAngle, Mypoint, MatrixOrder.Append)
g.DrawRectangle(New Pen(Color.Gold, 3), xMid, yMid, myW, myH)
g.FillRectangle(Brushes.LightCoral, xMid, yMid, myW, myH)
g.DrawString("Pro2Visual", Me.Font, Brushes.Black, xMid, yMid)
g.Transform = myMatrix

Me.BackgroundImage = bm
Me.BackgroundImageLayout = ImageLayout.Center

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If myAngle = 360 Then myAngle = 0
mydraw()
Me.Refresh()
End Sub
End Class

If you liked this article then Please Thumb This Up with a short review - Stumble It!


Email us at: info@pro2visual.com
 

Get your Website Listed on our Free Link Banner Directory
Advertise your site with your own unique link banner we will also provide text links to your site.

 

Popular Articles

Moving a borderless form in vb.net

ComboBox1.Items.Add

Link Progress Bar with Timer in vb.net

How to open a folder using the process control

Start Vb.net programming. No get rich quick scam

How to Convert the Date Format

Click here for Industry Leading Development Softwa