FIT 100

Lab 13:  Graphics, Randomization, Iteration

Spring 2001

Reading to be done for Lab 13:

·         p. 272-278 (top of 278) in Chapter 1- of Computer Programming Fundamentals with Applications in Visual Basic 6.0

 

Introduction:

For Project 3, you have complete control of the look and design of your program.  Using the functions and methods available in VB to draw lines, boxes, circles and rectangles, you and your program will create a work of art.  Lab 13 shows you the basic statements needed to manipulate geographic shapes in a way that is pleasing to the eye.

 

Objectives:

  1. To prove (or disprove) the idea that computers can be creative, or create art.
  2. To write statements that direct the creation of geometric shapes on the form.
  3. To write statements that pass parameters in a procedure call.
  4. To write statements that allow the program to decide the location and color of the shapes in the form.

 

TO DO: 

  1. Open up a new Standard EXE project in Visual Basic.

  2. Name your Project Lab13

  3. Name your Form frmLab13

  4. Place a label in the center of your form with the following caption:

“Click on the Form to see something cool!”

         
          Name the label lblInstrunctions

 

  1. Declare global variables that will hold the values for the number of times someone has:

·         Clicked the form
Dim clickcount as Integer
 

·         The x coordinate for a line, box or circle
Dim xCoord as Integer

·         The y coordinate for a line, box or circle
Dim yCoord as Integer

·         The radius of a circle
Dim rad as Integer

·         The number of counts used as your program goes through a loop
Dim loopCount as Integer

 

Use the declared variables above to hold values when you want to have the program draw a line, a box or a circle.

 

  1. In the Form_Load event of your project, initialize clickcount to 0.


clickcount = 0

  1. In the Form_Click event, start tracking the number of times a user clicks on the form by incrementing the clickcount by 1 each time the click event is triggered.

 

clickcount = clickcount + 1

 

  1. Write a condition statement that does the following:

·         If the clickcount is 1, then make the label invisible.

·         Make the BackColor of the form RGB(171, 25, 191)

·         Maximize the WindowState of the form

 

Your statement should look like this:

         
              If clickcount = 1 Then

                        lblInstructions.Visible = False

                        frmLab13.BackColor = RGB(171, 25, 191)

                        frmLab13.WindowState = 2           ‘WindowState is a Form property

‘a WindowState of 0 is Normal,

          End If                                                 ‘1 is Minimized and 2 is Maximized

 

  1. Run your program.

  2. Save your project and form.

  3. Play around with changing the RGB color code of the form to different numbers.

  4. Now you will start to create shapes that are filled with different colors.  There is a property on the Form that has to be changed to allow this.  Go to the properties window, change the FillStyle property of the Form to 0 (Solid).

 

Adding Shapes

  1. Go back to the Form_Click event of your program.  Add another condition into your current if/then statement that does the following:

·         If the clickcount is 2, not 1, make the form FillColor RGB(0, 255, 0)

·         Give the xCoord and yCoord variables the value of 1000

·         Create a Box that goes from the xCoord and yCoord variables to the ScaleWidth – xCoord and the ScaleHeight – yCoord.

·         Make the outline of the Box the color red using  RGB values

 

Your statement should now look like this:

         
              If clickcount = 1 Then

                   lblInstructions.Visible = False

                   frmLab13.BackColor = RGB(171, 25, 191)

                             frmLab13.WindowState = 2

                   ElseIf clickcount = 2 Then

                                    FillColor = RGB(0, 255, 0)

                                    xCoord = 1000

                                    yCoord = 1000

frmLab13.Line (xCoord, yCoord)-(ScaleWidth - xCoord, _

    ScaleHeight - yCoord), RGB(255, 0, 0), B     

‘ScaleWidth and ScaleHeight are constant ‘variables that always hold the value of form’s ‘current Width and Height in twips  

End If 

  1. Run your program.  What happens?

  2. Save your project

  3. Add a circle into the condition for when the clickcount = 2. (Right after you create the box) 

·         Make the fill color anything you want, but give the circle a radius of at least 500. 

