2009
11.20

Today I was tinkering with Cocoa, and drew a simple rectangle then added a small arrow head to it.

Cocoa Simple Drawing!

Cocoa Simple Drawing!



And here’s the code :

#import <cocoa /Cocoa.h>
@interface SimpleCustomView : NSView {
}
@end
</cocoa>
#import "SimpleCustomView.h"
 
@implementation SimpleCustomView
 
/* Method to create a shadow for our drawing */
-(NSShadow *)ourShadow {
	NSShadow *shadow = [[NSShadow alloc] init];
	[shadow setShadowColor: [NSColor colorWithCalibratedWhite:0.660 alpha:1.000]];
	[shadow setShadowBlurRadius: 3];
	[shadow setShadowOffset: NSMakeSize( 0, 0)];	
	return [shadow autorelease];
}
 
/* All drawing of NSView happens in this method */
- (void) drawRect:(NSRect)dirtyRect {
	/* Get our position and height */
	NSRect cords = [self bounds];
 
	/* Setup colors we will be using */
	NSColor *borderColor = [[NSColor colorWithCalibratedWhite:.00 alpha:1.0] retain];
	NSColor *startingColor = [[NSColor colorWithCalibratedRed:0.960 green:0.961 blue:0.960 alpha:1.000] retain];
	NSColor *endColor = [[NSColor colorWithCalibratedRed:0.892 green:0.893 blue:0.892 alpha:1.000] retain];	
	NSGradient *gradient = [[NSGradient alloc] initWithStartingColor:startingColor endingColor:endColor];
 
	/* Setup the normal rectable points */
	NSPoint bottomLeft = NSMakePoint(cords.origin.x + 20, cords.origin.y + 5);
	NSPoint topLeft = NSMakePoint(cords.origin.x + 20, cords.size.height - 5);
	NSPoint topRight = NSMakePoint(cords.size.width - 5, cords.size.height - 5);
	NSPoint bottomRight = NSMakePoint(cords.size.width - 5, cords.origin.y + 5);
 
	/* Setup the arrow points */
	NSPoint arrowBottom = NSMakePoint(cords.origin.x + 20, cords.size.height/2 - 10);
	NSPoint arrowTip = NSMakePoint(cords.origin.x + 5, cords.size.height/2);
	NSPoint arrowTop = NSMakePoint(cords.origin.x + 20, cords.size.height/2 + 10);
 
	/* Setup default NSBezierPath properties */
	[NSBezierPath setDefaultLineJoinStyle:NSRoundLineJoinStyle];
	[NSBezierPath setDefaultLineCapStyle:NSRoundLineCapStyle];
 
	/* Create a new path and draw the rectangle */
	NSBezierPath *path = [[NSBezierPath alloc] init];
	[path setLineWidth: 1.0f];
	[path setMiterLimit:0];
 
	/* Start drawing */
	[path moveToPoint:bottomLeft];
	[path lineToPoint:arrowBottom];
	[path lineToPoint:arrowTip];
	[path lineToPoint:arrowTop];
	[path lineToPoint:topLeft];
	[path lineToPoint:topRight];
	[path lineToPoint:bottomRight];
	[path closePath];
 
	/* Setup the drop shadow for our drawing */	
	[[self ourShadow] set];
 
	/* Setup the border color */
	[borderColor set];
 
	/* Draw the border */
	[path stroke];
 
	/* Fill the area with our gradient */
	[gradient drawInBezierPath:path angle:-90];
}
 
@end

No Comment.

Add Your Comment