• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Why does this code not create a colored dot?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Prev by Date: Re: Why does this code not create a colored dot?
  • Next by Date: Re: Path names in executable
  • Previous by thread: Re: Why does this code not create a colored dot?
  • Next by thread: Re: Why does this code not create a colored dot?
  • Index(es):
    • Date
    • Thread