Draw a square on Mac OS X?
I would like to draw a small square, w开发者_开发问答hich is 4x4 pixels. I would like to be able to adjust its color at runtime (not like a color well or anything, the user doesn't need to be able to). I had been doing this with an NSImage
, but this uses up a lot of resources and I can't adjust, and making a different little color swatch in Photoshop to change it to for any new color is a huge pain, so it is no longer suitable. How can this be done on Mac OS X ?
The simplest way to draw a 4x4 square at coordinate 0,0 (bottom left) on Mac OS X:
NSRectFill(NSMakeRect(0.0, 0.0, 4.0, 4.0));
The simplest way to set the colour used for that sort of drawing:
[[NSColor colorWithCalibratedRed:... green:... blue:... alpha:1.0] set];
;)
[[NSColor redColor] set];
[NSBezierPath fillRect:NSMakeRect(0,0,4,4)];
Something like this. I haven't compiled it, but this is the basic approach to drawing arbitrary shapes on Mac with Quartz:
- (void)drawRect:(NSRect)rect {
[[NSColor redColor] set];
CGPathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, CGRectMake(0, 0, 4, 4));
CGContextStrokePath([NSGraphicsContext currentContext], path);
CGPathRelease(path);
}
精彩评论