23 April 1999
Snyder, Bannon, Yasuhara

Project 2

"Yes, But Is It Art?"

The purpose of this project is to acquire additional experience with procedures, iteration, and conditionals in VB 6.0, hopefully having some fun with graphics and creativity along the way. The program you write will be expected to include the following features:

  1. a procedure with two or more parameters
  2. a procedure that calls another procedure of yours
  3. a procedure that is called from at least five different places in your code
  4. a function, i.e. a procedure that returns a value
  5. at least one For-Next loop
  6. at least one Select-Case statement

These will be the primary basis for grading this assignment, but they will probably be a natural outcome of your programming effort. Note that it’s possible for a single procedure to fulfill more than one of the above requirements, although it may take some clever planning and design. This is perfectly acceptable; i.e. you are not required to write a separate procedure for each of the six features listed above. As always, if you have any questions about this or any of the instructions, just e-mail us.

The question that will be explored by this project is, "Can Visual Basic be used ‘artistically?’" Naturally, art is often a matter of opinion, and in this case, the opinion that matters is yours. What is the most artistic or aesthetic way in which you can use the graphics and computational facilities of VB? Of course, VB was not designed as a graphics package, or even intended for use in any serious graphics applications, but with a little imagination, almost any general purpose programming language can be used to do interesting things graphically. Our goal is not to push the limits of graphics; we want to push the limits of VB. What can you think of? How creative can you be?

As an added incentive to being as ingenious as possible, we will have an in-class display of everyone’s design and award prizes for the best.

Suggestions: In general, you’re welcome to do anything you’d like with VB graphics and the above programming requirements. You could just draw a pleasing or clever picture on a form. Perhaps you could try making an electronic greeting card...Mothers Day is fast approaching! Motion or action in which the image develops dynamically on the screen can lead to interesting results; e.g. you might have a circle on your form that appears to grow every time you click. Repeatedly redrawing objects in different colors can also have a neat effect.

Project Design: Plan on constructing two designs, the first to be graded at an intermediate point and one as your final submission. (The second can be based on the first, or it can be entirely different.) Detailed requirements and due dates are given below.

When a project begins executing, it should show an empty form to the user. The user then clicks on the form, causing the image to be drawn. You will not be using controls extensively. All of the graphics will be produced by program code, written by you as procedures. Of course, since the image is produced when the user clicks on the form, the you’ll start your programming effort with the Form_Click() event subroutine. The window can be closed to terminate its execution, i.e. you do not need to explicitly stop it.

In developing the image program, you should emphasize writing procedures. (One could easily imagine writing a dozen small procedures for this project.) First of all, writing procedures is the purpose of the project. Secondly, and more importantly, procedures allow you to organize the development, encapsulating effects that you can then compose into more complex images. Accordingly, the Form_Click() event procedure should not be a very long piece of code, but rather it should merely orchestrate the calling of procedures that do the work, probably by calling other procedures.

Milestones: The first of your two required submissions is due at the start of lab on Thursday, April 29. The program you submit on this date should include at least four of the six programming requirements listed above. Focus on getting the programming requirements done before getting too wrapped up with aesthetic details, especially early on, when working toward this first due date.

The second (and final) submission is due at the start of lecture the following Tuesday, May 4. This program should fulfill all six programming requirements. It’s probably easiest for you to add to your first submission, but, as noted above, you’re welcome to submit a new program altogether, as long as the programming requirements are all met. Hopefully, you’ll have time to bring out the artist in you for this submission.

Since this is the first time you’re designing and writing procedures on your own, we strongly encourage you to keep in touch with us as you start planning your program and experimenting with VB, both things you should do before you start writing your program. You are encouraged (but not required) to e-mail Ken (at yasuhara@cs) a description of some/all of the procedures you plan to write for the first submission to fulfill four of the six requirements. This gives you a chance to make sure you’re on track for the Thursday due date.

Resources: As noted above, VB is not a graphics programming system, but there are many things that can be done with its basic capabilities. Chapter 12 of the Beginning Visual Basic 6.0 book discusses graphics, and though this chapter contains information that we have not covered (nor will we), it is possible to find quite a bit of useful information there that is accessible. See the examples below. If you see something in the Chapter that you’d like to use but don’t understand, just ask us.

