Online Book Reader

Home Category

Cocoa Programming for Mac OS X - Aaron Hillegass [79]

By Root 885 0
context is active. When the focus is unlocked, the graphics context is no longer active. Whenever you issue drawing commands, they will be executed in the current graphics context.

You can use NSBezierPath to draw lines, circles, curves, and rectangles. You can use NSImage to create composite images on the view. In this example, you will fill the entire view with a green rectangle.

Open StretchView.m and add the following code to the drawRect: method:

- (void)drawRect:(NSRect)dirtyRect

{

NSRect bounds = [self bounds];

[[NSColor greenColor] set];

[NSBezierPath fillRect:bounds];

}

As shown in Figure 17.10, NSRect is a struct with two members: origin, which is an NSPoint, and size, which is an NSSize.

Figure 17.10. NSRect, NSSize, and NSPoint

NSSize is a struct with two members: width and height (both floats).

NSPoint is a struct with two members: x and y (both floats).

For performance reasons, structs are used in a few places instead of Objective-C classes. For completeness, here is the list of all the Cocoa structs that you are likely to use: NSSize, NSPoint, NSRect, NSRange, NSDecimal, and NSAffineTransformStruct. NSRange is used to define subranges. NSDecimal describes numbers with very specific precision and rounding behavior. NSAffineTransformStruct describes linear transformations of graphics.

Note that your view knows its dimensions as an NSRect called bounds. In this method, you fetched the bounds rectangle, set the current color to green, and filled the entire bounds rectangle with the current color.

The NSRect that is passed as an argument to the view is the region that is “dirty” and needs redrawing. It may be less than the entire view. If you are doing very time-consuming drawing, redrawing only the dirty rectangle may speed up your application considerably.

Note that setNeedsDisplay: will trigger the entire visible region of the view to be redrawn. If you wanted to be more precise about which part of the view needs redrawing, you would use setNeedsDisplayInRect: instead:

NSRect dirtyRect;

dirtyRect = NSMakeRect(0, 0, 50, 50);

[myView setNeedsDisplayInRect:dirtyRect];

Build and run your app. Try resizing the window.

Drawing with NSBezierPath


If you want to draw lines, ovals, curves, or polygons, you can use NSBezierPath. In this chapter, you have already used the NSBezierPath’s fillRect: class method to color your view. In this section, you will use NSBezierPath to draw lines connecting random points (Figure 17.11).

Figure 17.11. Completed Application

The first thing you will need is an instance variable to hold the instance of NSBezierPath. You will also create an instance method that returns a random point in the view. Open StretchView.h and make it look like this:

#import

@interface StretchView : NSView {

NSBezierPath *path;

}

- (NSPoint)randomPoint;

@end

In StretchView.m, you will override initWithFrame:. As the designated initializer for NSView, initWithFrame: will be called automatically when an instance of your view is created. In your version of initWithFrame:, you will create the path object and fill it with lines to random points. Make StretchView.m look like this:

#import "StretchView.h"

@implementation StretchView

- (id)initWithFrame:(NSRect)frame

{

self = [super initWithFrame:frame];

if (self) {

// Seed the random number generator

srandom((unsigned)time(NULL));

// Create a path object

path = [NSBezierPath bezierPath];

[path setLineWidth:3.0];

NSPoint p = [self randomPoint];

[path moveToPoint:p];

int i;

for (i = 0; i < 15; i++) {

p = [self randomPoint];

[path lineToPoint:p];

}

[path closePath];

}

return self;

}

// randomPoint returns a random point inside the view

- (NSPoint)randomPoint

{

NSPoint result;

NSRect r = [self bounds];

result.x = r.origin.x + random() % (int)r.size.width;

result.y = r.origin.y + random() % (int)r.size.height;

return result;

}

- (void)drawRect:(NSRect)rect

{

NSRect bounds = [self bounds];

// Fill the view with green

[[NSColor

Return Main Page Previous Page Next Page

®Online Book Reader