Quartz Gradients

by Seth Willits

Continuing the article about drawing a simple rectangle with a Quartz shadow, in this article we take a look at axial gradients. The code for this is actually a major pain to explain (I admit I don’t know everything there is to know about it as well), so, sorry, but I’m going to pull a “just look at the code if you want” here.

What I’ve done is add on an extension to the Graphics class which takes a bunch of parameters.

Sub DrawAxialGradient(Extends g as Graphics,
c as Canvas,
vertical as Boolean,
color1 as Color,
color2 as Color,
Left as Integer,
Top as Integer,
Width as Integer,
Height as Integer)

The canvas parameter is actually optional, but you won’t be able to see why in the example project. Right now, because of the way Canvases are implemented it’s a pain in the butt to deal with because the context you’re drawing into is actually the window’s. That is, a Canvas isn’t a “real” control, it’s just an abstract way to look at a certain rectangle in the window. That’s a problem because if you’re trying to draw into a canvas, you need to know about the window the canvas is in so you can translate the coordinates and clip appropriately. If you’re not drawing in canvas, you pass in nil and it should just do the right thing.

The vertical boolean parameter specifies if the axial shading is vertical or horizontal (there’s no start point and end point definition in this implementation, it’s a bit hardcoded). Color1 and Color2 are the two (not, not three or more in this implementation), and the next four parameters are the bounds of the gradient.

It’s relatively limiting (better than what RB has!), but that’s mostly because to do everything you can in Quartz, you really have to do it all yourself, or I’d really have to spend more time to abstract it out and provide a few different methods and ways of doing things. Have look around the code and you should see why it’d actually be a little tricky.

Download Quartz Gradients REALbasic project

Originally published by ResExcellence
Reprinted with permission

Wiggle Window

by Erick Tejkowski

Should you accidentally enter the wrong password while logging onto an OS X machine, you’ll notice that the login window wiggles back and forth a few times. In fact, it looks like the login window is shaking its head “NO!” at you. This week we’ll look at how to implement this functionality with REALbasic. It turns out that it’s very simple to accomplish, so let’s get started.

Build the Interface

Launch REALbasic and open the default Window1. To this window, add a PushButton and a Timer. Set the Mode property of the Timer to 0 (zero). You can arrange the window however you wish.
Add the Code
Open the Code Editor for Window1 and add two properties to the window:

  • OrigX As Integer
  • Wiggle As Integer

Next, enter this code in the Action event of PushButton1:

Wiggle = 0
OrigX = Self.Left
Timer1.Period = 30
Timer1.Mode = 2

Finally, add this code to the Action event of Timer1:

If Wiggle > 5 Then
   Me.Mode = 0
   Self.Left = OrigX
Else
   If (Wiggle Mod 2) = 0 Then
      Self.Left = OrigX - 20
   Else
      Self.Left = OrigX + 20
   End If
   Wiggle = Wiggle + 1
End If

By clicking the PushButton, you reset some variables that keep track of the position of the window as well as the number of wiggles. Then, the timer starts firing. As the timer fires, it increases the wiggle counter and alternates the position of the window between its two positions. Once the counter exceeds 5, the timer stops. Simple, eh?

Conclusion

That’s all there is to it.

Download Wiggle Window REALbasic project

Originally published by ResExcellence
Reprinted with permission