]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/GraphicsContext.py
Don't decref if the interpreter is not initialized, (eg. it's in its
[wxWidgets.git] / wxPython / demo / GraphicsContext.py
1
2 import wx
3 import colorsys
4 from math import cos, sin, radians
5
6 #----------------------------------------------------------------------
7
8 BASE = 80.0 # sizes used in shapes drawn below
9 BASE2 = BASE/2
10 BASE4 = BASE/4
11
12
13 class TestPanel(wx.Panel):
14 def __init__(self, parent, log):
15 self.log = log
16 wx.Panel.__init__(self, parent, -1)
17
18 self.Bind(wx.EVT_PAINT, self.OnPaint)
19
20 def OnPaint(self, evt):
21 dc = wx.PaintDC(self)
22 gc = wx.GraphicsContext.Create(dc)
23
24 font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
25 font.SetWeight(wx.BOLD)
26 gc.SetFont(font)
27
28 # make a path that contains a circle and some lines, centered at 0,0
29 path = gc.CreatePath()
30 path.AddCircle(0, 0, BASE2)
31 path.MoveToPoint(0, -BASE2)
32 path.AddLineToPoint(0, BASE2)
33 path.MoveToPoint(-BASE2, 0)
34 path.AddLineToPoint(BASE2, 0)
35 path.CloseSubpath()
36 path.AddRectangle(-BASE4, -BASE4/2, BASE2, BASE4)
37
38
39 # Now use that path to demonstrate various capbilites of the grpahics context
40 gc.PushState() # save current translation/scale/other state
41 gc.Translate(60, 75) # reposition the context origin
42
43 gc.SetPen(wx.Pen("navy", 1))
44 gc.SetBrush(wx.Brush("pink"))
45 gc.SetTextColour("black")
46
47 # show the difference between stroking, filling and drawing
48 for label, PathFunc in [("StrokePath", gc.StrokePath),
49 ("FillPath", gc.FillPath),
50 ("DrawPath", gc.DrawPath)]:
51 if "wxGTK" in wx.PlatformInfo:
52 w, h = dc.GetTextExtent(label) # NYI in Cairo context
53 else:
54 w, h = gc.GetTextExtent(label)
55
56 gc.DrawText(label, -w/2, -BASE2-h)
57 PathFunc(path)
58 gc.Translate(2*BASE, 0)
59
60
61 gc.PopState() # restore saved state
62 gc.PushState() # save it again
63 gc.Translate(60, 200) # offset to the lower part of the window
64
65 gc.DrawText("Scale", 0, -BASE2)
66 gc.Translate(0, 20)
67
68 gc.SetBrush(wx.Brush(wx.Colour(178, 34, 34, 128))) # 128 == half transparent
69 for cnt in range(8):
70 gc.Scale(1.08, 1.08) # increase scale by 8%
71 gc.Translate(5,5)
72 gc.DrawPath(path)
73
74
75 gc.PopState() # restore saved state
76 gc.PushState() # save it again
77 gc.Translate(400, 200)
78 gc.DrawText("Rotate", 0, -BASE2)
79
80 gc.Translate(0, 75)
81 for angle in range(0, 360, 30):
82 gc.PushState() # save this new current state so we can pop back to
83 # it at the end of the loop
84 r, g, b = [int(c * 255) for c in colorsys.hsv_to_rgb(float(angle)/360, 1, 1)]
85 gc.SetBrush(wx.Brush(wx.Colour(r, g, b, 64)))
86
87 # use translate to artfully reposition each drawn path
88 gc.Translate(1.5 * BASE2 * cos(radians(angle)),
89 1.5 * BASE2 * sin(radians(angle)))
90
91 # use Rotate to rotate the path
92 gc.Rotate(radians(angle))
93
94 # now draw it
95 gc.DrawPath(path)
96 gc.PopState()
97
98 gc.PopState()
99
100
101 #----------------------------------------------------------------------
102
103 def runTest(frame, nb, log):
104 win = TestPanel(nb, log)
105 return win
106
107 #----------------------------------------------------------------------
108
109
110
111 overview = """<html><body>
112 <h2><center>wx.GraphicsContext and wx.GraphicsPath</center></h2>
113
114 The new advanced 2D drawing API is implemented via the
115 wx.GraphicsContext and wx.GraphicsPath classes. They wrap GDI+ on
116 Windows, Cairo on wxGTK and CoreGraphics on OS X. They allow
117 path-based drawing with alpha-blending and anti-aliasing, and use a
118 floating point cooridnate system.
119
120 <p>When viewing this sample pay attention to how the rounded edges are
121 smoothed with anti-aliased drawing, and how the shapes on the lower
122 half of the window are partially transparent, allowing you to see what
123 was drawn before.
124
125 </body></html>
126 """
127
128
129
130 if __name__ == '__main__':
131 import sys,os
132 import run
133 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
134