Re: Drawing onto NSImageView subclass (Newbie)
Re: Drawing onto NSImageView subclass (Newbie)
- Subject: Re: Drawing onto NSImageView subclass (Newbie)
- From: Jens Bauer <email@hidden>
- Date: Tue, 10 Feb 2004 02:52:20 +0100
Hi Alan,
You're only drawing one point in your drawing code.
Why you see more than one point, when you specify an image, is because
the image erases the point first.
You should remember all the points you want to draw, in some kind of
buffer.
You could use an array of points (you can't just add a NSPoint to a
NSArray, because NSPoint is not a subclass of NSObject, it's just a
plain structure).
This would probably work, but would be slow if you're going to add many
points:
@interface MyPoint : NSObject
{
NSPoint pt;
}
- (id)initWithPoint:(NSPoint *)aPoint;
- (id)init;
- (NSPoint)point;
- (void)setPoint:(NSPoint)aPoint;
@end
@implementation MyPoint
- (id)initWithPoint:(NSPoint *)aPoint
{
self = [super init];
if(self)
{
point = aPoint;
}
return(self);
}
- (id)init
{
return([self initWithPoint:NSMakePoint(0.0, 0.0)];
}
- (NSPoint)point
{
return(point);
}
- (void)setPoint:(NSPoint)aPoint
{
point = aPoint;
}
@end
There's a more complicated way, which is *way* faster, and uses less
memory. Let me know, if this isn't good enough, and I'll spend some
time on coming up with a solution.
...So in short: Create a NSMutableArray in your -initWithFrame for your
view...
pointArray = [[NSMutableArray array] retain];
...and in your mouseDown, you add the point by...
pt = [[MyPoint alloc] initWithPoint:center];
[pointArray addObject:pt];
...in your drawRect, you could iterate through all these points and
draw each of them by...
count = [pointArray count];
for(i = 0; i < count; i++)
{
[self addPoint:[[pointArray objectAtIndex] point]];
}
...and remember in your dealloc...
[pointArray release];
...That should do it.
On Tuesday, Feb 10, 2004, at 01:53 Europe/Copenhagen, Alan Cook wrote:
Show us your code for mouseDown: and drawRect:
Here it is. Some of the code can be moved around without any effect
like the [path fill] in drawRect:, but since that is actually drawing
it, it seemed that drawRect: would be the acceptable place to put it.
I also included the code for addPoint: (poorly named).
{snip}
Love,
Jens
_______________________________________________
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.