开发者

First Quartz 2D program giving errors... What's the mistake?

开发者 https://www.devze.com 2023-02-16 07:55 出处:网络
I am trying to make my first Quartz 2D app for MAC. I just have my app delegate and a myView class which is as below -

I am trying to make my first Quartz 2D app for MAC. I just have my app delegate and a myView class which is as below -

mydrawAppDelegate.h --

#import <Cocoa/Cocoa.h>

@class myView;

@interface mydrawAppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;

myView* view;
}

@property (assign) IBOutlet NSWindow *window;

@end

mydrawAppDelegate.m -

#import "mydrawAppDelegate.h"
#import "myView.h"

@implementation开发者_如何学C mydrawAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application 

view = [[myView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 480.0) ];
[[self.window contentView] addSubview:view];
}   
@end

myView.h -

#import <Cocoa/Cocoa.h>
#import <Quartz/Quartz.h>
#import <AppKit/AppKit.h>

@interface myView : NSView {

}

@end

myView.m -

#import "myView.h"

@implementation myView

- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
    // Initialization code here.

    NSLog(@"initwithframeeeeeee");
}
return self;
}

- (void)drawRect:(NSRect)dirtyRect {
  // Drawing code here.
NSLog(@"drawrect called");
CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];

CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [NSColor redColor]);
CGContextMoveToPoint(context, 100.0f, 100.0f);
CGContextAddLineToPoint(context, 300.0f, 300.0f);
CGContextStrokePath(context);
}

@end

It builds but gives "EXC_BAD_ACCESS" while running. And a compile time warning: "passing argument 2 of 'CGContextSetStrokeColorWithColor' from incompatible pointer type "

Also I am not using any nib file. Do we use nib file when we draw using quartz or not? Kindly help. Thanks...


Got the problem. NSColor cannot be directly converted to CGColor. commented the line

     CGContextSetStrokeColorWithColor(context, [NSColor redColor]);

and it works fine. Further changed color to red color using cgcreatecolor. :)

0

精彩评论

暂无评论...
验证码 换一张
取 消