Page 1 of 1

Cocoa: how to do Line, Circle, Dash & Arrow?

Posted: Thu May 28, 2015 12:19 am
by glomph
Hello,
not so experienced with cocoa
i need little help to draw in Cocoa :

a line , with thickness, dash and arrow
and a circle
even plot a point.

Sincerely
Glomph

Re: Cocoa: how to do Line, Circle, Dash & Arrow?

Posted: Thu May 28, 2015 12:25 am
by IdeasVacuum

Re: Cocoa: how to do Line, Circle, Dash & Arrow?

Posted: Thu May 28, 2015 1:08 am
by glomph
Hi IdeasVacuum,

thanks for the hint, but that stuff i use since years,
it is a bit slow.

I found out that cocoa can do this much faster and in better quality.
A line in cocoa i got out of Wilberts code ([PB Cocoa] Methods, Tips & Tricks)
about a filled roundbox.
This the rest i am stuck.

Sincerely
glomph

Re: Cocoa: how to do Line, Circle, Dash & Arrow?

Posted: Thu May 28, 2015 5:51 am
by wilbert
You can Google for "NSBezierPath Class Reference" .
It's the document from Apple describing all methods.
Here's a modified example with line width and dash

Code: Select all

Dim LineDashPattern.CGFloat(3)
LineDashPattern(0) = 9
LineDashPattern(1) = 6
LineDashPattern(2) = 3
LineDashPattern(3) = 6

LineWidth.CGFloat = 3
CocoaMessage(0, 0, "NSBezierPath setDefaultLineWidth:@", @LineWidth)

; *** Create image and draw onto it ***

CreateImage(0, 300, 200, 32, #PB_Image_Transparent)

StartDrawing(ImageOutput(0))

Crayons = CocoaMessage(0, CocoaMessage(0, 0, "NSColorList colorListNamed:$", @"Crayons"), "retain")

ColorGreen = CocoaMessage(0, 0, "NSColor greenColor")
ColorBrown = CocoaMessage(0, 0, "NSColor brownColor")
ColorMocha = CocoaMessage(0, Crayons, "colorWithKey:$", @"Mocha")
CocoaMessage(0, ColorMocha, "setStroke"); set stroke color to Mocha

Gradient = CocoaMessage(0, 0, "NSGradient alloc"); create gradient from green to brown
CocoaMessage(@Gradient, Gradient, "initWithStartingColor:", ColorGreen, "endingColor:", ColorBrown)
CocoaMessage(0, Gradient, "autorelease") 
GradientAngle.CGFloat = 315

Rect.NSRect
Rect\origin\x = 5
Rect\origin\y = 5
Rect\size\width = 290
Rect\size\height = 190

RadiusX.CGFloat = 20
RadiusY.CGFloat = 20

Path = CocoaMessage(0, 0, "NSBezierPath bezierPathWithRoundedRect:@", @Rect, "xRadius:@", @RadiusX, "yRadius:@", @RadiusY)
Phase.CGFloat = 0
CocoaMessage(0, Path, "setLineDash:", @LineDashPattern(), "count:", 4, "phase:@", @Phase)

CocoaMessage(0, Gradient, "drawInBezierPath:", Path, "angle:@", @GradientAngle)
CocoaMessage(0, Path, "stroke")

StopDrawing()


; *** Show the result ***

If OpenWindow(0, 0, 0, 320, 220, "Drawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  ImageGadget(0, 10, 10, 300, 200, ImageID(0))
  
  Repeat
    Event = WaitWindowEvent()
  Until Event = #PB_Event_CloseWindow
  
EndIf
To draw a circle you need bezierPathWithOvalInRect: or appendBezierPathWithOvalInRect:

Re: Cocoa: how to do Line, Circle, Dash & Arrow?

Posted: Thu May 28, 2015 6:18 am
by glomph
Hi Wilbert,
thanks for that!
I hoped for for a snippet like that,
now i would get through.
:D

Sincerely
glomph