Drawing Using Mouse Down Event in View

Discussion in 'Mac Programming' started by Blakeasd, Feb 14, 2011.

  1. Blakeasd macrumors 6502a

    Joined:
    Dec 29, 2009
    #1
    Hello.
    I am trying to draw into a view using the mouseDragged event. How can I go about doing this?
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Grab the mouse location relative to your view (e.g. [self convertPoint:[event locationInWindow] fromView:nil]) and store it, and redraw yourself (setNeedsDisplay:). Then in your drawRect: retrieve that point and draw it.
     
  3. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #3
    I understand how to get the mouse location. How do I draw it though?
     
  4. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #4
    Look at the documentation for NSBezierCurve, you might find something you can use in its methods.
     
  5. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #5
    NSBezierPath ;)
     
  6. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
  7. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #7
    Are you expecting to end up with a path-based image (drawing) or bitmap image (painting)?
     
  8. Blakeasd, Feb 15, 2011
    Last edited: Feb 15, 2011

    Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
  9. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #9
    If you want to "paint", you will be repeatedly applying a colored shape to the cursor location. Typically, you would use -mouseDown: to begin the application of the paint image and -mouseDragged: for repeated application (tracking the mouse). You would probably lock focus on a NSImage object, apply the "paint" to it, then tell the view to draw the part of the NSImage you just changed (this means the coördinate systems of the view and the image object have to line up properly for the drawing to show up right under the mouse.

    The brush will typically be a small shape, like an oval, rectangle or path outline defined in NSBezierPath. -stroke will outline the shape while -fill will paint it in. Use the method that seems appropriate for your needs (you could write the code, test it with one method, then test it with the other).

    Or you could use a NSImage to serve as your brush, drawing it into your target image the same way.
     
  10. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #10
    For my purposes I just need to draw very simple shapes. So I think a path-based image would be best, how do I achieve that?
     
  11. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #11
    Figure out how to create a NSBezierPath object in the shape you want, how to center it on the mouse location, how to apply it as needed to the target image and how to keep your view updated. Documentation is your friend.
     

Share This Page