Assumes "Automatic Reference Counting" (ARC) is enabled.
|
@interface Shape : NSObject
|
|
|
@property (assign) NSPoint center;
|
|
|
@property (strong) NSColor* color;
|
|
|
@end
|
|
|
@interface Rectangle : Shape
|
|
|
@property (assign) NSSize size;
|
|
|
@end
|
|
|
@interface Circle : Shape
|
|
|
@property (assign) CGFloat radius;
|
|
|
@end
|
|
@implementation Shape
|
|
|
@synthesize centre;
|
|
|
@synthesize color;
|
|
|
- (void)draw {
|
|
|
[color set];
|
|
|
}
|
|
|
@end
|
|
|
@implementation Rectangle
|
|
|
@synthesize size;
|
|
|
- (void)draw {
|
|
|
[super draw];
|
|
|
[NSBezierPath fillRect:NSMakeRect(self.centre.x - size.width / 2, self.centre.y - size.height / 2, size.width, size.height)];
|
|
|
}
|
|
|
@end
|
|
|
@implementation Circle
|
|
|
@synthesize radius;
|
|
|
- (void)draw {
|
|
|
[super draw];
|
|
|
[[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(self.centre.x - radius, self.centre.y - radius, radius2, radius2)] fill];
|
|
|
}
|
|
|
@end
|
|
- (void)drawRect:(NSRect)rect {
|
|
|
rect = [self bounds];
|
|
|
[[NSColor cyanColor] set];
|
|
|
[NSBezierPath fillRect:rect];
|
|
|
Rectangle* rectangle = [[Rectangle alloc] init];
|
|
|
rectangle.size = NSMakeSize(300, 200);
|
|
|
rectangle.centre = NSMakePoint(NSMidX(rect), NSMidY(rect));
|
|
|
rectangle.color = [NSColor whiteColor];
|
|
|
[rectangle draw];
|
|
|
Circle* circle = [[Circle alloc] init];
|
|
|
circle.radius = 60;
|
|
|
circle.centre = rectangle.centre;
|
|
|
circle.color = [NSColor redColor];
|
|
|
[circle draw];
|
|
|
}
|
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.