Re: scribbling
Re: scribbling
- Subject: Re: scribbling
- From: Richard Chamberlain <email@hidden>
- Date: Wed, 13 Aug 2003 23:17:09 +0100
Hi John,
On Wednesday, August 13, 2003, at 07:56 pm, John Randolph wrote:
First suggestion: don't do any drawing in an event-handling method,
and don't call -lockFocus yourself. Just update the info you need to
do the drawing, and call -setNeedsDisplayInRect:.
Second suggestion: don't throw away the path every time. You can
empty a path by sending it a -removeAllPoints message.
Third suggestion: check out the "cropped image" sample at:
http://developer.apple.com/samplecode/Sample_Code/Cocoa/
Cropped_Image.htm
Look at how the "LassoStyleCropMarker" was implemented.
Thanks for the suggestions.
I tried the lockFocus way and used [[self window] flushWindow]; which
worked fine and achieved the effect I was intending.
Given your suggestion though I tried another way.
So basically on mouseDown I store the NSPoint and in mouseDragged
create a new NSBezierPath between the original point and the new one. I
add the NSBezierPath to a NSMutableArray. This is pretty much how I had
it previously. However I now use setNeedsDisplayInRect and in drawRect
I use NSIntersectsRect to draw only the paths that need drawing. So I
don't throw away any paths I reuse them each time - which covers your
second suggestion I believe.
The problem with this is that compared to the lockFocus way the drawing
is not smooth with gaps between each NSBezierPath. The slower you move
the cursor the greater the space between each line is.
I don't really understand why this would be the case.
I've listed the code at the end of my mail in case I've missed
something.
Thanks,
Richard
--
- (void)mouseDown:(NSEvent *)event
{
lastPoint = [self convertPoint:[event locationInWindow]
fromView:nil];
NSLog(@"MouseDown x = %.2f, y = %.2f", lastPoint.x, lastPoint.y);
}
- (void)mouseDragged:(NSEvent *)event
{
NSPoint currentPoint = [self convertPoint:[event locationInWindow]
fromView:nil];
NSBezierPath* path;
path = [[NSBezierPath alloc] init];
[path setLineWidth: 2.0];
[path moveToPoint:lastPoint];
[path lineToPoint: currentPoint];
[path closePath];
[lines addObject:path];
lastPoint = currentPoint;
[self setNeedsDisplayInRect: [path bounds]];
[path release];
}
- (void)drawRect:(NSRect)rect
{
int i, count = 0;
NSBezierPath* path;
[[NSColor redColor] set];
for (i = 0; i < [lines count]; i++) {
path = [lines objectAtIndex:i];
if (NSIntersectsRect([path bounds], rect)) {
[path stroke];
count++;
}
}
NSLog(@"Lines drawn %d from a total of %d", count, i);
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.