Create Graphics & Shapes In “Visual Basic”

Create Graphics & Shapes in “Visual Basic”

Creating graphics and shapes in Visual Basic (VB) begins with the necessary programming steps to draw simple elements such as points, lines, rectangles, circles, ellipses and arcs. By combining these elements in different ways and forms, complicated graphics can appear on your computer screen. The process requires writing laborious computer code in the absence of sophisticated graphics software but is perfectly possible using the building blocks described here.

Instructions

1. Open a new Visual Basic project using the “Standard EXE” template, one of the choices that appear when you choose a new project. Save the project and the form when prompted as MyGraphicsDemo. Set the “Form1” Height in the Properties window to 5400 and the Width to 6800.

2. Double-click on the “CommandButton” icon in the left-hand Toolbox column to add it to the form. This is a small rectangle normally located in the right-hand column of tools and third from the top. The label appears when you put your cursor on it. Click on the button to focus on its “Properties” in the window on the right and change the “Caption” property to read “Show Me Some Colored Points!” without entering the quotation marks. Change the Height property to 500 and the Width property to 3000.

3. Open a code window by clicking on “View” and then “Code” in the top level Visual Basic menu. Set the location points on the screen, the size and the color first with the illustrative code. Then vary the settings to learn what is possible using just single points. In this code the “PSet” lines create different-colored dots, while the code lines containing “Rnd” duplicate many points in random colors and locations. When done, press “F5” to see the result.

READ  Radeon X850 Card Specs

Private Sub Command1_Click()

BackColor = vbRed

PSet (0, 0)

PSet (15, 5), vbCyan

PSet (20, 10), vbYellow

CurrentX = 500: CurrentY = 500

For i = 1 To 20

PSet Step(0, 0)

Next

For i = 1 To 500

DrawWidth = Rnd * 5 + 1

PSet (Rnd * ScaleWidth, Rnd * ScaleHeight), RGB(Rnd * 255, Rnd * 255, Rnd * 255)

Next

DrawWidth = 1

End Sub

4. Create a large triangle using the “Line” Visual Basic command, which uses location settings to draw the triangle. Add a colored rectangle with the Line command by using different location points. Add two more CommandButtons to the form as in Step 2. Change the Caption properties to read “Draw a Triangle” and “Draw a Rectangle” respectively. You may need to widen the boxes as in Step 2. Then enter the following code below the previous lines of code. When done, press “F5” to view.

Private Sub Command2_Click()

Line (1500, 3000) – Step(1000, 0), vbBlack

Line – Step(0, 1000), vbBlack

Line – (1500, 3000), vbBlack

End Sub

Private Sub Command3_Click()

Line (1200, 1500) – Step(2100, 1100), vbGreen, B

End Sub

5. Draw a circle with the following lines of code. Drawing ellipses and arcs uses the same process; you just alter the dimensions and where the circles start and end. By widening one aspect of the circle, you turn it into an ellipse and by not closing the circle, you make an arc. To set this up, add one more CommandButton, labeling this one “Draw a Circle”.

Private Sub Command4_Click()

DrawWidth = 5

FillStyle = vbFSSolid

FillColor = vbCyan

Circle (2000, 2000), 1000, vbBlue

End Sub