
Beginning REALbasic - From Novice To Professional (2006)
.pdf
316 |
C H A P T E R 1 1 ■ W O R K I N G W I T H G R A P H I C S A N D A U D I O |
Figure 11-8. Drawing a line, as seen on Linux
Drawing Shapes
The Graphics class also provides a number of different methods for drawing various types of shapes, including the following:
•Ovals
•Rectangles
•Polygons
•Round Rectangles
•Filled-in Ovals
•Filled-in Rectangles
•Filled-in Polygons
•Filled-in Round Rectangles
For example, the following code statements demonstrate how to draw a filled-in rectangle shape using the Graphics class’s FillRect method. This method requires four sets of arguments. The first two arguments represent the left-hand coordinates of the rectangle to be drawn and the last two arguments represent the width and height of the shape. The following shows an example of how to use the FillRect method.
g.ForeColor = RGB(255,0,0)
g.FillRect 50, 50, 200, 200
Figure 11-9 shows an example of the output produced.

C H A P T E R 1 1 ■ W O R K I N G W I T H G R A P H I C S A N D A U D I O |
317 |
Figure 11-9. Drawing a filled-in rectangle, as seen on Linux
The Fun and Practical Side of Working with Graphics
As you’ve already seen, REALbasic provides you with all the tools you need to create custom graphics. By adding custom graphics to your windows, you can add personal touches to your applications. Using the Canvas control, you can create and display custom images, such as corporate logos. The possibilities are endless.
■Note Another way to use graphics in REALbasic is to create custom controls. For example, a Visual Basic programmer migrating an application over to REALbasic might run into a situation where a particular Visual Basic control, such as the ListView control, does not match up to a corresponding REALbasic control. One way of dealing with this situation is to build your own custom control that provides equivalent functionality. You can do this using the Canvas control, the Graphics class, and some custom code. The creation of custom controls is an advanced topic and beyond the scope of this book. To learn more, refer to REALbasic’s Reference Manual.
Building an MP3 Player
This chapter wraps up by demonstrating how to create a custom-built MP3 player named the RB MP3 Player. While this chapter demonstrates the creation of this application using the Macintosh version of REALbasic, it can also run on Windows.
The RB MP3 Player, shown in Figure 11-10, provides users with the capability to select and play MP3 audio files. Users can also elect to repeatedly play a MP3 file. MP3 playback can be stopped at any time and the sound can be muted. The user also has the capability to control sound volume.

318 |
C H A P T E R 1 1 ■ W O R K I N G W I T H G R A P H I C S A N D A U D I O |
Figure 11-10. A REALbasic MP3 player, as seen on Mac OS X
Designing the User Interface
Begin the creation of the RB MP3 Player application by starting a new project. Resize Window1 as you see in Figure 11-10 and change its Title property to RB MP3 Player. Next, add three PushButton controls, two StaticText controls, a Slider control, and a Checkbox control, and then align them, as Figure 11-10 shows.
Once you finish adding the required controls, modify the property values for the controls, as Table 11-2 specifies.
Table 11-2. Property Modifications for the RB MP3 Player Application
Object |
Property |
Value |
PushButton1 |
Name |
pbnPlay |
|
Caption |
Play |
|
Enabled |
False |
PushButton2 |
Name |
pbnStop |
|
Caption |
Stop |
|
Enabled |
False |
PushButton1 |
Name |
pbnMute |
|
Caption |
Mute |
|
Enabled |
False |
StaticText1 |
Name |
txtVolume |
|
Text |
Volume: |
StaticText2 |
Name |
txtFileName |
|
Text |
|
Slider1 |
Name |
sdrVolume |
|
Enabled |
False |
|
Value |
50 |

