hello everyone,
i would like to know how to code for this: everytime i click generate i want the previous path’s colors to change into a different color.
THANKS
#import “RanWithParameters.h”
@implementation RanWithParameters
-
(id)initWithFrame:(NSRect)rect
{
NSLog(@“-------------initWithFrame------------------”);
if (self = [super initWithFrame:rect])
{
transform = [[NSAffineTransform alloc] init];
path = [[NSBezierPath alloc] init];
}
return self;
} -
(IBAction)generate:(id)sender
{
NSLog(@“generate”);
[self drawRandomLine];
[self setNeedsDisplay:YES];
}
-
(void)drawRandomLine
{
int i;
NSPoint p;//Create a path object
[path setLineWidth:2.0];
p = [self randomPoint:0];
[path moveToPoint: p];for (i = 0; i <= 10; i++)
{
p = [self randomPoint:i];
[path lineToPoint: p];
}
}
// randomPoint returns a random point inside the view
-
(NSPoint)randomPoint:(long)index
{
NSPoint result;
result.y = (random() % 100) -50;
result.x = index + 0;return result;
}
//Setting scales
-
(void)setScale:(NSRect)bounds
{
NSAffineTransformStruct I, unitsToPix;NSLog(@“setScale”);
I.m11 = 1;
I.m12 = 0;
I.m21 = 0;
I.m22 = 1;
I.tX = 0;
I.tY = 0;[transform setTransformStruct:I];
[transform translateXBy: bounds.origin.x yBy: bounds.origin.y + bounds.size.height/2.0];
[transform scaleXBy: bounds.size.width/10 yBy: bounds.size.height/100];unitsToPix = [transform transformStruct];
NSLog(@“unitsToPix.m11=%f unitsToPix.m12=%f unitsToPix.m21=%f unitsToPix.m22=%f unitsToPix.tX=%f unitsToPix.tY=%f”,
unitsToPix.m11, unitsToPix.m12, unitsToPix.m21, unitsToPix.m22, unitsToPix.tX, unitsToPix.tY);
} -
(void)drawRect:(NSRect)rect
{
NSRect bounds;NSLog(@“======= drawRect ======”);
bounds = [self bounds];
[self setScale:bounds];
//Fill the view with black
[[NSColor blackColor] set];
[NSBezierPath fillRect:bounds];// Draw the path red
[[NSColor greenColor] set];[path transformUsingAffineTransform: transform]; // transform path in units to path in pixels
[path stroke]; // draw path in pixels
[transform invert]; // invert transform to go from pixels to units
[path transformUsingAffineTransform: transform]; // transform path in pixels back to path in units
[transform invert]; // invert transform back to take us from units to pixels
} -
(void)dealloc
{
[path release];
[transform release];
[super dealloc];
}
@end