Cocoa画图

来源:互联网 发布:淘宝神笔是什么意思 编辑:程序博客网 时间:2024/04/30 13:55

points(NSPoint)

同直角坐标系一样原点在左下角

结构

typedef struct _NSPoint{

float x;

float y;

}NSPoint;

定义:NSPoint thePoint;

初始化:thePoint=NSMakePoint(0,0);

Rects(NSRect) and sizes(NSSize)

结构

typedef struct _NSRect{

NSPoint origin;

NSSize size;

}NSRect;

 

typedef struct _NSSize{

float width;

float height;

}NSSize;

定义:NSRect theRect;

初始化:theRect=NSMakeRect(0,0,100,100);//(x,y,width,height

使用:theRect.origin.x , theRect.origin.y

theRect.size.width , theRect.size.height

Colors(NSColor)

两种方法初始化

简单方式,用系统已提供的颜色名称

NSColor *aColor;

aColor=[NSColor blackColor];

blackColorBlack

blueColorBright blue

brownColorBrown

clearColorClear/transparent

cyanColorLight blue

DarkGrayColorDark gray(for the Canadians,dark grey)

grayColorMedium gray

greenColorBright green

lightGrayColorLight gray

magentaColorPinkish purple color

orangeColorOrange

purpleColorPurple

redColorBright red

whiteColorWhite

yellowColorBright and sunny yellow

自己新建颜色

Device dependent(or device)

DeviceRGB:(Red , green , blue and alpha components)

//参数值为0-1,颜色表示[(num +1)/256],alpha表不透明度

NSColor=theColor;//black

theColor=[NSColor colorWithDeviceRed:(float)0.0,

green:(float)0.0 blue:(float)0.0 alpha:(float)1.0];

[theColor set];//设置画笔

DeviceCMYK:(Cyan , magenta , yellow , black and alpha components)

DeviceWhite:(White and alpha components)

Device  independent(or calibrated)

Named

用线和图形绘画

利用点成线,线成面的原理

画一个矩形

NSRect theRect;

theRect=NSMakeRect(0,0,100,100);

NSBezierPath *thePath;

thePath=[NSBezierPath bezierPathWithRect:theRect];

//bezierPathWithOvalInRect 画椭圆

用颜色填充矩形

NSColor *theColor=[NSColor blackColor];

[theColor set];

[thePath fill];

画一条线

画一条线就相当于在一条线周围描个框,并填充它

在上面已填充的矩形周围描框,需要的操作同上相反

theColor=[NSColor blackColor];

[theColor set];

[thePath setLineWidth:5];

[thePath stroke];

画任意图形

用多个点连成线然后填充构成图形,有起始点,其它点以起始点为相对坐标,移动图形时仅需要改变起始点坐标

 

+X:X右边; -X:X左边; +Y:Y上边; -Y:Y下边

NSBezierPath *stopSign=[NSBezierPath bezierPath];

NSPoint pt1,pt2,pt3,pt4,pt5;//五边形

pt1=NSMakePoint(300,300);

pt2=NSMakePoint(400,300);

pt3=NSMakePoint(450,200);

pt4=NSMakePoint(350,100);

pt5=NSMakePoint(250,200);

[stopSign moveToPoint:pt1];

[stopSign relativeLineToPoint:pt2];

[stopSign relativeLineToPoint:pt3];

[stopSign relativeLineToPoint:pt4];

[stopSign relativeLineToPoint:pt5];

[stopSign closePath];

[[NSColor redColor] set];

[stopSign fill];

[[NSColor whiteColor]set];

[stopSign setLineWide:5];

[stopSign stroke];

画文字(Text)

NSString *theString=@"SHOW";

NSPoint theTextPos=NSMakePoint(100,120);

[theString drqwAtPoint:theTextPos withAttributes:nil];

可通过设置withAttributes方法(需要NSDictionary类型变量)修改文字显示样式

NSMutableDictionary *theAttributes=[[NSMutableDictionary alloc] init];

[theAttributes setObject:[NSFont fontWithName:@"Helvetica" size:62] forKey:NSFontAttributeName];

[theAttributes setObject:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];

[theString drawAtPoint:theTextPos withAttributes:theAttributes];

[theAttrubutes release];

显示图片

把一张图片拉到Xcode工程中

NSPoint theImagePos=NSMakePoint(0,0)

NSImage * theImage=[NSImage imageNamed:@"mypic.jpg"];

[theImage dissolveToPoint:theImagePos fraction:(1.0)];

//fraction的值表示图片显示的不透明度:1表不透明

原创粉丝点击