Articles 3D Audio Custom Controls General RB Graphics Hacks Mac OS X Menus Novelty Printing REALbasic 2005 REALbasic 2006 Registration Resources Reviews Serial Speech Sockets XML Video Resource Links News Current News February 2006 January 2006 December 2005 November 2005 October 2005 September 2005 August 2005 July 2005 June 2005 May 2005 April 2005 March 2005 ![]() REALbasic for Dummies by Erick Tejkowski ![]() Learning REALbasic through Applications REALbasic for Macintosh REALbasic Cross-Platform Application Development
Older files are in Stuffit 5 or greater format. Newer files are ".Zip". Download StuffIt Expander |
|
Today we'll look at how to draw a spiral with REALbasic. In the process, we will also learn an important secret of top programmers: let someone else do the work! Intro A friend once told me that the best programmers are lazy ones. His point wasn't that lazy employees were better than hard workers. Rather, "lazy" coders are often better, because they know how to work smarter not harder. This week we'll look at how to draw a spiral. Rather than calculate the formula to do this task, we will adapt a Visual Basic spiral example. Because of its close similarity to REALbasic, Visual Basic code is often a great source for programming ideas. The web is littered with thousands of Visual Basic code examples that are ripe for the taking. For the first step in this week's tutorial, look at a Visual Basic spiral code example. Try to read through the code and see if you can guess how it might translate to REALbasic. Some hints might help you decipher this example:
Build the Interface and Add the Code This week's example is simple to create. Launch REALbasic, open Window1 and add a Canvas and a Slider control to it. Double click the Canvas to open the Code Editor and place the following code in its Paint event:
dim x,y,n,xmid,ymid as integer
dim angle, radius as double
dim lastx,lasty as integer
XMid = Me.Width / 2
YMid = Me.Height / 2
lastx=XMid
lasty=YMid
g.foreColor = rgb(255,255,255)
g.fillrect 0,0,me.width,me.height
g.foreColor = rgb(0,100,0)
g.penwidth = slider1.value
g.penheight = slider1.value
For n = 1 To 1000
Angle = n * 0.1
Radius = Radius + Angle * 0.01
x = XMid + Cos(Angle) * Radius
y = YMid - Sin(Angle) * Radius
g.drawline x,y,lastx,lasty
lastx = x
lasty = y
next
Finally, add the following line of code to the ValueChanged event of Slider1. Canvas1.refresh That's it! Select Debug-Run to test your work. Then, go back and compare your code with the Visual Basic version. Can you figure out how we replaced the drawing commands from Visual Basic with the drawing commands of REALbasic? We also added a little fanciness to the project to make it a tad more interactive. The slider control changes the width of our pen which draws with a green color. Your final result should look something like this: ![]() Conclusion As you can see, borrowing code has its benefits. This example was easy, but calculating a formula for drawing sprials is not something that immediately comes to mind for most people. With a little mental work, you can adapt existing code examples to suit your needs. And, never forget the programmer's motto : "REUSE CODE!". If you don't believe me on this one, take a look at the DLL folder in Windows some time. Download the finished product here and see you next week! |
||||
|
||||||||||||||||||||||||||||||||
Maintained by the Staff of ResExcellence. This entire site ©1997-2006 ResExcellence
Privacy Statement? Sure we gotta Privacy Statement.
[an error occurred while processing this directive]