Graphics, Animation, and Randomization continued
Some additional techniques for programming graphics and animation, along with some new programming language concepts. This material is optional – you don’t need it to complete Project 2, or for the exams. |
Techniques for : | ||
Drawing geometric figures | ||
Objects that follow the mouse | ||
Animation | ||
Small ideas: | ||
Using trigonometry in programming graphical layout | ||
Named constants | ||
for loops (another construct for iteration) | ||
Mouse move events & dragging | ||
Double buffering | ||
Big idea: | ||
Object-oriented programming |
You can download the sample code for today from the “Example Code” link on the CSE/IMT 100 web page. All the code is in a zip file; ProjectSquiral is also available separately. | ||
Sample files: | ||
ProjectSquiral – interactive squirals | ||
ProjectBlackSquiral – nicer colors, full screen | ||
ProjectDrag – dragging an object | ||
ProjectAnimate – very simple animation | ||
ProjectAnimatedSquiral | ||
ProjectBufferedSqural – illustrates double buffering |
In programming we often need to use various constants, e.g. RGB combinations, the number of steps to take in a squiral, etc. | ||
It is good programming practice to give names to these, rather than embedding the constants in your code. | ||
Advantages: | ||
Putting them at the beginning of your code makes it easier to find and change them. | ||
If you use the constant in several places and want to change it, there’s just one place to change. |
VB has various constants already built in. Examples: | ||
vbRed, vbBlack, vbBlue, etc: colors | ||
vbLeftButton: code for left mouse button pushed (an integer) | ||
Declaring your own: |
Recall that VB, like most other modern environments for building graphical user interfaces, uses an event model. | |
For example, we generate an event (and call an appropriate procedure) whenever the user clicks on a button. | |
Whenever the mouse moves, VB generates a “mouse move event” | |
You can do simple animations using the timer control. On each call to the Timer procedure, erase the form and draw the new figure. |
If you are doing a complex animation, it will take VB a bit of time to do the drawing. | ||
Problem: flicker | ||
Solution: double buffering | ||
Draw the figure in a different picture that is not visible | ||
Copy the result into the visible picture | ||