Posted By

pgnatyuk on 09/17/10


Tagged

NSBezierPath


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

umang_nine
Tintenklecks
cnathan


Draw with NSBezierPath


 / Published in: Objective C
 

How to draw using NSBezierPath. Call these two methods from -drawRect.

  1. - (void) drawArrow
  2. {
  3. NSLog( @"state = %i", state );
  4.  
  5. // background
  6. NSRect bounds = [self bounds];
  7. [[NSColor orangeColor] set];
  8. NSRectFill( bounds );
  9.  
  10. // arrow
  11. CGFloat width = bounds.size.width;
  12. CGFloat height = bounds.size.height;
  13.  
  14. // no alloc and so no release in the end.
  15. NSBezierPath* path = [NSBezierPath bezierPath];
  16.  
  17. [path moveToPoint: NSMakePoint( width * 0.35, height * 0.1 )];
  18.  
  19. [path lineToPoint: NSMakePoint( width * 0.65, height * 0.1 )];
  20. [path lineToPoint: NSMakePoint( width * 0.65, height * 0.6 )];
  21. [path lineToPoint: NSMakePoint( width * 0.9, height * 0.6 )];
  22. [path lineToPoint: NSMakePoint( width * 0.5, height * 0.9 )];
  23. [path lineToPoint: NSMakePoint( width * 0.1, height * 0.6 )];
  24. [path lineToPoint: NSMakePoint( width * 0.35, height * 0.6 )];
  25.  
  26. [path closePath];
  27.  
  28. [[NSColor blueColor] set];
  29. [path fill];
  30.  
  31. [[NSColor whiteColor] set];
  32. [path stroke];
  33. }
  34.  
  35. - (void)drawHouse
  36. {
  37. NSLog( @"state = %i", state );
  38. NSRect bounds = [self bounds];
  39. [[NSColor yellowColor] set];
  40. NSRectFill( bounds );
  41.  
  42. CGFloat width = bounds.size.width;
  43. CGFloat height = bounds.size.height;
  44.  
  45. NSBezierPath *path = [NSBezierPath bezierPath];
  46.  
  47. [path moveToPoint: NSMakePoint( width * 0.1, height * 0.6 )];
  48.  
  49. [path lineToPoint: NSMakePoint( width * 0.1, height * 0.1 )];
  50. [path lineToPoint: NSMakePoint( width * 0.9, height * 0.1 )];
  51. [path lineToPoint: NSMakePoint( width * 0.9, height * 0.6 )];
  52. [path lineToPoint: NSMakePoint( width * 0.1, height * 0.6 )];
  53. [path lineToPoint: NSMakePoint( width * 0.5, height * 0.9 )];
  54. [path lineToPoint: NSMakePoint( width * 0.9, height * 0.6 )];
  55.  
  56. [path closePath];
  57.  
  58. [[NSColor whiteColor] set];
  59. [path fill];
  60.  
  61. [[NSColor blueColor] set];
  62. [path stroke];
  63. }
  64.  
  65.  
  66. - (void)drawRect:(NSRect)dirtyRect
  67. {
  68. [NSGraphicsContext saveGraphicsState];
  69.  
  70. //[self drawHouse];
  71. [self drawArrow];
  72.  
  73. [NSGraphicsContext restoreGraphicsState];
  74. }

Report this snippet  

You need to login to post a comment.