C H A P T E R 1 1 ■ W O R K I N G W I T H G R A P H I C S A N D A U D I O |
319 |
Object |
Property |
Value |
CheckBox1 |
Name |
chkLoop |
|
Enabled |
False |
|
Caption |
Loop |
|
|
|
Setting Up the Menu System
The RB MP3 application has a simple menu system that consists of one menu and two menu items. To set it up, open the Project Screen and double-click Menubar1. Select the Edit menu that REALbasic automatically adds to the menu system and press Delete to remove it. Then, select the File menu and click the Add Menu Item button located on the editor’s toolbar. Enter &Open MP3 File as the text for the menu item, and then press Enter. Next, drag-and-drop this menu item to the top of the File menu, so it is the first menu item displayed.
Setting Up Property Values
The RB MP3 Player application uses the Sound class to facilitate the playback of MP3 audio files, so you need to set up an instance of the Sound object that can be referenced by all parts of the application. Because the application consists of a single window, you can achieve this by using a custom window property to declare the Sound object. To set this up, open the Window Code editor, and then click the Add Property button located on the editor’s toolbar. Enter SoundFile in the Declaration field and Sound in the As field.
SoundFile As Sound
Adding Program Code
Now, you’re ready to start the coding process for this application. To begin, let’s set up the menu handler for the FileOpenMP3File menu item. Do so by clicking the Add Menu Handler button located on the Code Editor’s toolbar. Then, enter the following code statements.
Dim AudioTypes As New FileType |
'Declare a FileType object |
AudioTypes.Name = "Audio Files" 'Set a string identifying supported files AudioTypes.Extensions = ".mp3" 'Specify supported file types
'Declare a folder object and retrieve the name and path of a MP3 file Dim TargetFile as FolderItem = GetOpenFolderItem(AudioTypes)
'Perform the following as |
long as the user did not click on Cancel |
|
If TargetFile <> Nil Then |
|
|
SoundFile = TargetFile.OpenAsSound |
'Retrieve the audio file |
320 C H A P T E R 1 1 ■ W O R K I N G W I T H G R A P H I C S A N D A U D I O
'Enable interface |
controls |
pbnPlay.Enabled = |
True |
pbnStop.Enabled = |
True |
pbnMute.Enabled = |
True |
chkLoop.Enabled = |
True |
sdrVolume.Enabled |
= True |
'Display the name |
of the selected mp3 file |
txtFileName.Text = TargetFile.Name |
End If
When clicked, the FileOpenMP3File menu displays a standard Open window dialog, enabling the user to specify the name and location of a MP3 audio file. If the user specifies a MP3 file, it is opened and assigned to the SoundFile object. In addition, the interface controls on the window are enabled to let the user begin playing the MP3 audio file. The filename of the MP3 file is also displayed at the bottom of the window.
All that remains is to enter the code associated with each of the application’s interface controls. Start by opening the Action event handler for the pbnPlay control and assigning it the following code statements.
'Determine whether to execute the play or playlooping method
If SoundFile.IsPlaying <> True Then 'If mp3 is already playing don't do anything If chkLoop.Value = False Then
'Set sound level equal to slider control value SoundFile.Volume = sdrVolume.Value
SoundFile.Play 'Play the mp3 file Else
SoundFile.PlayLooping 'Repeatedly play the mp3 file chkLoop.Enabled = False 'Disable the CheckBox control
End If End If
When executed, the first statement checks to see if a MP3 file is already playing. If this is the case, the rest of the statements are skipped and the current MP3 file keeps playing. Otherwise, the next code statement checks to see if the user selected the CheckBox control (chkLoop), indicating the MP3 file should be played over and over again. Based on the result of this evaluation, the MP3 audio is played using either the Sound class’s Play or PlayLooping method.
Next, open the Action event handler for the pbnStop control and add the following code statement.
SoundFile.Stop 'Stop mp3 file playback
When executed, the code in this event handler stops MP3 file playback. Next, open the Action event handler for the pbnMute control and add the following code statements.
C H A P T E R 1 1 ■ W O R K I N G W I T H G R A P H I C S A N D A U D I O |
321 |
'Toggle between mute and volume If SoundFile.Volume <> 0 Then
SoundFile.Volume = 0 'Mute the sound Else
SoundFile.Volume = sdrVolume.Value 'Resume sound End If
These code statements toggle the value assigned to the Volume property between 0 and sdrVolume.Value, effectively muting and unmuting the sound playback. Finally, open the ValueChanged event handler for the sdrVolume control and add the following code statement.
SoundFile.Volume = Me.Value 'Adjust playback volume
This statement adjusts the volume level used to play the MP3 file, based on the value assigned to the Slider control (for example, Me.Value).
Testing the RB MP3 Player
That’s it. The RB MP3 Player should be ready to run. Save and compile the application, and then make sure it works as advertised. If you encounter any errors when compiling the application, go back and look for typos.
Summary
In this chapter, you learned how to liven your REALbasic applications by adding different kinds of audio capabilities, including beeping, sound play, speech pronunciation, and musical notes. You also learned how to work with the Canvas control, and how to use methods and properties belonging to the Graphics class. On top of all this, you learned how to create a number of new REALbasic applications.

