Professional Web Applications Themes

ObjC Nubie:Basic Drawing - Mac Programming

Let me first start off by stating the obvious: "Why yes; I am blind." as my question is all about basic drawing. I've been hitting all the doentation I can find about drawing into a view ( which methods to override are restated ~50 times or so ), the various NS*ImageRep's (of which there seems to be ~50 ), and NSBezierPath (of which there seems to be ~50 commands =) but at no point does the doentation state how to get what's drawn into the current view. I assume the use of NSBezierPath and NSGraphicsContext but all my little trials ...

  1. #1

    Default ObjC Nubie:Basic Drawing

    Let me first start off by stating the obvious: "Why yes; I am blind." as
    my question is all about basic drawing. I've been hitting all the
    doentation I can find about drawing into a view ( which methods to
    override are restated ~50 times or so ), the various NS*ImageRep's (of
    which there seems to be ~50 ), and NSBezierPath (of which there seems to
    be ~50 commands =) but at no point does the doentation state how to
    get what's drawn into the current view. I assume the use of NSBezierPath
    and NSGraphicsContext but all my little trials with the two (or
    separately) have done squat. I also assume (back to my opening
    statement) that this exact question is answered elsewhere but I have
    been able to find it.

    Please be gentle with any responses as I'm a Java bum (aka, used to a
    lot of hand-holding and oh yeah, overriding paint(Graphics)) who's
    trying to amend his ways :)

    Many thanks in advance.

    John Guest

  2. #2

    Default Re: ObjC Nubie:Basic Drawing

    In article <fK3Yb.72745$%nyroc.rr.com>,
    John Smith <net> wrote:
     

    There are two ways, the right way and the wrong way. I'll cover both. :)

    The right way is to create an NSView subclass. Override NSView's
    drawRect: method, and inside that method put all of your NSBezierPath,
    NSImage, etc. drawing commands. When the view needs to be updated, call
    [view setNeedsDisplay:YES], and the view will be redrawn next time your
    app hits the event loop.

    The wrong way is to call [view lockFocus], followed by drawing commands,
    followed by [view unlockFocus]. This may be more in line with
    environments you've used in the path. Don't do it! Everything is much
    easier if you follow the subclassing path I outlined above, but I'm
    covering this way just for completeness. :)
    Michael Guest

  3. #3

    Default Re: ObjC Nubie:Basic Drawing

    In article <mail-54C193.17525216022004localhost>,
    Michael Ash <com> wrote:
     

    And, as with all "there are two ways" statements, there's

    #3 Create an NSImage, call lockFocus on it, do your drawing, call
    unlockFocus, and then stash that image in an NSImageView using setImage.
    This has the advantage that it's easier, and the disadvantage that it is
    only for drawing of static images. If you want images to change quickly,
    or you want to do hit testing or mouse tracking, you're better off with
    an NSView subclass.

    Cheers,
    -- Uli
    http://www.zathras.de
    Uli Guest

  4. #4

    Default Re: ObjC Nubie:Basic Drawing

    John Smith <net> wrote:
     

    I'll assume this is your question?
     

    Well drawing in Cocoa is not unlike drawing in Java.

    What you do:

    1: subclass NSView: MyRedView
    2: override drawRect:(NSRect)rect ( ~ paint(Graphics) )
    3: set a color: [[NSColor redColor] set];
    4: draw a shape: NSRectFill([self bounds]);

    Note that all drawing you do inside drawRect goes into your view by
    default, you don't have to draw into a 'Graphics' context explicitly.

    To get this into your program:

    5: drag an NSView into your window from the IB palette.
    6: import (drag) MyRedView.h into your nib
    7: use the inspector to set the 'custom class' of your view to MyRedView

    Presto.

    Now you have a NSView that paints itself red, build on this.

    If this wasn't your question 'John', try to reformulate and a better
    answer may find it's way to you...

    Patrick
    Patrick Guest

  5. #5

    Default Re: ObjC Nubie:Basic Drawing

    THANK YOU BOTH!

    The straight override of NSView drawRect works! I pawed back through
    some of my earlier attempts and had tried that but for whatever reason
    it didn't seem to want to work but then I recoded it and now it's fine!
    I'll stick with getting one working and building up from there.

    Still can't get anything that involves lockFocus to work (at least not
    every time through) but I think I've got it :)

    Again: THANK YOU!
     [/ref]
    [ Message Truncated ]

    John Guest

Similar Threads

  1. Drawing in Flex? Drawing and AS for custom compon
    By yoway in forum Macromedia Flex General Discussion
    Replies: 0
    Last Post: April 18th, 10:19 PM
  2. Objc Nubie: Errors Errors Errors
    By John in forum Mac Programming
    Replies: 8
    Last Post: February 16th, 07:30 PM
  3. Using gcc/ObjC from shell, which libraries are needed?
    By David in forum Mac Programming
    Replies: 2
    Last Post: January 3rd, 07:29 PM
  4. Replies: 2
    Last Post: October 6th, 06:51 PM
  5. Python and ObjC
    By Wezzy in forum Mac Programming
    Replies: 3
    Last Post: September 16th, 05:48 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139