Re: Why does this code not create a colored dot?
Re: Why does this code not create a colored dot?
- Subject: Re: Why does this code not create a colored dot?
- From: Erik Buck <email@hidden>
- Date: Tue, 4 Apr 2006 06:34:01 -0700 (PDT)
You have not read or not understood the fundamentals of drawing with Cocoa.
Here you create an NSView instance. Perhaps you wanted to create an NSImage instance ?
NSView *imageView = [[NSView alloc] initWithFrame:frame];
Then you create an NSBezierPath instance, set the color in the current grphics context, and fill the path in the current graphics context:
NSBezierPath *path = [NSBezierPath bezierPathWithOvalInRect:frame];
[color set]; //???
[path fill];
No ask yourself some questions:
What is the current graphics context when my code is executing ?
What is the concept od a current graphics context ?
Why am I confused ("//???") about a simple operation like setting the current color in the current graphics context ?
Where do I expect my filled path to be drawn ? Why would I expect it to be drawin in an arbitrary newly allocated object like imageView ?
You need to start here http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaDrawingGuide/Introduction/chapter_1_section_1.html
Or buy a nice book.
Particularly for newbies, all drawing within an NSView should be implemented within the implementation of the view's -drawRect: method. There are other approaches, but they are all inappropriate for non-experts.
In your case, I suggest the following typed in online and never compiled:
- (NSImage *)createImageWithColor:(NSColor *)color
{
NSRect aRect = NSMakeRect(0,0,11,11);
NSImage *resultImage = [[[NSImage alloc] initWithSize:aRect.size] autorelease];
NSBezierPath *path = [NSBezierPath bezierPathWithOvalInRect:aRect];
[resultImage lockFocus]; // Set the current graphics context to one for the image
[[NSColor clearColor] set]
[NSBezierPath fillRect:aRect]; // clear the background
[color set];
[path fill]; // fill the oval
[resultImage unlockFocus];
return resultImage;
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden