Forum Navigation
Forum breadcrumbs - You are here:WeLoveGod RallysPublic Forums: fbcocoaTutorial_4
You need to log in to create posts and topics.

Tutorial_4

Posted by: svanvoorst <svanvoorst@...>

Sometimes you need to draw.  In Cocoa, this is done inside of an NSView using the -drawRect method as shown by this demo.  Normally we would have to list our methods inside of BeginCDeclaration...EndC, but in this case they are called automatically so this is not necessary.  The three methods called automatically for us are: -initWithFrame, -drawRect, and -isFlipped.  We could leave out -initWithFrame if we wanted to, but sometimes initializing data needs to be provided and this is where it would be stored.  I left it in for completeness, but the demo should run ok with it.  -drawRect is where all the action is and provides the drawing code.  I used calls for text and basic shapes from the NSBezierPath class.  These are similar to the calls used in Core Graphics.  It is also possible to create a CGContext inside -drawRect and use CG calls like we do in Carbon.  -isFlipped is important because it controls the location of the origin.  With Cocoa the origin is at the bottom left just like we learned in math class when we started creating graphs.  However, a lot of us got used to having the origin at the top left because that's where the early computers put it.  If you want to draw from the top down, then set -isFlipped to return YES.  If you want to draw from the bottom up, then set -isFlipped to return NO or omit the method.  By default Cocoa will draw from the bottom up if you don't tell it otherwise.  The next demo will show you how to print your masterpiece.

'------ start -----

BeginCDeclaration
@interface DrawView : NSView
//methods automatically called
@end  //interface
EndC
BeginCFunction
@implementation DrawView : NSView;
- (id)initWithFrame:(NSRect)frameRect
{
 if ((self = [super initWithFrame:frameRect]) != nil) 
 {
  self = [super initWithFrame:frameRect];
  // Add initialization code here
 }
 return self;
}
- (void)drawRect:(NSRect)rect
{
// ****** Background ***** //
[[NSColor whiteColor] set];
[NSBezierPath fillRect:rect];
// ****** Attributed text ****** //
NSString *myStr;
NSPoint myPt;
NSMutableDictionary *attr = [NSMutableDictionary dictionary];
[attr setObject:[NSFont fontWithName:@"Lucida Grande" size:18] forKey:NSFontAttributeName];
myStr = @"Attributed Text Drawn in an NSView";
myPt = NSMakePoint(30,20);
[myStr drawAtPoint:myPt withAttributes:attr];
[NSBezierPath setDefaultLineWidth:3.0];
// ****** Line ****** //
NSPoint begin, end;
begin = NSMakePoint( 30, 50 );
end = NSMakePoint( 360, 50 );
[NSBezierPath strokeLineFromPoint: begin toPoint:end];
// ***** Hashed Line ****** //
float lineDash[1];
NSBezierPath *hash = [NSBezierPath bezierPath];
[hash moveToPoint:NSMakePoint( 30, 70 )];
[hash lineToPoint:NSMakePoint( 360, 70 )];
lineDash[0] = 10;  //dash
lineDash[1] = 4;   //gap
[hash setLineDash:lineDash count: 2 phase:0.0 ];
[hash stroke];
// ****** Rounded Rectangle ****** //
NSRect  R = NSMakeRect(100, 100, 100, 100);
[[NSColor greenColor] set];
NSBezierPath *parr = [NSBezierPath bezierPath];
[parr appendBezierPathWithRoundedRect: R xRadius: 10.0 yRadius: 10.0 ];
[parr stroke];
// ****** Circle ****** // 
NSRect dotR = NSMakeRect( rect.size.width/2 - 50, rect.size.height/2 - 80, 100, 100 );
[[NSColor redColor] set]; 
[[NSBezierPath bezierPathWithOvalInRect:dotR] fill];
// ****** Oval ****** //
NSRect ovalR = NSMakeRect( 100, 230, 200, 100 );
[[NSColor magentaColor] set]; 
[[NSBezierPath bezierPathWithOvalInRect:ovalR] fill];
// ***** Polygon ****** //
[[NSColor blackColor] set]; 
NSBezierPath *path = [NSBezierPath bezierPath];
[path moveToPoint: NSMakePoint( 220.0, 150.0 )];
[path lineToPoint: NSMakePoint( 245.0, 175.0 )];
[path lineToPoint: NSMakePoint( 270.0, 150.0 )];
[path lineToPoint: NSMakePoint( 295.0, 150.0 )];
[path lineToPoint: NSMakePoint( 250.0, 120.0 )];
[path lineToPoint: NSMakePoint( 220.0, 150.0 )];
[path closePath];
[path setLineJoinStyle: NSRoundLineJoinStyle];
[path stroke];
NSRect outer = NSMakeRect( 80, 310, 100, 50 );
NSRect inner = NSInsetRect( outer, 1, 1 );
// ****** Filled Rectangle ***** //
[[NSColor blueColor] set];
[NSBezierPath fillRect:inner];
// ****** Stroked Rectangle ****** //
[[NSColor grayColor] set];
[NSBezierPath strokeRect:outer];
// ******Arc ******* //
[[NSColor blackColor] set]; 
NSBezierPath *arc = [NSBezierPath bezierPath];
[arc moveToPoint:NSMakePoint( 200, 320 )];
[arc appendBezierPathWithArcFromPoint:NSMakePoint( 200, 380 ) toPoint:NSMakePoint( 260, 380 ) radius:60];
[arc stroke];
}
// ----- Use this if you want 0,0 (origin) to be top, left ---- //
// ----- Otherwise origin will be at bottom, left (Unflipped) ----- //
-(BOOL)isFlipped
{
 return YES;
}
@end //implementation
EndC
BeginCCode
{
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init]; 
 [NSApplication sharedApplication];
 [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
// ----- Menubar and Menu ---- //
    id menubar = [[NSMenu new] autorelease];
    id appMenuItem = [[NSMenuItem new] autorelease];
    [menubar addItem:appMenuItem];
    [NSApp setMainMenu:menubar];
    id appMenu = [[NSMenu new] autorelease];
    id quitMenuItem = [[[NSMenuItem alloc] initWithTitle:@"Quit"
        action:@selector(terminate:) keyEquivalent:@"q"] autorelease];
    [appMenu addItem:quitMenuItem];
    [appMenuItem setSubmenu:appMenu];
//----- Window ---- //
   #define _wndW  440
   #define _wndH  500
   id window = [ [ NSWindow alloc ] 
                       initWithContentRect:NSMakeRect( 0, 0, _wndW, _wndH )
                       styleMask: NSClosableWindowMask | NSTitledWindowMask
                       backing:NSBackingStoreBuffered
                       defer:NO];
  [window center];
  [window setTitle: @"NSView_demo" ];
  [window makeKeyAndOrderFront:nil]; 
// ------- Create an instance of DrawView (NSView) ------ //
 id myDrawView = [[DrawView alloc]initWithFrame:NSMakeRect( 20, 20, _wndW - 40, _wndH - 40 )];
 [[window contentView] addSubview:myDrawView];
 [myDrawView release];
[NSApp activateIgnoringOtherApps:YES];
[NSApp run];
[pool drain];
}
EndC

'----- end ------

Steve Van Voorst


To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]