drawScoreArray NSBezierPath elementAtIndex and elementTypeAtIndex:associatedPoints.
drawScoreArray NSBezierPath elementAtIndex and elementTypeAtIndex:associatedPoints.
- Subject: drawScoreArray NSBezierPath elementAtIndex and elementTypeAtIndex:associatedPoints.
- From: Michel Haver <email@hidden>
- Date: Wed, 11 Jul 2001 21:41:18 +0200
I try to draw a ScoreArray in a Circle.
My drawScoreArray looks something like this now
#import "MyViewDrawScoreArray.h"
#import <ApplicationServices/ApplicationServices.h>
@implementation MyViewDrawScoreArray
- (id)init
{
[super init];
// Initialize the drawScoreArray Array
// drawScoreArray = [[ NSMutableArray alloc ] initWithCapacity: 11 ];
drawScoreArray = [NSMutableArray arrayWithCapacity:11];
// Initialize the drawScoreArray with 12 elements
// Element 0 on index 0
[ drawScoreArray insertObject: @"0" atIndex: 0 ];
// Element 10 on index 1
[ drawScoreArray insertObject: @"10" atIndex: 1 ];
// Element 20 on index 2
[ drawScoreArray insertObject: @"20" atIndex: 2 ];
// Element 30 on index 3
[ drawScoreArray insertObject: @"30" atIndex: 3 ];
// Element 40 on index 4
[ drawScoreArray insertObject: @"40" atIndex: 4 ];
// Element 50 on index 5
[ drawScoreArray insertObject: @"50" atIndex: 5 ];
// Element 60 on index 6
[ drawScoreArray insertObject: @"60" atIndex: 6 ];
// Element 70 on index 7
[ drawScoreArray insertObject: @"70" atIndex: 7 ];
// Element 80 on index 8
[ drawScoreArray insertObject: @"80" atIndex: 8 ];
// Element 90 on index 9
[ drawScoreArray insertObject: @"90" atIndex: 9 ];
// Element 100 on index 10
[ drawScoreArray insertObject: @"100" atIndex: 10 ];
// Element on index 11
[ drawScoreArray insertObject: @"" atIndex: 11 ];
return self;
}
-(BOOL)isFlipped {
return YES;
}
- (BOOL)isOpaque
{
return YES;
}
- (void)awakeFromNib
{
// [self setArrowPath: [self createArrowPath]];
[self setCirclePath: [self createCirclePath]];
}
- (id)initWithFrame:(NSRect)frame {
[super initWithFrame:frame];
// [self setString: @"Hello World 0456789"];
// [self setString: drawScoreArray objectAtIndex: 1];
[self setFont: [NSFont systemFontOfSize: 18]];
// circleStrokeColor = [[NSColor redColor] retain];
return self;
}
// dealloc is the method called when objects are being freed. (Note that
"release"
// is called to release objects; when the number of release calls reduce the
// total reference count on an object to zero, dealloc is called to free
// the object. dealloc should free any memory allocated by the subclass
// and then call super to get the superclass to do additional cleanup.
- (void)dealloc {
// [circleStrokecolor release];
// [color release];
[super dealloc];
}
- (void)setString:(NSString *)value {
[string autorelease];
string = [value copy];
[self setNeedsDisplay: YES];
}
- (void)setFont:(NSFont *)value {
[value retain];
[font autorelease];
font = value;
[self setNeedsDisplay: YES];
}
- (void)drawRect:(NSRect)rect {
// int i, j;
// float a0, a1;
// CGContextRef context;
// Leave the Directory and myBounds on
NSRect myBounds = [self bounds];
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
// Paint a white background
[[NSColor whiteColor] set];
[NSBezierPath fillRect:myBounds];
// Draw some crosshairs on the view
[[NSColor lightGrayColor] set];
[NSBezierPath
strokeLineFromPoint:NSMakePoint(0,(myBounds.size.height/2.0))
toPoint:NSMakePoint(myBounds.size.width, (myBounds.size.height/2.0))];
[NSBezierPath strokeLineFromPoint:NSMakePoint((myBounds.size.width/2.0),
0) toPoint:NSMakePoint((myBounds.size.width/2.0), myBounds.size.height)];
// Draw a black border around the view.
[[NSColor blackColor] set];
[NSBezierPath strokeRect:myBounds];
// Render a string.
// [attrs setObject: font forKey: NSFontAttributeName];
// [string drawAtPoint: NSMakePoint((myBounds.size.width/4.0), 5)
withAttributes: attrs];
// Draw the inner and outer circle
// [self drawCircle];
// [self drawCircle1];
// Draw the content of the drawScoreArray
[self drawScoreArray ];
}
- (NSBezierPath *)circlePath
{
return circlePath;
}
- (void)setCirclePath:(NSBezierPath *)newPath
{
[newPath retain];
[circlePath release];
circlePath = newPath;
}
- (NSBezierPath *)createCirclePath
{
// An NSPoint that is re-used to create the arrow model.
NSPoint point;
NSBezierPath *model = [NSBezierPath bezierPath];
// Create simple circle with it's center at (0,0).
point.x = 1;
point.y = 0;
[model moveToPoint:point];
point.x = 0;
[model appendBezierPathWithArcWithCenter:point
radius:1.0 startAngle:0 endAngle:360];
// Draw a plus sign.
// Start with a horizontal line.
point.x = .05;
point.y = 0;
[model moveToPoint:point];
point.x = -.05;
[model lineToPoint:point];
// Draw the vertical line
point.x = 0;
point.y = .05;
[model moveToPoint:point];
point.y = -.05;
[model lineToPoint:point];
// Finish drawing.
[model closePath];
return model;
}
- (void)drawScoreArray
{
NSBezierPath *circleScoreArrayToDraw;
NSAffineTransform *translationMatrix;
NSAffineTransform *scaleMatrix;
//Make the Circle grow with resizing MyView
NSRect myBounds = [self bounds];
//Make a drawString
NSString * drawString;
// NSMutableString * drawString;
//Make NSpoint
NSPoint * point;
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
//Make screenLocation grow with the NSView
//NSPoint screenLocation = NSMakePoint(50, 50);
// Read somehow the scoreArray from 0 to 11 and draw the string at
Point on the circle with 30 degrees for each element. How??
NSPoint screenLocation = NSMakePoint(myBounds.size.width/2.0,
myBounds.size.height/2.0);
NSColor *circleStrokeColor = [NSColor blackColor];
// NSColor *circleStrokeColor = [NSColor blueColor];
// NSColor *circleFillColor = [NSColor blueColor];
float circleRadius = 130.0;
translationMatrix = [NSAffineTransform transform];
[translationMatrix translateXBy: screenLocation.x yBy:
screenLocation.y];
scaleMatrix = [NSAffineTransform transform];
[scaleMatrix scaleBy: circleRadius];
[scaleMatrix appendTransform:translationMatrix];
circleScoreArrayToDraw = [scaleMatrix transformBezierPath:[self
circlePath]];
// drawString = [NSMutableString stringWithString:[drawScoreArray
objectAtIndex:0]];
// this code doesmn't work and gives really shit
NSLog(@"String drawScoreArray: %@.\n", [drawScoreArray
objectAtIndex:0]);
drawString = [drawScoreArray objectAtIndex:0];
// drawString = @"5";
NSLog(@"String draw: %@.\n", drawString);
[attrs setObject: font forKey: NSFontAttributeName];
[drawString drawAtPoint: NSMakePoint((myBounds.size.width/4.0), 5)
withAttributes: attrs];
[circleStrokeColor set];
[circleScoreArrayToDraw setLineWidth:2.0];
[circleScoreArrayToDraw stroke];
}
@end
The statements
NSLog(@"String drawScoreArray: %@.\n", [drawScoreArray objectAtIndex:0]);
drawString = [drawScoreArray objectAtIndex:0];
NSLog(@"String draw: %@.\n", drawString);
Results in
Jul 11 21:05:50 Circles[546] String drawScoreArray Array: (null).
Jul 11 21:05:50 Circles[546] String draw: (null).
Why is the drawstring set to null if I has initialize it with a stringvalue.
// Element 0 on index 0
[ drawScoreArray insertObject: @"0" atIndex: 0 ];
An other issue that puzzles me is:
I can draw a Circle with NSBezierPath in a NSView. I want to obtain
information about the Path and their associated points from that Circle
directly by using the elementAtIndex and
elementTypeAtIndex:associatedPoints. These are defined in the BezierPath.h
as
- (NSBezierPathElement)elementAtIndex:(int)index
associatedPoints:(NSPointArray)points;
// As above with points == NULL.
- (NSBezierPathElement)elementAtIndex:(int)index;
- (void)setAssociatedPoints:(NSPointArray)points atIndex:(int)index;
The way it should be used is not clear to me at the moment.
I want to get from the Circle with (0,0) and radius 130
12 points, each point with 30 degrees difference to each other.
How can build the NSPointArray with elementAtIndex to break down the path
and reconstruct it point to point?
Cheers,
Michel