Dumb drawing mistake somewhere...
Dumb drawing mistake somewhere...
- Subject: Dumb drawing mistake somewhere...
- From: William Squires <email@hidden>
- Date: Tue, 26 Jun 2012 14:42:55 -0500
I've got 3 classes, ShapeView (NSView subclass), Shape (NSObject) and ShapeTest (the application delegate).
ShapeView.h
#import <Cocoa/Cocoa.h>
@class Shape;
@interface ShapeView : NSView
@property (nonatomic, retain) NSMutableArray *theShapes;
-(void)addShape:(Shape *)theShape;
@end
ShapeView.m
#import "ShapeView.h"
#import "Shape.h"
@implementation ShapeView
@synthesize theShapes = _theShapes;
#pragma mark - Memory Mangement
- (id)initWithFrame:(NSRect)frame
{
if (self = [super initWithFrame:frame])
{
// Initialization code here.
self.theShapes = [[NSMutableArray alloc] initWithCapacity:10];
}
return self;
}
-(void)dealloc
{
self.theShapes = nil;
[super dealloc];
}
#pragma mark - Drawing
- (void)drawRect:(NSRect)dirtyRect
{
// NSInteger shapeCount = [self.theShapes count];
// Drawing code here.
for (Shape *theShape in self.theShapes)
{
[theShape draw];
}
}
#pragma mark - Public API
-(void)addShape:(Shape *)theShape
{
// primitive implementation with no error checking - just insert it
[self.theShapes addObject:theShape];
[self setNeedsDisplay:YES];
}
@end
Shape.h
@interface Shape : NSObject
@property (nonatomic, assign) CGPoint upperLeft;
@property (nonatomic, retain) NSColor *fillColor;
@property (nonatomic, retain) NSColor *wireColor;
-(id)initWithUpperLeftX:(CGFloat)newX andY:(CGFloat)newY withWireColor:(NSColor *)theWireColor andFillColor:(NSColor *)theFillColor;
-(void)draw;
-(NSString *)description;
@end
Shape.m
#import "Shape.h"
@implementation Shape
@synthesize upperLeft = _upperLeft;
@synthesize fillColor = _fillColor;
@synthesize wireColor = _wireColor;
#pragma mark - Memory Management
/*
-- Designated Initializer
*/
-(id)initWithUpperLeftX:(CGFloat)newX andY:(CGFloat)newY withWireColor:(NSColor *)theWireColor andFillColor:(NSColor *)theFillColor
{
if (self = [super init])
{
_upperLeft.x = newX;
_upperLeft.y = newY;
self.wireColor = [NSColor blackColor];
self.fillColor = [NSColor whiteColor];
}
return self;
}
-(id)init
{
return [self initWithUpperLeftX:0.0 andY:0.0 withWireColor:[NSColor blackColor] andFillColor:[NSColor whiteColor]];
}
-(void)dealloc
{
self.wireColor = nil;
self.fillColor = nil;
[super dealloc];
}
-(void)draw
{
NSBezierPath *theShapePath = nil;
theShapePath = [[[NSBezierPath alloc] init] autorelease];
[theShapePath setLineWidth:1.0];
[self.wireColor set];
[theShapePath moveToPoint:self.upperLeft];
[theShapePath lineToPoint:self.upperLeft];
[theShapePath closePath];
[theShapePath stroke];
}
-(NSString *)description
{
return [NSString stringWithFormat:@"upperLeft={%8.2f,%8.2f}\n wireColor=%@, fillColor=%@",self.upperLeft.x,self.upperLeft.y,self.wireColor,self.fillColor];
}
@end
and, in my AppDelegate.m I have the following:
#import "AppDelegate.h"
#import "Shape.h"
#import "LineShape.h"
#import "ShapeView.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize customView = _customView;
- (void)dealloc
{
[super dealloc];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
ShapeView *myShapeView = (ShapeView *)self.customView;
// Insert code here to initialize your application
Shape *starPoint = [[Shape alloc] initWithUpperLeftX:5.0 andY:50.0 withWireColor:[NSColor blueColor] andFillColor:[NSColor whiteColor]];
[myShapeView addShape:starPoint];
LineShape *myLine = [[LineShape alloc] initWithUpperLeftX:10.0 andY:10.0 andLowerRightX:20.0 andY:20.0];
[myShapeView addShape:myLine];
}
@end
Nothing seems to be disconnected or nil, but no drawing is occuring. What am I missing here? I think the problem is how to color the stroked NSBezierPath, but I'm not sure.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden