Re: Rects and points...
Re: Rects and points...
- Subject: Re: Rects and points...
- From: Tom Waters <email@hidden>
- Date: Tue, 20 Nov 2001 13:30:03 -0800
There are a lot of problems with your example. Several are fundamental
to how cocoa applications are supposed to render pixels on the screen.
It seems you are trying to do things in an "immediate mode", as in,
"draw these curves now!" Cocoa applications do things differently. You
want to create a subclass of NSView which overrides drawRect to do the
drawing you want, and then just "let things happen". drawRect will be
called automatically whenever the display needs to change. If you want
to cause an update, say when the user moves a slider, you just put a
[myView setNeedsDisplay:YES] in the IB action callback for the slider.
As far as the specifics of your attempt go:
is this a method on an NSView subclass?
if so, you don't want to have an IBAction method for drawing, you want
to override drawRect:
also, calling initWithFrame in the middle of your draw function is
wrong. initWithFrame will be called once by the runtime when your NIB
is loaded.
setNeedsDisplay is a method for telling the system to call drawRect...
it has no effect in your example.
Also, note that even if your example was able to get things to appear on
the screen, you're not creating a proper Bezier curve. They are cubic
curves and require four points to get the classic curve you expect.
From the postcript world: currentpoint, the curveto point and two
control points.
Here's my guess on an example that will work better for you. Just put a
custom view into a window in IB and edit its custom class attribute to
be a class called MyView, and have this in the implementation:
@interface MyView : NSView {}
@end
@implementation MyView
- (void)drawRect:(NSRect)rect
{
// Point definitions
NSPoint point_a = NSMakePoint(0.0, 0.0);
NSPoint point_b = NSMakePoint(50.0, 100.0);
NSPoint point_c = NSMakePoint(150.0,50.0);
NSPoint point_d = NSMakePoint(200.0,150.0);
NSBezierPath *curve = [NSBezierPath bezierPath];
[[NSColor blackColor] set];
[curve moveToPoint:point_a];
[curve lineToPoint:point_b];
[curve lineToPoint:point_c];
[curve lineToPoint:point_d];
[curve stroke];
[curve removeAllPoints];
[[NSColor redColor] set];
[curve moveToPoint:point_a];
[curve curveToPoint:point_d controlPoint1:point_b
controlPoint2:point_c];
[curve stroke];
}
@end
On Tuesday, November 20, 2001, at 07:16 AM, Riccardo Santato wrote:
Hi there.
I'd reaally like someone of you help me in making this code work.
I'm just having fun with Bezier curves, but nothing is displayed... Can
you tell me please where are the errors ?
Many thanx to everyone 4 your kindness
- (IBAction)draw:(id)sender
{
// Point definitions
point_a = NSMakePoint(0.0, 0.0);
point_b = NSMakePoint(100.0, 100.0);
point_c = NSMakePoint(50.0,50.0);
NSLog (@"points: defined");
// Rectangle definition
view_rect = [self bounds];
// View allocation
[self initWithFrame:view_rect];
// Initializations
[self setNeedsDisplay:YES];
curve = [NSBezierPath bezierPathWithRect:view_rect];
[self convertPoint:point_a toView:self];
[self convertPoint:point_b toView:self];
[self convertPoint:point_c toView:self];
[[NSColor blackColor] set];
[curve moveToPoint:point_a];
[curve lineToPoint:point_b];
[curve lineToPoint:point_c];
[curve curveToPoint:point_a controlPoint1:point_b
controlPoint2:point_c];
[self drawRect:view_rect];
[self displayRect:view_rect];
}
@end
--
Riccardo Santato
www.riccardosantato.com
_______________________________________________
cocoa-dev mailing list
email@hidden
http://www.lists.apple.com/mailman/listinfo/cocoa-dev