Regan
Regan

Reputation: 1497

Draw a square on Mac OS X?

I would like to draw a small square, which 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 ?

Upvotes: 3

Views: 3719

Answers (3)

Rob Napier
Rob Napier

Reputation: 299355

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);
}

Upvotes: 2

NSResponder
NSResponder

Reputation: 16861

[[NSColor redColor] set];
[NSBezierPath fillRect:NSMakeRect(0,0,4,4)];

Upvotes: 2

Jonathan Grynspan
Jonathan Grynspan

Reputation: 43472

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];

;)

Upvotes: 10

Related Questions