|
2-21-02
Free Spirals! by
Erick Tejkowsi
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:
- Focus your attention on the Form_Paint subroutine. It is equivalent to the Paint event of REALbasic's Canvas control.
- The coordinate system for drawing Visual Basic graphics works like REALbasic, with the origin in the top left corner of the Canvas.
- Ditch the Declare statements. We don't need them.
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!
2-19-02
REALbasic News
by Erick Tejkowsi
Carbon or Cocoa ?
Many people are confused by references to Carbon and Cocoa. Geoff
Perlman, CEO of REAL Software,
explains what they are, what impact they have on you as a Mac
user, and what that means to users of REALbasic.
Check it out today!
REALbasic 4.0.2fc3
REAL Software has posted a "final candidate" of the forthcoming REALbasic 4.0.2
maintenance release.
Plucky is Ducky!
Plucky is a full featured headline grabber for your desktop that is
written in RealBasic. Plucky has full support for multi-threading and
can visit over 4,000 websites. Thanks to great libraries from RealBasic
users, many of the features of MacOS X are implemented, such as dock
icon notification and a dock menu.
You can
get it here.
New Version of Component X Graphics
Component X Graphics 2.1
speeds up high quality free rotation by 3.5
times or better compared to previous versions. It also speeds up high
quality stretching by 10% or better.
Scrolling Credits
ScrollCredits 0.1b1 is the second (public) plugin from
a person known only as "ra". The plugin lets you incorporate scrolling
credits in your application. You can choose the color of the text and
the backgound or you can choose a picture for the background. It
includes a fade-in/fade-out effect.
You can download it
here.
|