NSbezierPath => bug

  • Créateur du sujet Créateur du sujet Elaum
  • Date de début Date de début

Elaum

Membre confirmé
12 Mars 2005
19
0
38
marseille
Bonjour, je suis en train d'essayer de porter un vieux programme que j'avais fait sur calculette en objective-C et je recontre des difficultés.

En gros, je déclare une fenetre avec une CustomView, 2 textfield Side (pour le nombre de coté du polygone) et LvlCurve (un coeficient dans le calcul)
Le problème et que rien ne s'affiche, je ne comprend pas d'où viens le bug.
Des yeux avisés trouverons sans doute l'erreur.

Voici mon code de ma custom view :
Bloc de code:
- (void)drawRect:(NSRect)rect { 
	int i,rho,theta;
    	[[NSColor whiteColor] set];						
    	NSRectFill([self bounds]);
	[[NSColor greenColor] set]; 
	
    	NSBezierPath *polygone = [NSBezierPath bezierPath]; 
    	NSPoint cplx = NSMakePoint(300, 200); //Mon point de depart
	[polygone moveToPoint:cplx];
	for (i = 0 ; i < [Side intValue] ; i++)
	{
		//Le calcul de la rotation (c'est des math)
		rho = sqrt(cplx.x*cplx.x+cplx.y*cplx.y);
		theta = acos(cplx.x/(double)rho);
		theta += 2 * PI * [LvlCurve intValue] / (double) [Side intValue];
		cplx.x = (int)(rho * cos(theta)); cplx.y = (int)(rho * sin(theta));
		//Fin du calcul
		[polygone lineToPoint:cplx]; 
	}
	
	[polygone closePath];
	[[NSColor greenColor] set]; 
    	[polygone fill]; 
}

Sous #import "CocoaDrawing.h", j'ai mis :
Bloc de code:
#import "CocoaDrawing.h"
#include <math.h>

#define PI 3.1415926535897932388626

Si quelqu'un a un idée :)
 
Bonjour,
tu dois encadrer tes routines de dessin dans un [self lockFocus] et [self unlockFocus], et bien sur ne pas oublier d'envoyer un setNeedsDisplay si necessaire.
 
ntx a dit:
Bonjour,
tu dois encadrer tes routines de dessin dans un [self lockFocus] et [self unlockFocus]...

Je crois pas que c'est pas nécessaire car drawRect le fait déjà. Je cite la doc d'apple

Before a display... method invokes drawRect:, it sets the window server up with information about the view, including the window device it draws in, the coordinate system and clipping path it uses, and other graphics state information. The method used to do this is lockFocus, and it has a companion method that undoes its effects, called unlockFocus.


, et bien sur ne pas oublier d'envoyer un setNeedsDisplay si necessaire

Par contre (ntx a raison) setNeedsDisplay s'impose et c'est certainement ça que tu dois oublier.
Je structurerais ton code d'Elaum un peu différement :

Dans ta customview on déclare l'objet NSBezierPath

Bloc de code:
 @interface myView : NSView  {
.....
NSBezierPath *polygone ;
...
}

qu'on initialise dans initWithFrame (par exemple)

Bloc de code:
polygone =[[NSBezierPath alloc]] init]

Ensuite une méthode spécifique qui construit ton NSBezierPath qui finit avec un setNeedDisplay

Bloc de code:
- (void)constructPathAndDraw {
        [polygone removeAllPoints];
        int i,rho,theta;
        NSPoint cplx = NSMakePoint(300, 200); //Mon point de depart
        [polygone moveToPoint:cplx];
        for (i = 0 ; i < [Side intValue] ; i++)
        {
                //Le calcul de la rotation (c'est des math)
                rho = sqrt(cplx.x*cplx.x+cplx.y*cplx.y);
                theta = acos(cplx.x/(double)rho);
                theta += 2 * PI * [LvlCurve intValue] / (double) [Side intValue];
                cplx.x = (int)(rho * cos(theta)); cplx.y = (int)(rho * sin(theta));
                //Fin du calcul
                [polygone lineToPoint:cplx]; 
        }
        
        [polygone closePath];
        [self setNeedDisplay:YES];
}

puis dans drawrect tu écris :


Bloc de code:
- (void)drawRect:(NSRect)rect { 
        [[NSColor whiteColor] set];                                             
        NSRectFill(rect);        
        [[NSColor greenColor] set]; 
        [polygone fill]; 
}


Pour dessiner ta courbe on n'a plus qu'à envoyer le message :

Bloc de code:
 [(self ou myView) constructPathAndDraw]

J'ai pas testé ce code mais ça devrait fonctionner.