2

on my timer app on mac os x I want to mark the areas of my clock with green, yellow, orange and red. See screenshot below. And I want to fill the elapsed time with transparent grey.

But, as you can see at the screenshot, only the arc segment is filled. But I want the whole sector to be filled. The only thing I can do is [thePath fill];

As always, I assume I´m doing something wrong. But what?

enter image description here


Call

[self drawTheArcWithColor:path1  :liveAngle1  :[NSColor greenColor ]  :lineTheWidth];

Method

- (void) drawTheArcWithColor:(NSBezierPath*) thePath :(CGFloat) angle :(NSColor*) theColor :(CGFloat) line {

    [thePath setLineWidth:line];
    [thePath appendBezierPathWithArcWithCenter:centerPoint  radius:2+circleHeight/2 startAngle:angle endAngle:angle+90.0f];

    [theColor setStroke];
    [[NSColor grayColor] setFill];
    [thePath fill];
    [thePath stroke];

}
IluTov
  • 6,807
  • 6
  • 41
  • 103
Ronald Hofmann
  • 1,390
  • 2
  • 15
  • 26
  • 1
    You should really embrace Objective-C method names... – Jonathan Grynspan Dec 28 '12 at 12:16
  • I don´t see what you mean? Is that the problem? – Ronald Hofmann Dec 28 '12 at 12:17
  • Can you show us how you create and fill the rest of the path? – HashtagMarkus Dec 28 '12 at 12:17
  • 1
    Your method is named `-drawTheArcWithColor::::`. It should be named `-drawArc:angle:color:lineWidth:` or similar. – Jonathan Grynspan Dec 28 '12 at 12:18
  • Apple documentation says that this is not mandatory. Why should I in such a short method? – Ronald Hofmann Dec 28 '12 at 12:20
  • @Zerd1984: the circle in the background is a graphic and fixed. The red digit moves like the second digit of a stop watch. – Ronald Hofmann Dec 28 '12 at 12:23
  • How do you create path1? so far I just can see that you create a path which is only the arc – HashtagMarkus Dec 28 '12 at 12:26
  • Well, I just initiate path1: NSBezierpath path1 = [NSBezierpath bezierPath]; otherwise I get a line from the existing path to the beginning of path1. I didn't find another way to create an arc. – Ronald Hofmann Dec 28 '12 at 12:38
  • With regard to naming, you *should* name it following convention for readability. If you want to get Objective-C help, a good first step is to not write code that looks foreign to people who can help you. If you don't want named parameters for a short procedure, this is a perfect opportunity to use a normal C function, like: `void drawArcWithColor(NSBezierPath *path, CGFloat angle, NSColor *color, CGFloat lineWidth)` – Quinn Taylor Dec 12 '14 at 00:19

1 Answers1

4

You need to start the path at the center point, then add the arc segment (implicitly adding a line from the center point to the start of the arc segment) and finally close the path (creating an implicit line from the end of the arc segment to the center point).

- (void) drawTheArcWithColor:(NSBezierPath*) thePath :(CGFloat) angle :(NSColor*) theColor :(CGFloat) line {

   [thePath setLineWidth:line];
   [thePath moveToPoint:centerPoint];
   [thePath appendBezierPathWithArcWithCenter:centerPoint  radius:2+circleHeight/2 startAngle:angle endAngle:angle+90.0f];
   [thePath closePath];

   [theColor setStroke];
   [[NSColor grayColor] setFill];
   [thePath fill];
   [thePath stroke];

}
Codo
  • 75,595
  • 17
  • 168
  • 206
  • Aha, I understand, I have to draw the sector. Because I just created the arc only the segment was filled :) Can I make the two lines from the center to the begin/end invisible? – Ronald Hofmann Dec 28 '12 at 12:45
  • Very good hint, thanks a lot. Any idea how to make the fill transparent? – Ronald Hofmann Dec 28 '12 at 13:27
  • For fill the path with a partially transparent color, use the *alpha* component of the color: `[[NSColor colorWithDeviceRed: 0.1f green: 1.0f blue: 0.1f alpha: 0.4f] setFill];`. – Codo Dec 28 '12 at 13:39
  • All problems solved, thanks a lot folks. Greetings from Switzerland, Ronald Hofmann. – Ronald Hofmann Dec 28 '12 at 14:17