C H A P T E R 1 2
■ ■ ■
Debugging REALbasic
Applications
In Chapter 11, you learned how to integrate audio and graphics into your REALbasic applications. In this, the final chapter of this book, you learn what to do when your REALbasic applications won’t compile. This includes learning how to use REALbasic’s built-in debugger to track down and fix syntax errors, as well as learning how to trace program flow and track the status of variables, properties, and objects. You also learn how to set breakpoints to arbitrarily pause applications, after which you can manually control statement execution one line at a time. And, last, you learn how to programmatically handle errors to prevent them from crashing your applications or to present the user with friendlier and more useful errors messages.
Specifically, you learn how to
•Track down and fix syntax errors
•Use the REALbasic debugger to track logic flow and variable, property, and object values
•Set breakpoints to pause application execution
•Add code that handles run-time errors
Tracking Down and Fixing Errors
You have inevitably run into a problem or two as you worked with the various sample applications presented in this book. Specifically, you probably made a few typos here and there. Up to this point, you were advised to double-check your typing as the primary means to tracking down and fixing errors or bugs.
As you begin developing your own REALbasic applications from scratch and, as your applications grow larger and more complex, you are going to run into more errors. Even the most experienced and talented programmers have to deal with errors. This is an inevitable fact of programming.
323

324 |
C H A P T E R 1 2 ■ D E B U G G I N G R E A L B A S I C A P P L I C A T I O N S |
REALbasic applications are subject to several different types of errors, which we explore in the following sections. These errors fall into three main categories as follows:
•Syntax
•Run-time
•Logical
■Tip As a programmer, you should always look for ways to write better, error-free code. One way of working toward this goal is adopting a number of good programming practices, including
•Using consistent naming standards for your variables, constants, objects, properties, subroutines, and functions
•Adding programming logic to your applications that validate all user input
•Adding programming logic to your applications to handle missing files or incorrect file paths
•Providing users with clear instruction on the proper use of your applications
•Anticipating places in your applications where errors are most likely to occur and developing excep- tion-handling procedures to deal with these situations
•Testing your applications extensively, making sure every piece of functionality is verified
•When testing, try feeding your application erroneous input to ensure it handles the input correctly
Understanding Syntax Errors
Perhaps the most common form of programming error is the syntax error. Syntax errors occur when you fail to write programming statements that conform to REALbasic’s rules. In many cases, syntax errors occur because of typos you make when keying in programming statements. Syntax errors also occur when you fail to provide required statement parameters or when you provide more parameters than are accepted by a method, function, and so on.

C H A P T E R 1 2 ■ D E B U G G I N G R E A L B A S I C A P P L I C A T I O N S |
325 |
Eliminating Syntax Errors One at a Time
If a syntax error occurs when you are testing your application within the REALbasic integrated development environment (IDE), REALbasic stops executing the application and flags the error. REALbasic highlights the code statement where the syntax error occurred in the Code Editor and displays a small image of a bug to the left of the code statement, as Figure 12-1 shows.
Figure 12-1. REALbasic displays a message describing a syntax error at the bottom of the IDE as a Tip, as seen on Linux.
REALbasic won’t compile your application until every syntax error has been corrected. Each time you attempt to compile an application with one or more syntax errors, REALbasic stops compilation at the first syntax error it finds and highlights the error. By highlighting syntax errors in this manner and providing you with information about the error, REALbasic makes tracking syntax errors relatively painless.