下面的代碼應該在mapWindow NSView上繪製一個矩形。我的程序還有另一個使用NSView窗口的文件;爲什麼我想要一個新窗口。但是,矩形不顯示。任何幫助,將不勝感激。Objective-C在單獨的窗口上繪圖
@interface mapWindow : NSView {@private NSView* theMapWindow;}
- (void)drawRect:(int)pointx: (int)pointy;
@property (assign) IBOutlet NSView* theMapWindow;
@end
@implementation mapWindow
@synthesize theMapWindow;
- (void)mouseDown:(NSEvent *)event
{
NSPoint point = [event locationInWindow];
//NSLog(@"mouseDown location: (%f,%f)", (float) point.x, (float) point.y);
[self drawRect:point.x:point.y];
}
- (void)drawRect:(int)pointx: (int)pointy
{
NSLog(@"Drawing point at (%d, %d)",pointx, pointy);
NSPoint origin = { pointx,pointy };
NSRect rect;
rect.origin = origin;
rect.size.width = 128;
rect.size.height = 128;
NSBezierPath * path;
path = [NSBezierPath bezierPathWithRect:rect];
[path setLineWidth:4];
[[NSColor whiteColor] set];
[path fill];
[[NSColor grayColor] set];
[path stroke];
[theMapWindow setNeedsDisplayInRect:rect];
}
point.x和point.y不能被調用,因爲它們不存在。 – evdude100 2011-06-05 13:26:37
但我做了類似的事情,謝謝!我用NSPoint origin = [self drawPoint]; – evdude100 2011-06-05 13:53:47