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

Mac OSX specific forum
User avatar
glomph
User
User
Posts: 48
Joined: Tue Apr 27, 2010 1:43 am
Location: St. Elsewhere / Germany
Contact:

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

Post 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
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

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

Post by IdeasVacuum »

IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
glomph
User
User
Posts: 48
Joined: Tue Apr 27, 2010 1:43 am
Location: St. Elsewhere / Germany
Contact:

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

Post 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
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3943
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

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

Post 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:
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
glomph
User
User
Posts: 48
Joined: Tue Apr 27, 2010 1:43 am
Location: St. Elsewhere / Germany
Contact:

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

Post by glomph »

Hi Wilbert,
thanks for that!
I hoped for for a snippet like that,
now i would get through.
:D

Sincerely
glomph
Post Reply