In an effort to provide some useful raw material for creating interesting images, consider the following resources.

  1. Recall from Lecture (11) that the size of the window that you have to work with is given by the ScaleWidth and ScaleHeight properties. See p. 396.
  2. It is possible to "splash" the window across the whole display, i.e. maximize it, by setting the WindowState property of the form to 2, e.g.
  3.     frmMain.WindowState = 2
    

    After this, ScaleWidth and ScaleHeight will be huge. Find out by running this version of the Form_Click() procedure from a normal size window:

    Sub Form_Click()
        MsgBox "The initial size is " & ScaleWidth & " x " & ScaleHeight
        frmMain.WindowState = 2
        MsgBox "The new size is " & ScaleWidth & " x " & ScaleHeight
    End Sub
    
  4. The QBColors mentioned in Lecture (11) are pretty basic. It is possible to create, using a combination of red, green and blue, any color that can be displayed. This is explained on pages 391-394. As an example of producing a color by the combination of red, green and blue, use the RGB(r,g,b) function which has the three obvious parameters. The numbers given for each of the parameters have to be between 0 and 255. Try it by writing in the Form_Click() procedure
  5.     frmMain.BackColor = RGB(216,232,182)
    

    which turns the background to an "olive green". Of course, it is possible to dynamically change these values.

  6. It is possible to develop the code discussed in the book to find the proper R-G-B values for your desired colors, but that kind of code is already running in many applications. For example, in Microsoft Word, go to Format à Background à More Colors à Custom to get to one of the places where the color palette is available. Many other programs include this feature, including Windows Paint (Options à Edit Colors… à Define Custom Colors >>) and the Display Properties control panel. Click in the color square and adjust the brightness slider on the right until you find the color you want, and use the Red, Green, and Blue values in your program when calling the RGB() procedure.
  7. As discussed in Lecture (11) to draw a box on frmMain, use the line drawing facilities as follows:
  8. FillColor = RGB(_,_,_)   'set color for inside of rectangle
    FillStyle = 0            'make shapes filled with solid color
    frmMain.Line (boxLeft, boxTop)-(boxLeft + boxWidth, boxTop + boxHeight), RGB(_,_,_), B
    

    where you must put everything after the first two lines on a single line in your program. Also, fill in the blank R-G-B values with numbers or variable values from 0 through 255. Remember the key points about this call, namely the facts that you are specifying two corners of a rectangle, the "" is required between the two corners, and the "B" is required. If you don’t put in the second RGB() call shown, then the line around the rectangle is black.

    Drawing a circle is even easier (p. 419):

    FillColor = RGB(_,_,_)
    FillStyle = 0
    frmMain.Circle (centerLeft, centerTop), radius, RGB(_,_,_)
    

    where, if the RGB() is omitted, the line around the circle is black. Of course, (centerLeft, centerTop) is the center point of the circle, and radius is the radius.

  9. An interesting technique is shown by Wright on p. 413 of the book:
  10. He calls it splatter painting, and you should read about how it works in the book. I used it in the following procedure to get the effect shown:

    Private Sub Form_Click()
        Dim nIndex As Integer        'declare a loop index variable
        Dim nXCoord As Integer, nYCoord As Integer
        Dim nRed As Integer, nGreen As Integer, nBlue As Integer
        
        DrawMode = 9
    
        Randomize                   'Set up random number generator
    
        For nIndex = 1 To 32000
    	nXCoord = Int(Rnd(1) * frmMain.ScaleWidth)
    	nYCoord = Int(Rnd(1) * frmMain.ScaleHeight)
    	nRed = Int(Rnd(1) * 255)
    	nGreen = Int(Rnd(1) * 255)
    	nBlue = Int(Rnd(1) * 255)
    	PSet (nXCoord, nYCoord), RGB(nRed, nGreen, nBlue)
        Next nIndex
        
        DrawMode = 13
    End Sub
    

    Set up form so that it starts out with a white background (as described in lecture) and place a label with the words "Hi, Mom!" as its caption. The splatters will normally cover the form, including the letters. This looks kind of dumb, so the way to avoid this (on black letters) is to change the form property DrawMode to 9, Mask Pen. The DrawModes have different effects on how color appears when you draw, and you might want to experiment with them.

    The loop goes through 32,000 iterations. (Wright used only 2,000.) It is not possible to go through more than 32,767 using a standard Integer. So, to do more iterations of splattering, you will need to enclose the "32000-loop" shown in another, outer loop.

    A key idea in this example is random numbers. Computers cannot generate true random numbers, because they follow instructions faithfully. But, they can generate number sequences that "look" random and that can be verified scientifically to have random properties. To use random numbers, you need to do two things: First, you must put the Randomize command at the start of your program so the computer sets up the randomization. Second, when you want a random number, just write the function Rnd(1). The result is a random number between 0 and 1. So, the procedure above finds a random color value between 0 and 255 by the code

        Int(Rnd(1)*255)
    

    Suppose the random number from Rnd(1) was 0.5 (which doesn’t seem too random, I suppose!), then the product would yield 127.5 Since this must be converted into a whole number, the Int function is applied to the result to throw away the fractional digits, giving 127.

  11. Recall how we got the clock to "run" by redisplaying it whenever the time event went off? That same idea can be used for animations, too. Place a timer control on your form as we did before. (Don’t worry about where you place it, because it will disappear when the program starts running.) Next, declare a global variable at the top of your form code,
  12. Dim tickTime As Integer

    so that it is global to the timer routine. Next, initialize the tickTime variable in the Form_Load() event procedure

    Private Sub Form_Load()
        TickTime = 0
    End Sub
    

    Finally, write some code to draw on each time tick:

    Private Sub tmrMyClock()
        FillStyle = 0
        FillColor = RGB(200, 100, 50)
        frmMain.Line (200 + tickTime * 200, 200)-(400 + tickTime * 200, 400), RGB(200, 100, 50), B
        tickTime = tickTime + 1
    End Sub
    

    Remember the line drawing command must be on a single line. Then, whenever the timer goes off, the box is drawn in the next position. Run it. Nothing happen? Did you set the Interval on the timer? Experiment with different "speeds", smaller numbers make the timer go off more frequently, speeding up the image drawn. How do you stop it from running off the form? An If statement can be used to assure that tickTime isn’t allowed to get too large.

  13. There’s a quick and easy way to clear your entire form window (set it back to its background color):

frmMain.Cls

Cls stands for "clear screen."

Watch the FAQ and e-mail list for more graphics ideas and answers to your questions. Good luck, and have fun.