def run(self): self.app = wx.App(0) self.frame = wx.Frame(None, wx.ID_ANY, 'GC Renderer Test', size=(800, 600)) self.frame.Show() # creation phase renderer = GCRenderer(window=self.frame, double_buffered=False) renderer.Clear('white') black_brush = renderer.CreateBrush('plain', 'black') red_brush = renderer.CreateBrush('plain', 'red') black_pen = renderer.CreatePen(wx.Colour(0, 0, 0, 255), width=5) red_pen = renderer.CreatePen(wx.Colour(255, 0, 0, 50), width=10) red_pen.Activate() bmp = wx.BitmapFromImage(wx.Image('../data/toucan.png')) font = renderer.CreateFont(14, 'default', 'italic', 'normal', True, 'Arial', 'blue') font.Activate() path = renderer.CreatePath() vertices = [(0, 20), (70, 35), (99, 46), (235, 11), (555, 555)] path.MoveToPoint(*vertices[0]) for v in vertices[1:]: path.AddLineToPoint(*v) # do drawing black_brush.Activate() red_pen.Activate() renderer.DrawEllipse(100, 100, 50, 80) renderer.DrawLines([(0, 0), (200, 200)]) red_brush.Activate() black_pen.Activate() path.Stroke() renderer.DrawRotatedText('Hello World!', 50, 300, angle=30) renderer.DrawBitmap(bmp, 200, 200, 40, 40) renderer.Present() self.app.MainLoop()
if __name__ == "__main__": import os.path app = MyApp(0) buffer = wx.EmptyBitmap(200, 200, 32) dc = wx.MemoryDC(buffer) dc.SetBackground(wx.WHITE_BRUSH) dc.Clear() dc = wx.GraphicsContext.Create(dc) dc.SetBrush(dc.CreateBrush(wx.BLACK_BRUSH)) print('default matrix %s' % dc.GetTransform().Get()) import math original_matrix = dc.GetTransform() path = dc.CreatePath() path.AddEllipse(0, 20, 40, 20) path.MoveToPoint(0, 0) path.AddLineToPoint(10, 10) path.AddLineToPoint(20, 0) path.CloseSubpath() if wx.Platform == "__WXMSW__": a = 45 else: a = radians(45) original = dc.CreateMatrix(*original_matrix.Get()) om = dc.CreateMatrix(*original_matrix.Get()) m0 = dc.CreateMatrix() m0.Scale(0.5, 0.5) m1 = dc.CreateMatrix() m1.Translate(150, 150)
def _draw_balloon_bg(self, dc, inner_size, tip_pos, above, sticky): """ Draw the balloon background leaving inner_size for content. tip_pos determines where the tip of the ballon should be. above determines if the balloon should be above the tip (True) or below (False). This is not currently implemented. W |----------------| ______________ _ / \ | R = Corner Radius | | | AA = Left Arrow-leg angle | W_ARROW | | H MARGIN = Text margin | |--| | | * = Starting point \____ ______/ _ / / | /_/ | H_ARROW * - |----| ARROW_OFFSET Calculation of points starts at the tip of the arrow and continues clockwise around the ballon. Return (bounding_rect, x, y) where x and y is at top of inner region. """ # Prepare path object gc = wx.GraphicsContext.Create(self.dc) path = gc.CreatePath() # Calculate path R = BALLOON_RADIUS W = 1 * R + inner_size[0] H = 1 * R + inner_size[1] H_ARROW = 14 W_ARROW = 15 AA = 20 # Starting point at the tip of the arrow (tipx, tipy) = tip_pos p0 = wx.Point(tipx, tipy) path.MoveToPoint(p0.x, p0.y) # Next point is the left base of the arrow p1 = wx.Point(p0.x + H_ARROW * math.tan(math.radians(AA)), p0.y - H_ARROW) path.AddLineToPoint(p1.x, p1.y) # Start of lower left rounded corner p2 = wx.Point(p1.x - ARROW_OFFSET + R, p1.y) path.AddLineToPoint(p2.x, p2.y) # The lower left rounded corner. p3 is the center of the arc p3 = wx.Point(p2.x, p2.y - R) path.AddArc(p3.x, p3.y, R, math.radians(90), math.radians(180)) # The left side p4 = wx.Point(p3.x - R, p3.y - H + R) left_x = p4.x path.AddLineToPoint(p4.x, p4.y) # The upper left rounded corner. p5 is the center of the arc p5 = wx.Point(p4.x + R, p4.y) path.AddArc(p5.x, p5.y, R, math.radians(180), math.radians(-90)) # The upper side p6 = wx.Point(p5.x + W - R, p5.y - R) top_y = p6.y path.AddLineToPoint(p6.x, p6.y) # The upper right rounded corner. p7 is the center of the arc p7 = wx.Point(p6.x, p6.y + R) path.AddArc(p7.x, p7.y, R, math.radians(-90), math.radians(0)) # The right side p8 = wx.Point(p7.x + R, p7.y + H - R) path.AddLineToPoint(p8.x, p8.y) # The lower right rounded corner. p9 is the center of the arc p9 = wx.Point(p8.x - R, p8.y) path.AddArc(p9.x, p9.y, R, math.radians(0), math.radians(90)) # The lower side p10 = wx.Point(p9.x - W + W_ARROW + ARROW_OFFSET, p9.y + R) path.AddLineToPoint(p10.x, p10.y) path.CloseSubpath() # Draw sharp lines on GTK which uses Cairo # See: http://www.cairographics.org/FAQ/#sharp_lines gc.Translate(0.5, 0.5) # Draw the ballon BORDER_COLOR = wx.Colour(127, 127, 127) BG_COLOR = wx.Colour(255, 255, 231) PEN = wx.Pen(BORDER_COLOR, 1, wx.SOLID) BRUSH = wx.Brush(BG_COLOR, wx.SOLID) gc.SetPen(PEN) gc.SetBrush(BRUSH) gc.DrawPath(path) # Draw the pin if sticky: pin = wx.Bitmap(os.path.join(ICONS_DIR, "stickypin.png")) else: pin = wx.Bitmap(os.path.join(ICONS_DIR, "unstickypin.png")) self.dc.DrawBitmap(pin, p7.x - 5, p6.y + 5, True) # Return bx = left_x by = top_y bw = W + R + 1 bh = H + R + H_ARROW + 1 bounding_rect = wx.Rect(bx, by, bw, bh) return (bounding_rect, left_x + BALLOON_RADIUS, top_y + BALLOON_RADIUS)
def drawInKMode(self, dc): """ Drawing in Kandinsky mode Args: dc (wx.PaintDC): PaintDC to draw on. Returns: None """ if DEBUG: print("FlexTilesFrame.drawInKMode()") img = self.tileImgLarge.Copy() ftR = self.ftR # rect of FlexTiles area ##### [begin] drawing sz = self.tileSz bmp = wx.Bitmap(sz, sz, depth=-1) memDC = wx.MemoryDC() memDC.SelectObject(bmp) ### draw existing tile image if img.GetSize()[0] != sz: img = img.Rescale(sz, sz, wx.IMAGE_QUALITY_HIGH) memDC.DrawBitmap(wx.Bitmap(img), 0, 0) isTempDrawing = False # temporary drawing is for some cases # such as line drawing (x1,y1 is determined but not x2, y2) ### make new drawings, if available if self.kD != None: memDC.SetPen(wx.Pen(self.selectedSCol, self.selectedSThick)) memDC.SetBrush(wx.Brush(self.selectedFCol)) kD = self.kD if kD["name"] == "fill": memDC.SetBackground(wx.Brush(self.selectedFCol)) memDC.Clear() self.kD = None elif kD["name"] in ["line", "rectangle", "circle"]: # can be drawn with two points if kD["x1"] != None: x1 = kD["x1"] y1 = kD["y1"] if kD["x2"] == None: x2 = self.currMP[0] - ftR[0] y2 = self.currMP[1] - ftR[1] isTempDrawing = True else: x2 = kD["x2"] y2 = kD["y2"] self.kD = None if kD["name"] == "line": memDC.DrawLine(x1, y1, x2, y2) elif kD["name"] == "rectangle": memDC.DrawRectangle(x1, y1, (x2 - x1), (y2 - y1)) elif kD["name"] == "circle": rad = int(np.sqrt((x2 - x1)**2 + (y2 - y1)**2)) memDC.DrawCircle(x1, y1, rad) elif kD["name"] == "curvyline": # curvy line drawing if kD["x1"] != None: pen = wx.Pen(self.selectedSCol, self.selectedSThick) x1 = kD["x1"] y1 = kD["y1"] if kD["x2"] == None: x2 = self.currMP[0] - ftR[0] y2 = self.currMP[1] - ftR[1] memDC.DrawLine(x1, y1, x2, y2) isTempDrawing = True else: x2 = kD["x2"] y2 = kD["y2"] gc = wx.GraphicsContext.Create(memDC) gc.SetPen( wx.Pen(self.selectedSCol, self.selectedSThick)) path = gc.CreatePath() path.MoveToPoint(x1, y1) if kD["x3"] == None: x3 = self.currMP[0] - ftR[0] y3 = self.currMP[1] - ftR[1] path.AddCurveToPoint(x1, y1, x2, y2, x3, y3) isTempDrawing = True else: x3 = kD["x3"] y3 = kD["y3"] path.AddCurveToPoint(x1, y1, x2, y2, x3, y3) self.kD = None gc.StrokePath(path) elif kD["name"] == "polygon": pts = copy(kD["pts"]) if not kD["isClosed"]: # when it's not complete # add current mouse pointer position to the point list pts.append( (self.currMP[0] - ftR[0], self.currMP[1] - ftR[1])) if len(pts) == 2: # not polygon yet # draw as line memDC.DrawLine(pts[0][0], pts[0][1], pts[1][0], pts[1][1]) isTempDrawing = True elif len(pts) > 2: if kD["isClosed"]: self.kD = None else: isTempDrawing = True memDC.DrawPolygon(pts) # draw polygon elif kD["name"] == "pencil": pts = self.freePencilDrawingPts l = len(pts) if l > 1: memDC.DrawLines(pts) self.freePencilDrawingPts = [copy(pts[-1])] memDC.SelectObject(wx.NullBitmap) ##### [end] drawing # draw tile on the given DC dc.DrawBitmap(bmp, ftR[0], ftR[1]) if not isTempDrawing: # update tile image self.tileImgLarge = bmp.ConvertToImage()