Not Quite a Magic Ball

In this chapter, we continue looking at the entertainment aspects of Tablet PC development as we create a 'Magic Ball,' which answers any questions we give it. You are probably familiar with the general ideas of the application, which gives generic answers to any question we ask it.

Note

The source code for the projects are located on the CD-ROM in the PROJECTS folder. You can either type them in as you go or you can copy the projects from the CD-ROM to your hard drive for editing.

Starting the Project

Let's start a new VB Windows Forms application and begin this project by constructing the GUI, which consists of a couple of PictureBox controls, a ListBox control, and a Button control. You can see their properties in the Table 27.1 and their respective locations in Figure 27.1.

Table 27.1: Adding controls to the application

Type

Name

Location

Size

PictureBox

picDest

8,8

400,400

PictureBox

PictureBox1

n/a

n/a

ListBox

ListBox1

n/a

n/a

Button

Button1

425,375

80,25

Timer

tmrSpin

Figure 27.1: The GUI for our application is complete.

With the GUI in place, we can concentrate on the programming aspects of the project. Begin by adding the following variable declarations:

Private SourceBm As Bitmap
Private SourceWid As Integer
Private SourceHgt As Integer
Private SourceCx As Single
Private SourceCy As Single
Private SourceCorners As PointF()
Private DestWid As Integer
Private DestHgt As Integer
Private DestCx As Single
Private DestCy As Single
Private DestBm As Bitmap
Private DestBackColor As Color
Dim Positive As Boolean
Dim txtAnswer As String
Dim PicTop As Integer

The first section of these variables handles the source image we are going to load. This is an image of the ball, which is located in the Projects folder on the CD-ROM in the Chapter 27 directory. This image is loaded into PictureBox1 as a way of getting it into the application. This PictureBox is never visible, nor will the end user ever be aware of its existence.

We can load the image into the PictureBox in the Form_Load event. The code for this part of the event, which loads the image and sets some of its properties, has been used several times, so it's not worth repeating at this late stage in the book:

The only thing that would be different to you in the previous code deals with the corners of the image. We use the points to rotate the image, which appears to give it some basic animation. The next step is to load the image into the destination variables, which are the second set of the original variables. This is the variable that will be manipulated and ultimately loaded into the picDest picture box. We also need to translate the corners to center the box at origin:

The remaining portion of the procedure sets some additional properties, such as setting the Interval of the timer to 100 and its Enabled property to False, initializing stop_time to Now, setting the text of Button1 to "Spin", and setting the text of the form to "Not Too Magic Ball". Additionally, we add items to the ListBox, which are the answers the ball gives us when we stop it from spinning.

The button is used to start and stop the ball from spinning. It is within its Click event that we set this up using an If...Then statement. When the user clicks the button, the If...Then checks to see if tmrSpin is enabled. If it is, then we know that the ball is spinning and we need to stop it, change the Text property of the button to "Spin", and produce an answer. If it is not enabled, we can start the spinning, change the text of the button to "Stop", and set the answer to an empty string.

There are two remaining procedures we need to work with, the first of which is the Paint event of picDest. We can use this Paint event to provide the ability to draw text on a picture box programmatically. This is how the answer will be drawn, and why we have to set it to an empty string whenever we are spinning the ball.

The final procedure we need to work with is the tmrSpin elapsed procedure. Within this procedure, we are going to perform the calculations for rotating and moving the picture so that it appears to be somewhat animated. The first lines of code begin by testing to see if we have a positive or negative value stored in the Boolean variable called Positive. This variable is used to go from positive to negative, negative to positive, and so on and so forth. By going back and forth with random values, it appears as if the ball is being twisted back and forth.

Here is the code for the beginning part of the tmrSpin elapsed procedure:

To rotate the image, we use the built-in Sin and Cos functions, translate to center the results of the destination image, and then display the results using picDest. picDest is also moved, which simply adds to the illusion of movement, by changing its Top property. When the button is clicked to stop the procedure, picDest is returned to its starting point and the original image is loaded back into it so that the ball appears exactly as it started. This also makes rendering the answer much easier.

This actually finishes the application, which can now be saved and tested. When you run it, make sure the ball rotates back and forth as in Figures 27.2 and 27.3. You can also verify that you receive an answer similar to the one seen in Figure 27.4.

Figure 27.2: The ball rotates one direction to start an animation.

Figure 27.3: The ball moves the opposite direction to simulate back and forth movement.

Figure 27.4: The animation is stopped and the answer is displayed.

Summary

In this chapter, we created another entertainment style of application. Although the Tablet PC is such a new platform, it appears that simple types of games are probably best suited for its hardware as mobility and battery power are an issue. That said, users of a Tablet PC might like to play games when they have power connected to their machine or when they know they can recharge their systems before they need to use it again for serious business purposes. Like the PDA market, the Tablet PC is poised for great growth in the area of entertainment and game development. With that in mind, we move our focus back to these topics, including 3D rendering with OpenGL and DirectX, in Chapter 29, 3D Rendering with OpenGL and DirectX 9. In Chapter 28, Storing Ink in a Database, we go back to the world of ink and look at how we can store ink in a database.