1from Cocoa import * 2from Quartz import * 3 4import math 5 6 7def doAlphaRects(context): 8 # ***** Part 1 ***** 9 ourRect = CGRectMake(0.0, 0.0, 130.0, 100.0) 10 numRects = 6 11 rotateAngle = 2 * math.pi / numRects 12 tintAdjust = 1./numRects 13 14 # ***** Part 2 ***** 15 CGContextTranslateCTM(context, 2*ourRect.size.width, 16 2*ourRect.size.height) 17 18 # ***** Part 3 ***** 19 tint = 1.0 20 for i in range(numRects): 21 CGContextSetRGBFillColor(context, tint, 0.0, 0.0, tint) 22 CGContextFillRect(context, ourRect) 23 CGContextRotateCTM(context, rotateAngle) # cumulative 24 tint -= tintAdjust 25 26class MyView (NSView): 27 def drawRect_(self, rect): 28 nsctx = NSGraphicsContext.currentContext() 29 context = nsctx.graphicsPort() 30 31 CGContextSetLineWidth(context, 5.0) 32 # Draw the coordinate axes. 33 CGContextBeginPath(context) 34 # First draw the x axis. 35 CGContextMoveToPoint(context, -2000., 0.0) 36 CGContextAddLineToPoint(context, 2000., 0.0) 37 CGContextDrawPath(context, kCGPathStroke) 38 # Next draw the y axis. 39 CGContextMoveToPoint(context, 0.0, -2000.0) 40 CGContextAddLineToPoint(context, 0.0, 2000.0) 41 CGContextDrawPath(context, kCGPathStroke) 42 43 doAlphaRects(context) 44