单独窗口上的Objective-C图纸
下面的代码应该在mapWindow NSView上绘制一个矩形。我的程序还有一个使用NSView窗口的文件;因此,我想有一个新的窗口。但是,不会显示矩形。任何帮助都将不胜感激单独窗口上的Objective-C图纸,objective-c,graphics,drawing,drawrect,Objective C,Graphics,Drawing,Drawrect,下面的代码应该在mapWindow NSView上绘制一个矩形。我的程序还有一个使用NSView窗口的文件;因此,我想有一个新的窗口。但是,不会显示矩形。任何帮助都将不胜感激 @interface mapWindow : NSView {@private NSView* theMapWindow;} - (void)drawRect:(int)pointx: (int)pointy; @property (assign) IBOutlet NSView* theMapWindow; @en
@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];
}
你做错了。您不应该自己调用
drawRect
,它会为您调用。用setNeedsDisplayInRect
替换drawRect
的调用,并从drawRect
方法中删除setNeedsDisplayInRect
。您做错了。您不应该自己调用drawRect
,它会为您调用。将drawRect
的调用替换为setNeedsDisplayInRect
并从drawRect
方法中删除setNeedsDisplayInRect
。您的drawRect::是自定义方法,与drawRect:,当您的视图作为当前上下文时,将不会调用它,请尝试抛出[self-lockFocus]
,[self-lockFocus]
围绕您的绘图代码。您的drawRect::是自定义方法,与drawRect:不同,当您的视图作为当前上下文时,将不会调用它,请尝试抛出一个[self-lockFocus]
,[self-unlockFocus]
围绕您的绘图代码。NSView
的drawRect:
方法代表您调用;应将其用于图形,如中所述
--
NSView
的drawRect:
方法是代表您调用的;应将其用于图形,如中所述
--
换句话说:-(void)setNeedsDisplayInRect:(int)pointx:(int)pointy{insert..}?而不是
[self-drawRect:point.x:point.y]
do[映射窗口设置需要显示在rect:rect]
并删除[映射窗口设置需要显示在rect:rect]中]代码>。您必须为整个窗口生成rect,因为您没有它。这不是drawRect:它是drawRect::谢谢,我将在其他答案中使用它!换句话说:-(void)setNeedsDisplayInRect:(int)pointx:(int)pointy{insert..}?而不是[self-drawRect:point.x:point.y]
do[映射窗口设置需要显示在rect:rect]
并删除[映射窗口设置需要显示在rect:rect]中]代码>。您必须为整个窗口生成rect,因为您没有它。这不是drawRect:它是drawRect::谢谢,我将在其他答案中使用它!point.x和point.y无法调用,因为它们不存在。不过,我这样做是为了完成类似的操作,谢谢!我使用了NSPoint origin=[self drawPoint];point.x和point.y无法调用,因为它们不存在。不过,我这样做是为了完成类似的操作,谢谢!我使用了NSPoint origin=[self drawPoint];谢谢你让我知道,我将把它变成一个内置方法。谢谢你让我知道,我将把它变成一个内置方法。
@interface mapWindow : NSView {
@private
NSView* theMapWindow;
NSPoint drawPoint;
}
// - (void)drawRect:(int)pointx: (int)pointy;
@property (assign) IBOutlet NSView* theMapWindow;
@property (assign) NSPoint drawPoint;
@end
@implementation mapWindow
@synthesize theMapWindow, drawPoint;
- (void)mouseDown:(NSEvent *)event
{
NSPoint point = [event locationInWindow];
NSLog(@"[%@ %@] mouseDown location == %@",
NSStringFromClass([self class]),
NSStringFromSelector(_cmd),
NSStringFromPoint(point));
[self setDrawPoint:point];
[self setNeedsDisplay:YES];
//NSLog( @"mouseDown location: (%f,%f)", (float) point.x, (float) point.y);
//[self drawRect:point.x:point.y];
}
- (void)drawRect:(NSRect)frame {
NSLog(@"Drawing point at %@", NSStringFromPoint(drawPoint));
NSRect drawFrame = NSMakeRect(point.x, point.y, 128.0, 128.0);
[NSBezierPath setDefaultLineWidth:4];
[[NSColor whiteColor] set];
[NSBezierPath fillRect:drawFrame];
[[NSColor grayColor] set];
[NSBezierPath strokeRect:drawFrame];
}