]> git.saurik.com Git - wxWidgets.git/blame_incremental - wxPython/demo/DrawXXXList.py
Reworked how stock objects are initialized. They now have an
[wxWidgets.git] / wxPython / demo / DrawXXXList.py
... / ...
CommitLineData
1# 11/5/2003 - Jeff Grimmett (grimmtooth@softhome.net)
2#
3# o Updated for wx namespace
4# o Replaced all calls to deprecated whrandom module with up to date random calls.
5# See Python docs regarding whrandom for details.
6#
7# 11/25/2003 - Jeff Grimmett (grimmtooth@softhome.net)
8#
9# o New binding
10#
11
12import random
13import time
14
15import wx
16
17#----------------------------------------------------------------------
18
19colours = [
20 "BLACK",
21 "BLUE",
22 "BLUE VIOLET",
23 "BROWN",
24 "CYAN",
25 "DARK GREY",
26 "DARK GREEN",
27 "GOLD",
28 "GREY",
29 "GREEN",
30 "MAGENTA",
31 "NAVY",
32 "PINK",
33 "RED",
34 "SKY BLUE",
35 "VIOLET",
36 "YELLOW",
37 ]
38
39#----------------------------------------------------------------------
40
41def makeRandomPoints(num, w, h):
42 pnts = []
43
44 for i in range(num):
45 x = random.randint(0, w)
46 y = random.randint(0, h)
47 pnts.append( (x,y) )
48
49 return pnts
50
51
52def makeRandomLines(num, w, h):
53 pnts = []
54
55 for i in range(num):
56 x1 = random.randint(0, w)
57 y1 = random.randint(0, h)
58 x2 = random.randint(0, w)
59 y2 = random.randint(0, h)
60 pnts.append( (x1,y1, x2,y2) )
61
62 return pnts
63
64
65def makeRandomRectangles(num, W, H):
66 rects = []
67
68 for i in range(num):
69 w = random.randint(10, W/2)
70 h = random.randint(10, H/2)
71 x = random.randint(0, W - w)
72 y = random.randint(0, H - h)
73 rects.append( (x, y, w, h) )
74
75 return rects
76
77
78def makeRandomText(num):
79 Np = 8 # number of characters in text
80 text = []
81
82 for i in range(num):
83 word = []
84
85 for i in range(Np):
86 c = chr( random.randint(48, 122) )
87 word.append( c )
88
89 text.append( "".join(word) )
90
91 return text
92
93
94def makeRandomPolygons(num, W, H):
95 Np = 8 # number of points per polygon
96 polys = []
97
98 for i in range(num):
99 poly = []
100
101 for i in range(Np):
102 x = random.randint(0, W)
103 y = random.randint(0, H)
104 poly.append( (x,y) )
105
106 polys.append( poly )
107
108 return polys
109
110
111def makeRandomPens(num, cache):
112 pens = []
113
114 for i in range(num):
115 c = random.choice(colours)
116 t = random.randint(1, 4)
117
118 if not cache.has_key( (c, t) ):
119 cache[(c, t)] = wx.Pen(c, t)
120
121 pens.append( cache[(c, t)] )
122
123 return pens
124
125
126def makeRandomBrushes(num, cache):
127 brushes = []
128
129 for i in range(num):
130 c = random.choice(colours)
131
132 if not cache.has_key(c):
133 cache[c] = wx.Brush(c)
134
135 brushes.append( cache[c] )
136
137 return brushes
138
139
140def makeRandomColors(num):
141 colors = []
142
143 for i in range(num):
144 c = random.choice(colours)
145 colors.append(wx.NamedColour(c))
146 return colors
147
148
149
150pencache = {}
151brushcache = {}
152points = None
153lines = None
154rectangles = None
155polygons = None
156text = None
157pens = None
158brushes = None
159colors1 = None
160colors2 = None
161
162
163def Init(w, h, n):
164 global pencache
165 global brushcache
166 global points
167 global lines
168 global rectangles
169 global polygons
170 global text
171 global pens
172 global brushes
173 global colors1
174 global colors2
175
176 # make some lists of random shapes
177 points = makeRandomPoints(n, w, h)
178
179 try:
180 import Numeric
181 Apoints = Numeric.array(points)
182 except:
183 pass
184
185 lines = makeRandomLines(n, w, h)
186 rectangles = makeRandomRectangles(n, w, h)
187 polygons = makeRandomPolygons(n, w, h)
188 text = makeRandomText(n)
189
190 # make some random pens and brushes
191 pens = makeRandomPens(n, pencache)
192 brushes = makeRandomBrushes(n, brushcache)
193 # make some random color lists
194 colors1 = makeRandomColors(n)
195 colors2 = makeRandomColors(n)
196
197
198
199def TestPoints(dc,log):
200 dc.BeginDrawing()
201 start = time.time()
202 dc.SetPen(wx.Pen("BLACK", 4))
203
204 dc.DrawPointList(points)
205 dc.DrawPointList(points, wx.Pen("RED", 2))
206 dc.DrawPointList(points, pens)
207
208 dc.EndDrawing()
209 log.write("DrawTime: %s seconds with DrawPointList\n" % (time.time() - start))
210
211
212def TestArrayPoints(dc,log):
213 try:
214 import Numeric
215
216 dc.BeginDrawing()
217 start = time.time()
218 dc.SetPen(wx.Pen("BLACK", 1))
219
220 for i in range(1):
221 dc.DrawPointList(Apoints)
222
223 #dc.DrawPointList(Apoints, wx.Pen("RED", 2))
224 #dc.DrawPointList(Apoints, pens)
225 dc.EndDrawing()
226 log.write("DrawTime: %s seconds with DrawPointList an Numpy Array\n" % (time.time() - start))
227 except ImportError:
228 log.write("Couldn't import Numeric")
229 pass
230
231
232def TestLines(dc,log):
233 dc.BeginDrawing()
234 start = time.time()
235
236 dc.SetPen(wx.Pen("BLACK", 2))
237 dc.DrawLineList(lines)
238 dc.DrawLineList(lines, wx.Pen("RED", 2))
239 dc.DrawLineList(lines, pens)
240
241 dc.EndDrawing()
242 log.write("DrawTime: %s seconds with DrawLineList\n" % (time.time() - start))
243
244
245def TestRectangles(dc,log):
246 dc.BeginDrawing()
247 start = time.time()
248
249 dc.SetPen( wx.Pen("BLACK",1) )
250 dc.SetBrush( wx.Brush("RED") )
251
252 dc.DrawRectangleList(rectangles)
253 dc.DrawRectangleList(rectangles,pens)
254 dc.DrawRectangleList(rectangles,pens[0],brushes)
255 dc.DrawRectangleList(rectangles,pens,brushes[0])
256 dc.DrawRectangleList(rectangles,None,brushes)
257## for i in range(10):
258## #dc.DrawRectangleList(rectangles,pens,brushes)
259## dc.DrawRectangleList(rectangles)
260
261 dc.EndDrawing()
262 log.write("DrawTime: %s seconds with DrawRectanglesList\n" % (time.time() - start))
263
264
265def TestEllipses(dc,log):
266 dc.BeginDrawing()
267 start = time.time()
268
269 dc.SetPen( wx.Pen("BLACK",1) )
270 dc.SetBrush( wx.Brush("RED") )
271
272 dc.DrawEllipseList(rectangles)
273 dc.DrawEllipseList(rectangles,pens)
274 dc.DrawEllipseList(rectangles,pens[0],brushes)
275 dc.DrawEllipseList(rectangles,pens,brushes[0])
276 dc.DrawEllipseList(rectangles,None,brushes)
277 dc.DrawEllipseList(rectangles,pens,brushes)
278
279 dc.EndDrawing()
280 log.write("DrawTime: %s seconds with DrawEllipsesList\n" % (time.time() - start))
281
282
283def TestRectanglesArray(dc,log):
284 try:
285 import Numeric
286 Apoints = Numeric.array(rectangles)
287
288 dc.BeginDrawing()
289 start = time.time()
290 dc.SetPen(wx.Pen("BLACK", 1))
291 dc.DrawRectangleList(rectangles)
292 dc.DrawRectangleList(rectangles,pens)
293 dc.DrawRectangleList(rectangles,pens[0],brushes)
294 dc.DrawRectangleList(rectangles,pens,brushes[0])
295 dc.DrawRectangleList(rectangles,None,brushes)
296## for i in range(10):
297## #dc.DrawRectangleList(rectangles,pens,brushes)
298## dc.DrawRectangleList(rectangles)
299
300 dc.EndDrawing()
301 log.write("DrawTime: %s seconds with DrawRectangleList and Numpy Array\n" % (time.time() - start))
302 except ImportError:
303 log.write("Couldn't import Numeric")
304 pass
305
306
307def TestRectanglesLoop(dc,log):
308 dc.BeginDrawing()
309
310 start = time.time()
311 dc.DrawRectangleList(rectangles,pens,brushes)
312 log.write("DrawTime: %s seconds with DrawRectanglesList\n" % (time.time() - start))
313
314 start = time.time()
315
316 for i in range(len(rectangles)):
317 dc.SetPen( pens[i] )
318 dc.SetBrush( brushes[i] )
319 dc.DrawRectangle(rectangles[i][0],rectangles[i][1],rectangles[i][2],rectangles[i][3])
320
321 dc.EndDrawing()
322 log.write("DrawTime: %s seconds with Python loop\n" % (time.time() - start))
323
324
325def TestPolygons(dc,log):
326 dc.BeginDrawing()
327
328 start = time.time()
329 dc.SetPen(wx.Pen("BLACK", 1))
330 dc.DrawPolygonList(polygons)
331 dc.DrawPolygonList(polygons,pens)
332 dc.DrawPolygonList(polygons,pens[0],brushes)
333 dc.DrawPolygonList(polygons,pens,brushes[0])
334 dc.DrawPolygonList(polygons,None,brushes)
335 log.write("DrawTime: %s seconds with DrawPolygonList\n" % (time.time() - start))
336
337 dc.EndDrawing()
338
339
340def TestText(dc,log):
341 dc.BeginDrawing()
342
343 start = time.time()
344
345 # NOTE: you need to set BackgroundMode for the background colors to be used.
346 dc.SetBackgroundMode(wx.SOLID)
347 foreground = colors1
348 background = colors2
349 dc.DrawTextList(text, points, foreground, background)
350
351 log.write("DrawTime: %s seconds with DrawTextList\n" % (time.time() - start))
352
353 dc.EndDrawing()
354
355
356
357class TestNB(wx.Notebook):
358 def __init__(self, parent, id, log):
359 style = wx.NB_BOTTOM
360
361 if wx.Platform == "__WXMAC__":
362 style = 0
363
364 wx.Notebook.__init__(self, parent, id, style=style)
365 self.log = log
366
367 # Initialize our various samples and add them to the notebook.
368 win = DrawPanel(self, TestEllipses, log)
369 self.AddPage(win, 'Ellipses')
370
371 win = DrawPanel(self, TestText, log)
372 self.AddPage(win, 'Text')
373
374 win = DrawPanel(self, TestPolygons, log)
375 self.AddPage(win, 'Polygons')
376
377 win = DrawPanel(self, TestPoints, log)
378 self.AddPage(win, 'Points')
379
380 win = DrawPanel(self, TestLines, log)
381 self.AddPage(win, 'Lines')
382
383 win = DrawPanel(self, TestRectangles, log)
384 self.AddPage(win, 'Rectangles')
385
386# Class used for all the various sample pages; the mechanics are the same
387# for each one with regards to the notebook. The only difference is
388# the function we use to draw on it.
389class DrawPanel(wx.Panel):
390 def __init__(self, parent, drawFun, log):
391 wx.Panel.__init__(self, parent, -1)
392 self.SetBackgroundColour(wx.WHITE)
393
394 self.log = log
395 self.drawFun = drawFun
396 self.Bind(wx.EVT_PAINT, self.OnPaint)
397
398
399 def OnPaint(self, evt):
400 dc = wx.PaintDC(self)
401 dc.Clear()
402 self.drawFun(dc,self.log)
403
404
405#----------------------------------------------------------------------
406
407def runTest(frame, nb, log):
408 w = nb.GetClientSize().width
409 h = nb.GetClientSize().height
410 if w < 600: w = 600
411 if h < 400: h = 400
412 Init(w, h, 200)
413 win = TestNB(nb, -1, log)
414 return win
415
416#----------------------------------------------------------------------
417
418
419overview = """\
420
421Some methods have been added to wx.DC to help with optimization of
422drawing routines. Currently they are:
423
424<pre>
425 DrawPointList(sequence, pens=None)
426</pre>
427 Where sequence is a tuple, list, whatever of 2 element tuples
428 (x, y) and pens is either None, a single pen or a list of pens.
429
430<pre>
431 DrawLineList(sequence, pens=None)
432</pre>
433 Where sequence is a tuple, list, whatever of 4 element tuples
434 (x1,y1, x2,y2) and pens is either None, a single pen or a list
435 of pens.
436
437<pre>
438 DrawRectangleList(rectangles, pens=None, brushes=None)
439</pre>
440
441
442<pre>
443 DrawEllipseList(ellipses, pens=None, brushes=None)
444</pre>
445
446
447<pre>
448 DrawPolygonList(polygons, pens=None, brushes=None)
449</pre>
450
451
452<pre>
453 DrawTextList(textList, coords, foregrounds = None, backgrounds = None)
454</pre>
455
456"""
457
458
459if __name__ == '__main__':
460 import sys,os
461 import run
462 run.main(['', os.path.basename(sys.argv[0])])
463