Re: Quartz 2D drawing problems
Re: Quartz 2D drawing problems
- Subject: Re: Quartz 2D drawing problems
- From: John Randolph <email@hidden>
- Date: Thu, 13 Nov 2003 17:38:33 -0800
On Nov 12, 2003, at 11:34 PM, Michael Schmidt wrote:
On Tue, 11 Nov 2003 14:23:32 -0800
John Randolph <email@hidden> wrote:
There's not enough information here for anyone to help you. Show us
the code.
Ok, I figured out a minimum example. I've done the following steps:
- created a new cocoa-application project in Xcode
- created a subclass of NSView
- put a custom view of this class into the app window
The drawRect function of my NSView subclass looks like this:
static float BlackColor[] = { 0.0, 0.0, 0.0, 1.0 };
static float BlueColor[] = { 0.4, 0.4, 0.8, 1.0 };
- (void)drawRect:(NSRect)rect
{
CGContextRef cg_ctx;
CGColorSpaceRef cg_space;
CGRect cg_rect;
cg_ctx = (CGContextRef)[[NSGraphicsContext currentContext]
graphicsPort];
cg_space = CGColorSpaceCreateDeviceRGB ();
CGContextSetFillColorSpace (cg_ctx, cg_space);
CGContextSetStrokeColorSpace (cg_ctx, cg_space);
// (1) stroke a line
CGContextSetLineWidth(cg_ctx, 1.0);
CGContextSetStrokeColor (cg_ctx, BlackColor);
CGContextBeginPath(cg_ctx);
CGContextMoveToPoint (cg_ctx, 104.5, 155.5);
CGContextAddLineToPoint(cg_ctx, 104.5, 164.5);
CGContextAddLineToPoint(cg_ctx, 120.5, 164.5);
CGContextAddLineToPoint(cg_ctx, 120.5, 173.5);
CGContextStrokePath(cg_ctx);
Well, the symptom seems to be that stroking the rectangle is causing a
closepath operation to happen, which includes the existing path.
Calling CGContextNewPath() before you draw the rectangle should fix
the problem, but it didn't, so I'll ask you to file a bug on this.
// (2) fill and stroke a rectangle
cg_rect.origin.x = 137.5;
cg_rect.origin.y = 169.5;
cg_rect.size.width = 21.0;
cg_rect.size.height = 7.0;
CGContextSetFillColor (cg_ctx, BlueColor);
CGContextSetStrokeColor (cg_ctx, BlueColor);
CGContextFillRect (cg_ctx, cg_rect);
CGContextStrokeRect (cg_ctx, cg_rect);
}
A screenshot of the window can be found here:
http://www.absint.com/cocoa/result.jpg
One can see a small blue line from the beginning to the end of the path
defined in (1). This line is drawn when the FillRect function is
called.
My system is an iBook late 2001 (no QE), OS X 10.3.1
When I re-did it with NSBezierPath and NSRectFill, the symptom went
away:
- (void)drawRect:(NSRect)rect
{
NSRect aRect = NSMakeRect(137.5, 169.5, 21.0, 7.0);
NSBezierPath *path = [NSBezierPath bezierPath];
[path moveToPoint:NSMakePoint(104.5, 155.5)];
[path lineToPoint:NSMakePoint(104.5, 164.5)];
[path lineToPoint:NSMakePoint(120.5, 164.5)];
[path lineToPoint:NSMakePoint(120.5, 173.5)];
[[NSColor blackColor] set];
[path stroke];
[[NSColor blueColor] set];
NSRectFill(aRect);
NSFrameRect(aRect);
}
It also worked if I reversed the order of the drawing, doing the
rectangle first, and the open path second.
Is there some reason why you need to do this at the CG level?
-jcr
John C. Randolph <email@hidden> (408) 974-8819
Sr. Cocoa Software Engineer,
Apple Worldwide Developer Relations
http://developer.apple.com/cocoa/index.html
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.