hello everyone,
i want to type in numbers in the NSTextField and want it to resize the NSView. I want it to change the scaling of X and Y:
[transform scaleXBy: bounds.size.width/10 yBy: bounds.size.height/100];
i'll type in one number for x and another for y, and x will affect 10 and y will affect 100. i can't seem to make it work...
Than you!
the whole code is:
#import "RanWithParameters.h"
@implementation RanWithParameters
- (id)initWithFrameNSRect)rect
{
NSLog(@"-------------initWithFrame------------------");
if (self = [super initWithFrame:rect])
{
transform = [[NSAffineTransform alloc] init];
path = [[NSBezierPath alloc] init];
//xmax = [[NSString alloc] init];
}
return self;
}
- (IBAction)generateid)sender
{
NSLog(@"generate");
[self drawRandomLine];
[self setNeedsDisplay:YES];
}
- (void)drawRandomLine
{
int i;
NSPoint p;
//Remove previosly drawn random points
[path removeAllPoints];
//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)randomPointlong)index
{
NSPoint result;
result.y = (random() % 100) -50;
result.x = index + 0;
return result;
}
//Setting scales
- (void)setScaleNSRect)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);
}
- (IBAction)xMaxid)sender
setScaleNSRect)bounds
{
float xMax;
NSString *xmax = [randomOutlet stringValue];
xMax = [xmax floatValue];
[transform scaleXBy: bounds.size.width/xMax yBy: bounds.size.height/100];
}
- (void)drawRectNSRect)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 redColor] 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
i want to type in numbers in the NSTextField and want it to resize the NSView. I want it to change the scaling of X and Y:
[transform scaleXBy: bounds.size.width/10 yBy: bounds.size.height/100];
i'll type in one number for x and another for y, and x will affect 10 and y will affect 100. i can't seem to make it work...
Than you!
the whole code is:
#import "RanWithParameters.h"
@implementation RanWithParameters
- (id)initWithFrameNSRect)rect
{
NSLog(@"-------------initWithFrame------------------");
if (self = [super initWithFrame:rect])
{
transform = [[NSAffineTransform alloc] init];
path = [[NSBezierPath alloc] init];
//xmax = [[NSString alloc] init];
}
return self;
}
- (IBAction)generateid)sender
{
NSLog(@"generate");
[self drawRandomLine];
[self setNeedsDisplay:YES];
}
- (void)drawRandomLine
{
int i;
NSPoint p;
//Remove previosly drawn random points
[path removeAllPoints];
//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)randomPointlong)index
{
NSPoint result;
result.y = (random() % 100) -50;
result.x = index + 0;
return result;
}
//Setting scales
- (void)setScaleNSRect)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);
}
- (IBAction)xMaxid)sender
setScaleNSRect)bounds
{
float xMax;
NSString *xmax = [randomOutlet stringValue];
xMax = [xmax floatValue];
[transform scaleXBy: bounds.size.width/xMax yBy: bounds.size.height/100];
}
- (void)drawRectNSRect)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 redColor] 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