3

How i can draw a line in a specific window after a button click?

i'm using this:

NSBezierPath * path = [NSBezierPath bezierPath];
        [path setLineWidth: 4];

        NSPoint startPoint = {  21, 21 };
        NSPoint endPoint   = { 128,128 };
        [path  moveToPoint: startPoint];    
        [path lineToPoint:endPoint];

        [[NSColor redColor] set]; 
        [path stroke];

but it work only if i put it in the:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification

how i can solve this? my goal is to create an application that can draw lines , according to the details(coordinate) received

thank you.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
Corninos
  • 31
  • 1
  • 2
  • Where do you want to draw them (in what view)? I think you have to subclass NSView and put your drawing code in there. I'm not sure why it works without subclassing when you put it in the applicationWillFinishLaunching method. – rdelmar Apr 28 '12 at 00:29
  • ok, i'm drawing inside NSview class, and inside a custom view – Corninos Apr 28 '12 at 09:28

2 Answers2

2

You should not be drawing outside of a view or layer's drawing method (e.g. drawRect:). What you want to do, in broad strokes, is have a view there that draws the line when a flag is set, and when you click the button, set the flag and tell the view to redraw.

Chuck
  • 234,037
  • 30
  • 302
  • 389
2

When you click the mouse event. This code will create line, curve and draw.

  #import <Cocoa/Cocoa.h>


    @interface BezierView : NSView {
        NSPoint points[4];
        NSUInteger pointCount;
    }

    @end

    #import "BezierView.h"

    @implementation BezierView
    - (id)initWithFrame:(NSRect)frame {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code here.
        }
        return self;
    }

    - (void)drawRect:(NSRect)rect 
    {
        NSBezierPath *control1 = [NSBezierPath bezierPath];
        [control1 moveToPoint: points[0]];
        [control1 lineToPoint: points[1]];
        [[NSColor redColor] setStroke];
        [control1 setLineWidth: 2];
        [control1 stroke];

        NSBezierPath *control2 = [NSBezierPath bezierPath];
        [control2 moveToPoint: points[2]];
        [control2 lineToPoint: points[3]];
        [[NSColor greenColor] setStroke];
        [control2 setLineWidth: 2];
        [control2 stroke];

        NSBezierPath *curve = [NSBezierPath bezierPath];
        [curve moveToPoint: points[0]];
        [curve curveToPoint: points[3]
              controlPoint1: points[1]
              controlPoint2: points[2]];

        [[NSColor blackColor] setStroke];
        CGFloat pattern[] = {4, 2, 1, 2};
        [curve setLineDash: pattern
                     count: 4
                     phase: 1];
        [[NSColor grayColor] setFill];
        [curve fill];
        [curve stroke];
    }

    - (void)mouseDown: (NSEvent*)theEvent
    {
        NSPoint click = [self convertPoint: [theEvent locationInWindow]
                                  fromView: nil];
        points[pointCount++ % 4] = click;
        if (pointCount % 4 == 0)
        {
            [self setNeedsDisplay: YES];
        }
    }
    @end
Durul Dalkanat
  • 7,266
  • 4
  • 35
  • 36