evdude100
evdude100

Reputation: 437

Objective-C Drawing on a separate window

The following code is supposed to draw a rectangle on the mapWindow NSView. There is another file for my program that uses the NSView window; hence why I want to have a new window. However, the rectangle does not display. Any help would be appreciated.

@interface mapWindow : NSView {@private NSView* theMapWindow;}

- (void)drawRect:(int)pointx: (int)pointy;

@property (assign) IBOutlet NSView* theMapWindow;

@end

@implementation mapWindow
@synthesize theMapWindow;
- (void)mouseDown:(NSEvent *)event 
{
    NSPoint point = [event locationInWindow];
    //NSLog( @"mouseDown location: (%f,%f)", (float) point.x, (float) point.y);
    [self drawRect:point.x:point.y];
}

- (void)drawRect:(int)pointx: (int)pointy
{
    NSLog(@"Drawing point at (%d, %d)",pointx, pointy);
    NSPoint origin = { pointx,pointy };

    NSRect rect;
    rect.origin = origin;
    rect.size.width  = 128;
    rect.size.height = 128;

    NSBezierPath * path;
    path = [NSBezierPath bezierPathWithRect:rect];

    [path setLineWidth:4];

    [[NSColor whiteColor] set];
    [path fill];

    [[NSColor grayColor] set]; 
    [path stroke];

    [theMapWindow setNeedsDisplayInRect:rect];
}

Upvotes: 0

Views: 465

Answers (3)

NSGod
NSGod

Reputation: 22948

NSView's drawRect: method is called on your behalf; you should use it to your drawing, as described in Drawing View Content.

@interface mapWindow : NSView {

@private
   NSView* theMapWindow;
   NSPoint drawPoint;
}

// - (void)drawRect:(int)pointx: (int)pointy;

@property (assign) IBOutlet NSView* theMapWindow;
@property (assign) NSPoint drawPoint;

@end

--

@implementation mapWindow

@synthesize theMapWindow, drawPoint;

- (void)mouseDown:(NSEvent *)event 
{
    NSPoint point = [event locationInWindow];
    NSLog(@"[%@ %@] mouseDown location == %@",
              NSStringFromClass([self class]),
              NSStringFromSelector(_cmd),
              NSStringFromPoint(point));

    [self setDrawPoint:point];

    [self setNeedsDisplay:YES];

    //NSLog( @"mouseDown location: (%f,%f)", (float) point.x, (float) point.y);
    //[self drawRect:point.x:point.y];
}

- (void)drawRect:(NSRect)frame {
    NSLog(@"Drawing point at %@", NSStringFromPoint(drawPoint));
    NSRect drawFrame = NSMakeRect(point.x, point.y, 128.0, 128.0);
    [NSBezierPath setDefaultLineWidth:4];
    [[NSColor whiteColor] set];
    [NSBezierPath fillRect:drawFrame];
    [[NSColor grayColor] set]; 
    [NSBezierPath strokeRect:drawFrame];
}

Upvotes: 1

Grady Player
Grady Player

Reputation: 14549

your drawRect:: is custom method, and not the same as drawRect:, it will not be called when you have your view as the current context, try throwing a [self lockFocus],[self unlockFocus] around your drawing code.

Upvotes: 1

Daniel
Daniel

Reputation: 31559

You doing it wrong. you shouldn't call drawRect yourself, it will be called for you. replace the call of drawRect with setNeedsDisplayInRect and remove setNeedsDisplayInRect from the drawRect method.

Upvotes: 2

Related Questions