Dumb drawing mistake somewhere...

  • 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.
  • On 27/06/2012, at 5:42 AM, William Squires wrote:

    > [theShapePath moveToPoint:self.upperLeft];
    > [theShapePath lineToPoint:self.upperLeft];

    This path is a point, it has zero length, it won't draw anything (or possibly just a dot).

    What did you expect it to draw?

    --Graham
previous month june 2012 next month
MTWTFSS
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30  
Go to today