·         Use your lecture notes and the Project 3 description to figure out the proper syntax.

 

Procedures-Parameter and Arguments

  1. In your form click event, add yet another condition that does the following:

·         If the clickcount is 3, not 2 or 1, then start a loop using the Do While iteration statement.

·         Use the loopCount variable to keep track of the number of times your program does the loop.

·         Set loopCount to 0

·         While the loopCount is less than 10:

1.      Increment loopCount by 1

2.      Increment xCoord by 500

3.      Increment yCoord by 500

4.      Use the new xCoord and yCoord variable values as arguments and pass them to a procedure called Boxes.

·         Create a procedure with the name Boxes at the bottom of your code window.

1.      Set the parameters as integers and name them x and y

2.      Inside the procedure, change the fill color property to Red using RGB

3.      Create a Box that goes from the x and y coordinates to x + 500 and y + 500.

4.      Make the outline color Red as well using RGB

Your additional statement inside the IF/Then of the Form_Click event should look like this:

 

                   ElseIf clickcount = 3 Then

                                    loopCount = 0

   

                                    Do While loopCount < 10

                                    loopCount = loopCount + 1

                                    xCoord = xCoord + 500

                                    yCoord = yCoord + 500

                                   

Call Boxes(xCoord, yCoord)

 

                                    Loop

           

 

Your Boxes procedure should look like this:

 

                   Private Sub Boxes(x As Integer, y As Integer)

frmLab13.FillColor = RGB(255, 0, 0)

frmLab13.Line (x, y)-(x + 1000, y + 1000), RGB(255, 0, 0), B

 

End Sub

 

  1. Run your program.  What happens?
    All the boxes appear at once.  The loop is moving so fast that the naked eye cannot perceive the iteration.  What would happen if we used the timer control to slow down the loop a little so we can see each part of the loop as it runs?

 

Using the Timer Control

  1. Place a timer control on your form.  Name it tmrTime.  Leave the interval at 0.

 

  1. Add a 4th condition to check to your If/Then statement that does the following:

 

·         If the clickcount is 4, not 3, 2 or 1 then set the tmrTime interval to 1000. 

·         Go to the tmrTime object and select the Timer procedure from the drop down menus.

·         Inside the Timer event, we only want the event to “fire” if the user has clicked 4 times.  Write a condition that checks to see if clickcount is 4.

·         If it is, then increment xCoord by 1000 and yCoord by 1000

·         Use the new xCoord and yCoord variable values as arguments and pass them to the procedure called Boxes.

 

Your 4th condition to check inside the IF/Then of the Form_Click event should look like this:

 

          ElseIf clickcount = 4 Then

                                                                

                                    tmrTime.Interval = 1000

            End If      

 

The Timer event for the tmrTime control should look like this:

 

          Private Sub tmrTime_Timer()

If clickcount = 4 Then

 

xCoord = xCoord + 1000

                                    yCoord = yCoord + 1000

       

                                    Call Boxes(xCoord, yCoord)

 

End If

 

End Sub

 

  1. Run your program.  What happens on the 4th click?  Tres cool, no?
    For as long as clickcount is 4, the tmrTime_Timer event will fire with an interval of 1000 milliseconds and the statements inside of the If/Then will be executed.  Those statements increment the xCoord and yCoord variable values and then call a procedure Boxes with xCoord and yCoord as arguments.


  2. Add one last condition to the form click event.  If someone clicks a 5th time, use the Cls method to clear your form of all objects, make the lblInstructions caption visible and change the caption to “Thank you for clicking!”

 

Your last condition should look like this:

 

ElseIf clickcount = 5 Then

 

                  frmLab13.Cls

                  lblInstructions.Visible = True

                  lblInstructions.Caption = "Thank you for clicking!"

   

End If

 

  1. Run your program.  What happens on the 5th click? 

  2. For your project, practice changing the location of the Box and Circle coordinates.  Use the Timer event to control the flow.  Use what you learn about Randomization in lecture and in your reading to randomly place shapes on your form and to randomly choose color.

  3. Save your project to disk or FTP it to your Dante account.  You can use it as a reference for Project 3.