Drawing Basics


Cocoa and the Core Graphics APIs use slightly different models to handle drawing paths. Cocoa encapsulates a path into an object, an instance of NSBezierPath, that knows how to draw itself. The Core Graphics API, in contrast, relegates all drawing functionality to a CGContext.

Drawing with Quartz 2D

While the Core Graphics API set does include the CGPath abstract data type discussed in the previous chapter, instances of that class don't have any drawing behavior of their own. A CGPath instance must be copied into the current path of a CGContext before the computer can draw it. After copying the path into a context, the methods of the CGContext data type change the drawing parameters stored in the graphics state to affect the way the path is rendered. Finally, there are routines of the CGContext that stroke or fill the path using those parameters.

The interface of a CGContext includes several methods that accomplish the actual drawing. There are simple routines that simply stroke or fill the path. There are also routines that can stroke and fill a path in a single drawing operation. Regardless of the routine, however, the drawing operation will consume the current path and leave it empty.

In PostScript, stroking or filling a path also consumes the path. Unlike PostScript, however, Quartz (and PDF) do not save the current path as part of the graphics state in a contextafter you execute a drawing operation, the path is irrevocably lost. If you want to draw that same path again, you will have to reconstruct it using the context routines first (or recopy a CGPath object into the context).

The CGContext interface also contains a number of utility routines that can short-circuit the steps required to draw simple shapes in a context. For example, the routine CGContextFillRect creates a rectangular path and fills it, all in one operation. These convenience routines will also destroy the current path of the context.

Drawing with NSBezierPath

An NSBezierPath is a bit more sophisticated than a CGPath. NSBezierPath objects know how to draw themselves. To stroke or fill an NSBezierPath you call the appropriate methods of the object itself. The path will be drawn in the current NSGraphicsContext. Most of the drawing parameters that manipulate the appearance of the stroke and the fill are also part of the NSBezierPath API. Moreover, unlike drawing with the basic Core Graphics APIs, drawing an NSBezierPath does not consume the NSBezierPath object. You are free to redraw the same object many times.

Cocoa also provides a number of functions that draw simple shapes into the current graphics context. For example, the NSFrameRect family of routines draws rectangles in the current NSGraphicsContext. You must be careful, however. NSFrameRect does not draw a rectangle the same way that creating a rectangular path and stroking the rectangle would. This routine draws a rectangle that is immediately inside the rectangle you pass it, rather than having the stroke straddle the edge as you might expect. See the NSFrameRect documentation for more information.




Quartz 2D Graphics for Mac OS X Developers
Quartz 2D Graphics for Mac OS X Developers
ISBN: 0321336631
EAN: 2147483647
Year: 2006
Pages: 100

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net