]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/DrawXXXList.py
7 #----------------------------------------------------------------------
29 #----------------------------------------------------------------------
31 def makeRandomPoints(num
, w
, h
):
35 x
= random
.randint(0, w
)
36 y
= random
.randint(0, h
)
42 def makeRandomLines(num
, w
, h
):
46 x1
= random
.randint(0, w
)
47 y1
= random
.randint(0, h
)
48 x2
= random
.randint(0, w
)
49 y2
= random
.randint(0, h
)
50 pnts
.append( (x1
,y1
, x2
,y2
) )
55 def makeRandomRectangles(num
, W
, H
):
59 w
= random
.randint(10, W
/2)
60 h
= random
.randint(10, H
/2)
61 x
= random
.randint(0, W
- w
)
62 y
= random
.randint(0, H
- h
)
63 rects
.append( (x
, y
, w
, h
) )
68 def makeRandomText(num
):
69 Np
= 8 # number of characters in text
76 c
= chr( random
.randint(48, 122) )
79 text
.append( "".join(word
) )
84 def makeRandomPolygons(num
, W
, H
):
85 Np
= 8 # number of points per polygon
92 x
= random
.randint(0, W
)
93 y
= random
.randint(0, H
)
101 def makeRandomPens(num
, cache
):
105 c
= random
.choice(colours
)
106 t
= random
.randint(1, 4)
108 if not cache
.has_key( (c
, t
) ):
109 cache
[(c
, t
)] = wx
.Pen(c
, t
)
111 pens
.append( cache
[(c
, t
)] )
116 def makeRandomBrushes(num
, cache
):
120 c
= random
.choice(colours
)
122 if not cache
.has_key(c
):
123 cache
[c
] = wx
.Brush(c
)
125 brushes
.append( cache
[c
] )
130 def makeRandomColors(num
):
134 c
= random
.choice(colours
)
135 colors
.append(wx
.NamedColour(c
))
166 # make some lists of random shapes
167 points
= makeRandomPoints(n
, w
, h
)
171 Apoints
= Numeric
.array(points
)
175 lines
= makeRandomLines(n
, w
, h
)
176 rectangles
= makeRandomRectangles(n
, w
, h
)
177 polygons
= makeRandomPolygons(n
, w
, h
)
178 text
= makeRandomText(n
)
180 # make some random pens and brushes
181 pens
= makeRandomPens(n
, pencache
)
182 brushes
= makeRandomBrushes(n
, brushcache
)
183 # make some random color lists
184 colors1
= makeRandomColors(n
)
185 colors2
= makeRandomColors(n
)
189 def TestPoints(dc
,log
):
192 dc
.SetPen(wx
.Pen("BLACK", 4))
194 dc
.DrawPointList(points
)
195 dc
.DrawPointList(points
, wx
.Pen("RED", 2))
196 dc
.DrawPointList(points
, pens
)
199 log
.write("DrawTime: %s seconds with DrawPointList\n" % (time
.time() - start
))
202 def TestArrayPoints(dc
,log
):
208 dc
.SetPen(wx
.Pen("BLACK", 1))
211 dc
.DrawPointList(Apoints
)
213 #dc.DrawPointList(Apoints, wx.Pen("RED", 2))
214 #dc.DrawPointList(Apoints, pens)
216 log
.write("DrawTime: %s seconds with DrawPointList an Numpy Array\n" % (time
.time() - start
))
218 log
.write("Couldn't import Numeric")
222 def TestLines(dc
,log
):
226 dc
.SetPen(wx
.Pen("BLACK", 2))
227 dc
.DrawLineList(lines
)
228 dc
.DrawLineList(lines
, wx
.Pen("RED", 2))
229 dc
.DrawLineList(lines
, pens
)
232 log
.write("DrawTime: %s seconds with DrawLineList\n" % (time
.time() - start
))
235 def TestRectangles(dc
,log
):
239 dc
.SetPen( wx
.Pen("BLACK",1) )
240 dc
.SetBrush( wx
.Brush("RED") )
242 dc
.DrawRectangleList(rectangles
)
243 dc
.DrawRectangleList(rectangles
,pens
)
244 dc
.DrawRectangleList(rectangles
,pens
[0],brushes
)
245 dc
.DrawRectangleList(rectangles
,pens
,brushes
[0])
246 dc
.DrawRectangleList(rectangles
,None,brushes
)
247 ## for i in range(10):
248 ## #dc.DrawRectangleList(rectangles,pens,brushes)
249 ## dc.DrawRectangleList(rectangles)
252 log
.write("DrawTime: %s seconds with DrawRectanglesList\n" % (time
.time() - start
))
255 def TestEllipses(dc
,log
):
259 dc
.SetPen( wx
.Pen("BLACK",1) )
260 dc
.SetBrush( wx
.Brush("RED") )
262 dc
.DrawEllipseList(rectangles
)
263 dc
.DrawEllipseList(rectangles
,pens
)
264 dc
.DrawEllipseList(rectangles
,pens
[0],brushes
)
265 dc
.DrawEllipseList(rectangles
,pens
,brushes
[0])
266 dc
.DrawEllipseList(rectangles
,None,brushes
)
267 dc
.DrawEllipseList(rectangles
,pens
,brushes
)
270 log
.write("DrawTime: %s seconds with DrawEllipsesList\n" % (time
.time() - start
))
273 def TestRectanglesArray(dc
,log
):
276 Apoints
= Numeric
.array(rectangles
)
280 dc
.SetPen(wx
.Pen("BLACK", 1))
281 dc
.DrawRectangleList(rectangles
)
282 dc
.DrawRectangleList(rectangles
,pens
)
283 dc
.DrawRectangleList(rectangles
,pens
[0],brushes
)
284 dc
.DrawRectangleList(rectangles
,pens
,brushes
[0])
285 dc
.DrawRectangleList(rectangles
,None,brushes
)
286 ## for i in range(10):
287 ## #dc.DrawRectangleList(rectangles,pens,brushes)
288 ## dc.DrawRectangleList(rectangles)
291 log
.write("DrawTime: %s seconds with DrawRectangleList and Numpy Array\n" % (time
.time() - start
))
293 log
.write("Couldn't import Numeric")
297 def TestRectanglesLoop(dc
,log
):
301 dc
.DrawRectangleList(rectangles
,pens
,brushes
)
302 log
.write("DrawTime: %s seconds with DrawRectanglesList\n" % (time
.time() - start
))
306 for i
in range(len(rectangles
)):
308 dc
.SetBrush( brushes
[i
] )
309 dc
.DrawRectangle(rectangles
[i
][0],rectangles
[i
][1],rectangles
[i
][2],rectangles
[i
][3])
312 log
.write("DrawTime: %s seconds with Python loop\n" % (time
.time() - start
))
315 def TestPolygons(dc
,log
):
319 dc
.SetPen(wx
.Pen("BLACK", 1))
320 dc
.DrawPolygonList(polygons
)
321 dc
.DrawPolygonList(polygons
,pens
)
322 dc
.DrawPolygonList(polygons
,pens
[0],brushes
)
323 dc
.DrawPolygonList(polygons
,pens
,brushes
[0])
324 dc
.DrawPolygonList(polygons
,None,brushes
)
325 log
.write("DrawTime: %s seconds with DrawPolygonList\n" % (time
.time() - start
))
330 def TestText(dc
,log
):
335 # NOTE: you need to set BackgroundMode for the background colors to be used.
336 dc
.SetBackgroundMode(wx
.SOLID
)
339 dc
.DrawTextList(text
, points
, foreground
, background
)
341 log
.write("DrawTime: %s seconds with DrawTextList\n" % (time
.time() - start
))
347 class TestNB(wx
.Notebook
):
348 def __init__(self
, parent
, id, log
):
351 if wx
.Platform
== "__WXMAC__":
354 wx
.Notebook
.__init
__(self
, parent
, id, style
=style
)
357 # Initialize our various samples and add them to the notebook.
358 win
= DrawPanel(self
, TestEllipses
, log
)
359 self
.AddPage(win
, 'Ellipses')
361 win
= DrawPanel(self
, TestText
, log
)
362 self
.AddPage(win
, 'Text')
364 win
= DrawPanel(self
, TestPolygons
, log
)
365 self
.AddPage(win
, 'Polygons')
367 win
= DrawPanel(self
, TestPoints
, log
)
368 self
.AddPage(win
, 'Points')
370 win
= DrawPanel(self
, TestLines
, log
)
371 self
.AddPage(win
, 'Lines')
373 win
= DrawPanel(self
, TestRectangles
, log
)
374 self
.AddPage(win
, 'Rectangles')
376 # Class used for all the various sample pages; the mechanics are the same
377 # for each one with regards to the notebook. The only difference is
378 # the function we use to draw on it.
379 class DrawPanel(wx
.Panel
):
380 def __init__(self
, parent
, drawFun
, log
):
381 wx
.Panel
.__init
__(self
, parent
, -1)
382 self
.SetBackgroundColour(wx
.WHITE
)
385 self
.drawFun
= drawFun
386 self
.Bind(wx
.EVT_PAINT
, self
.OnPaint
)
389 def OnPaint(self
, evt
):
390 dc
= wx
.PaintDC(self
)
392 self
.drawFun(dc
,self
.log
)
395 #----------------------------------------------------------------------
397 def runTest(frame
, nb
, log
):
398 w
= nb
.GetClientSize().width
399 h
= nb
.GetClientSize().height
403 win
= TestNB(nb
, -1, log
)
406 #----------------------------------------------------------------------
411 Some methods have been added to wx.DC to help with optimization of
412 drawing routines. Currently they are:
415 DrawPointList(sequence, pens=None)
417 Where sequence is a tuple, list, whatever of 2 element tuples
418 (x, y) and pens is either None, a single pen or a list of pens.
421 DrawLineList(sequence, pens=None)
423 Where sequence is a tuple, list, whatever of 4 element tuples
424 (x1,y1, x2,y2) and pens is either None, a single pen or a list
428 DrawRectangleList(rectangles, pens=None, brushes=None)
433 DrawEllipseList(ellipses, pens=None, brushes=None)
438 DrawPolygonList(polygons, pens=None, brushes=None)
443 DrawTextList(textList, coords, foregrounds = None, backgrounds = None)
449 if __name__
== '__main__':
452 run
.main(['', os
.path
.basename(sys
.argv
[0])])