FIT 100

Lab 14:  Graphics and Randomization

Spring 2001

Reading to be done for Labs 13 and 14:

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

 

Introduction:

This is an extension of Lab 13.  Working forward from where Lab 13 left off, we do more with iteration and cover the Randomize statement as a way to introduce an element of surprise and creativity into your program.  

 

Objectives:

  1. To prove (or disprove) the idea that computers can be creative, or create art.
  2. To write statements to direct the creation of geometric shapes on the form.
  3. To write statements that pass parameters in a procedure call.
  4. To use the Randomize statement and the Rnd function to generate a random number.

 

TO DO: 

  1. Open up the Lab13.vbp project from last lab.

  2. In your form click event you already have a great deal of code to help create shapes on the screen when the program runs.  Let’s add a command button and use the click event associated with it to create other graphics.

  3. Add a command button to the upper left corner of your form:

·         Name the button cmdShrinkBox

·         Make the Caption “I’m Shrinking!”

·         Set the visibility to false

  1. We will have the button appear for someone to use when they have clicked 5 times (when the “Thanks for Clicking” label reappears), so:

·         Inside of the condition If clickcount = 5, make the cmdShrinkBox visible.
 

cmdShrinkBox.Visible = True

  1. Run your program. 

 

  1. Notice when you get to the fifth click, the label appears and so does the button.

  2. Click the button.  Nothing happens.  We need to add statements to the click event of cmdShrinkBox so that it appears as though a box is slowing shrinking down to nothing.

  3. First, save your project and form.

  4. Declare 4 local variables as integers inside of the cmdShrinkBox click event.  Call them xPos, yPos, xPos2 and yPos2. 

·         Assign xPos and yPos the value of 3000.

·         Assign xPos2 and yPos2 the value of 8000.

Private Sub cmdShrinkBox_Click()

Dim xPos As Integer, yPos As Integer

Dim xPos2 As Integer, yPos2 As Integer

xPos = 3000

yPos = 3000

xPos2 = 8000

yPos2 = 8000

 

End Sub

  1. Create an unfilled box that starts at xPos and yPos and goes to xPos2 and yPos2.  Make the box outline color Black using RGB colors.

    **You aren’t given the code for this.  Try to figure it out using the other boxes you have created as a template for creating this one. To make a box look unfilled, you have to change the fill style to transparent.**

  2. Run your program. Do you get a large box with a black outline?

 

  1. The next bit is a little tricky.  The box is created, but we want it to appear to shrink.

    The easiest way to make the box look smaller is to create a loop.  Each time through the loop, the box is redrawn, displayed then cleared so that in the next loop another box can be drawn on a clean form. 

    But the box is getting smaller, right?  So each time we draw it, the coordinates have to change a little. 

 

·         The beginning points (xPos and yPos) should increment.

·         The end points (xPos2 and yPos2) should decrement.

·         Since one end of the box in increasing and one is decreasing, the loop should run until they meet.  That is, it should run for as long as xPos does not equal xPos2 (or as long as yPos does not equal yPos2).  You can use either one for the iteration.

·         Draw the new box with the new coordinates

·         Clear the box so that the loop can run again and it looks as though there is movement

Your loop should look like this:

Do While xPos <> xPos2          ‘run the loop until xPos matches yPos, then stop

     

        xPos = xPos + 1                       ‘increment xPos and yPos

        yPos = yPos + 1                       ‘by one each time through

        xPos2 = xPos2 – 1                   ‘decrement xPos2 and yPos2

        yPos2 = yPos2 – 1                   ‘by one each time through

       

        Line (xPos, yPos)-(xPos2, yPos2), RGB(0, 0, 0), B

        Cls                       ‘This clears the form of any objects currently on it

    Loop

  1. Run your program. Do you get a large box with a black outline that slowly shrinks?

  2. Save your program

  3. How would you add color to your boxes?  Make the boxes appear blue as you do the loop.

 

Using the RANDOMIZE Statement

The Randomize statement and the Rnd function are extremely useful (especially in this project) when you want to randomly generate a number. 

 

You might want that number for Box coordinates, Circle coordinates, the radius of a circle, Fill styles (they go from 0 to 7), color, etc.

 

  1. Any time you plan on utilizing the Rnd function in a program, you should initialize the random number generator.  Do this by placing the statement Randomize inside of the form load event (remember you are already initializing clickcount here, so add this statement right after it):

    Randomize

  2. Now, let’s randomly generate a box color for our shrinking boxes button.  Since the colors can range from 0 to 255, we want to randomly generate a number from 0 to 255.

·         Go into the cmdShrinkBox click event.

·         Inside the loop, change the fill style to 0 so there is a solid color allowed for the boxes

·         Make the Fill color a randomly generated number between 0 and 255 (there are 256 possibilities) and make it change to display as an integer.

 

Your loop should now look like:

Do While xPos <> xPos2

       

     FillStyle = 0

      FillColor = RGB(Int(Rnd() * 256), Int(Rnd() * 256), Int(Rnd() * 256))                                                                         ‘generates a random number, not

                                                                        ‘exceeding 255 for red, green and blue!

      

          xPos = xPos + 1

          yPos = yPos + 1

          xPos2 = xPos2 - 1

          yPos2 = yPos2 - 1

       

          Line (xPos, yPos)-(xPos2, yPos2), RGB(0, 0, 0), B

          Cls

Loop

  1. Run your program.  When you get to the button and click it, is a box created that slowly shrinks and changes color?

  2. Experiment with different colors, coordinates. 

 

·         Can you create a series of statements that will slowly increase the size of a box?

·         Can you generate a circle that gets smaller and then bigger?

 

Try to answer these questions in your program outside of lab to give you practice for your project.

         

Going back through what you created for Lab 13 and 14, try adding in more of:

·         The use of the timer (or add additional timers)

·         Do while loops

·         Procedures that when called make boxes and circles

·         Randomly created boxes, circles, colors, etc.

 

Use these to labs and the lecture content to help you create a cool, innovative program that will astound your friends!

 

  1. Save your project to disk or FTP it to your Dante account so you can use it as reference for your project.