Sample code below.
//
// DotView.h
//
// Created by Paul Buxton on 25/09/2009.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface DotView : NSView {
IBOutlet NSColorWell *colorWell;
IBOutlet NSSlider *slider;
NSPoint center;
NSColor *color;
float radius;
}
- (IBAction)setColor:(id)sender;
- (IBAction)setRadius:(id)sender;
@end
//
// DotView.m
//
// Created by Paul Buxton on 25/09/2009.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "DotView.h"
@implementation DotView
-(id)initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:frameRect];
center.x=50.0;
center.y=50.0;
radius=20;
color = [[NSColor redColor] retain];
return self;
}
-(void)awakeFromNib
{
[colorWell setColor:color];
[slider setFloatValue:radius];
}
-(void)dealloc
{
[color release];
[super dealloc];
}
-(void)drawRect:(NSRect)dirtyRect
{
NSRect dotRect;
[[NSColor whiteColor] set];
NSRectFill([self bounds]);
dotRect.origin.x = center.x - radius;
dotRect.origin.y = center.y - radius;
dotRect.size.width = 2*radius;
dotRect.size.height = 2*radius;
[color set];
[[NSBezierPath bezierPathWithOvalInRect:dotRect] fill];
}
-(BOOL)isOpaque
{
return YES;
}
-(void)mouseDown:(NSEvent *)event
{
NSPoint eventlocation = [event locationInWindow];
center = [self convertPoint:eventlocation fromView:nil];
[self needsDisplay:YES];
}
- (IBAction)setColor:(id)sender {
NSColor *newColor = [sender color];
[newColor retain];
[color release];
color = newColor;
[self needsDisplay:YES];
}
- (IBAction)setRadius:(id)sender {
radius = [sender floatValue];
[self needsDisplay:YES];
}
-(BOOL)acceptsFirstMouse:(NSEvent *)theEvent
{
return YES;
}
-(BOOL)acceptsFirstResponder
{
return YES;
}
@end
Cheers,
Paul
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden