On 27/02/2008, at 11:05 AM, Jamie Phelps wrote:

Hey, guys. I'm having what I assume is a simple problem. [NSBezierPath fillRect:[self bounds]]; fills my NSView subclass fine the first time drawRect: is called, but breaks the frame on redraw. Here is some more code for context if needed. I tried adding the initial background fill to an awakeFromNib: method with no success. If anyone can offer thoughts on where I'm going wrong I'd appreciate it.


There are several major problems with your code. You need to go back and read the Cocoa Drawing Guide and make sure you understand it. You should probably also have a look at the Sketch example project.

You cannot call -setNeedsDisplay: in your -drawRect: method. -drawRect is called automatically at the end of the event loop if the view is marked as needing display and setting -setNeedsDisplay: in the drawRect makes no sense at all.

In your -mouseUp: method, you are directly calling -drawRect: without locking focus on anything - this is not permitted and will cause massive problems. You should never call -drawRect: directly, just call -setNeedsDisplay:YES and -drawRect: will be called automatically.

I also don't see why you're calling -setNeedsDisplay on mouse down, since there is no change in what is to be drawn.

You also don't initialize your ovalBounds NSRect or test to see whether it is valid, which would be good practice.

I have made a few changes to your code, including using -mouseDragged: to handle the oval drawing instead of -mouseUp: and I think it achieves what you are probably looking for.

@implementation DrawingView
- (id)initWithFrame:(NSRect)rect{
        self = [super initWithFrame:rect];
        [NSBezierPath setDefaultLineWidth:5.0];
        //initialize the ovalBounds
        ovalBounds=NSZeroRect;
        return self;
}
- (void)drawRect:(NSRect)rect{
        [[NSColor blueColor] set];
        [NSBezierPath fillRect:[self bounds]];
        //test to ensure that the oval is valid
        if(!NSEqualRects(NSZeroRect, ovalBounds))
        {
                path = [NSBezierPath bezierPathWithOvalInRect:ovalBounds];
                [[NSColor yellowColor] set];
                [path stroke];
                [[NSColor greenColor] set];
                [path fill];
        }
}
//in mouseDown we just set the origin point
- (void)mouseDown:(NSEvent *)event{
        NSPoint p = [event locationInWindow];
        downPoint = [self convertPoint:p
                                                  fromView:nil];
}

-(void) mouseDragged:(NSEvent*) event
{
        NSPoint p = [event locationInWindow];
        currentPoint = [self convertPoint:p
                                                  fromView:nil];
        float minX = MIN(downPoint.x, currentPoint.x);
        float maxX = MAX(downPoint.x, currentPoint.x);
        float minY = MIN(downPoint.y, currentPoint.y);
        float maxY = MAX(downPoint.y, currentPoint.y);
        ovalBounds = NSMakeRect(minX, minY, maxX-minX, maxY-minY);
        [self setNeedsDisplay:YES];
}
@end

--
Rob Keniger
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to