]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/DrawXXXList.py
2 from wxPython
.wx
import *
5 #----------------------------------------------------------------------
27 #----------------------------------------------------------------------
29 def makeRandomPoints(num
, w
, h
):
32 x
= whrandom
.randint(0, w
)
33 y
= whrandom
.randint(0, h
)
38 def makeRandomLines(num
, w
, h
):
41 x1
= whrandom
.randint(0, w
)
42 y1
= whrandom
.randint(0, h
)
43 x2
= whrandom
.randint(0, w
)
44 y2
= whrandom
.randint(0, h
)
45 pnts
.append( (x1
,y1
, x2
,y2
) )
49 def makeRandomRectangles(num
, W
, H
):
52 w
= whrandom
.randint(10, W
/2)
53 h
= whrandom
.randint(10, H
/2)
54 x
= whrandom
.randint(0, W
- w
)
55 y
= whrandom
.randint(0, H
- h
)
56 rects
.append( (x
, y
, w
, h
) )
60 def makeRandomText(num
):
61 Np
= 8 # number of charcters in text
66 c
= chr( whrandom
.randint(48, 122) )
68 text
.append( "".join(word
) )
72 def makeRandomPolygons(num
, W
, H
):
73 Np
= 8 # number of points per polygon
78 x
= whrandom
.randint(0, W
)
79 y
= whrandom
.randint(0, H
)
86 def makeRandomPens(num
, cache
):
89 c
= whrandom
.choice(colours
)
90 t
= whrandom
.randint(1, 4)
91 if not cache
.has_key( (c
, t
) ):
92 cache
[(c
, t
)] = wxPen(c
, t
)
93 pens
.append( cache
[(c
, t
)] )
97 def makeRandomBrushes(num
, cache
):
100 c
= whrandom
.choice(colours
)
101 if not cache
.has_key(c
):
102 cache
[c
] = wxBrush(c
)
103 brushes
.append( cache
[c
] )
107 def makeRandomColors(num
):
110 c
= whrandom
.choice(colours
)
111 colors
.append(wxNamedColor(c
))
142 # make some lists of random shapes
143 points
= makeRandomPoints(n
, w
, h
)
146 Apoints
= Numeric
.array(points
)
149 lines
= makeRandomLines(n
, w
, h
)
150 rectangles
= makeRandomRectangles(n
, w
, h
)
151 polygons
= makeRandomPolygons(n
, w
, h
)
152 text
= makeRandomText(n
)
154 # make some random pens and brushes
155 pens
= makeRandomPens(n
, pencache
)
156 brushes
= makeRandomBrushes(n
, brushcache
)
157 # make some random color lists
158 colors1
= makeRandomColors(n
)
159 colors2
= makeRandomColors(n
)
163 def TestPoints(dc
,log
):
166 dc
.SetPen(wxPen("BLACK", 4))
168 dc
.DrawPointList(points
)
169 dc
.DrawPointList(points
, wxPen("RED", 2))
170 dc
.DrawPointList(points
, pens
)
173 log
.write("DrawTime: %s seconds with DrawPointList\n" % (time
.time() - start
))
176 def TestArrayPoints(dc
,log
):
182 dc
.SetPen(wxPen("BLACK", 1))
184 dc
.DrawPointList(Apoints
)
185 #dc.DrawPointList(Apoints, wxPen("RED", 2))
186 #dc.DrawPointList(Apoints, pens)
188 log
.write("DrawTime: %s seconds with DrawPointList an Numpy Array\n" % (time
.time() - start
))
190 log
.write("Couldn't import Numeric")
194 def TestLines(dc
,log
):
198 dc
.SetPen(wxPen("BLACK", 2))
199 dc
.DrawLineList(lines
)
200 dc
.DrawLineList(lines
, wxPen("RED", 2))
201 dc
.DrawLineList(lines
, pens
)
204 log
.write("DrawTime: %s seconds with DrawLineList\n" % (time
.time() - start
))
207 def TestRectangles(dc
,log
):
211 dc
.SetPen( wxPen("BLACK",1) )
212 dc
.SetBrush( wxBrush("RED") )
214 dc
.DrawRectangleList(rectangles
)
215 dc
.DrawRectangleList(rectangles
,pens
)
216 dc
.DrawRectangleList(rectangles
,pens
[0],brushes
)
217 dc
.DrawRectangleList(rectangles
,pens
,brushes
[0])
218 dc
.DrawRectangleList(rectangles
,None,brushes
)
219 ## for i in range(10):
220 ## #dc.DrawRectangleList(rectangles,pens,brushes)
221 ## dc.DrawRectangleList(rectangles)
224 log
.write("DrawTime: %s seconds with DrawRectanglesList\n" % (time
.time() - start
))
227 def TestEllipses(dc
,log
):
231 dc
.SetPen( wxPen("BLACK",1) )
232 dc
.SetBrush( wxBrush("RED") )
234 dc
.DrawEllipseList(rectangles
)
235 dc
.DrawEllipseList(rectangles
,pens
)
236 dc
.DrawEllipseList(rectangles
,pens
[0],brushes
)
237 dc
.DrawEllipseList(rectangles
,pens
,brushes
[0])
238 dc
.DrawEllipseList(rectangles
,None,brushes
)
239 dc
.DrawEllipseList(rectangles
,pens
,brushes
)
242 log
.write("DrawTime: %s seconds with DrawEllipsesList\n" % (time
.time() - start
))
245 def TestRectanglesArray(dc
,log
):
248 Apoints
= Numeric
.array(rectangles
)
252 dc
.SetPen(wxPen("BLACK", 1))
253 dc
.DrawRectangleList(rectangles
)
254 dc
.DrawRectangleList(rectangles
,pens
)
255 dc
.DrawRectangleList(rectangles
,pens
[0],brushes
)
256 dc
.DrawRectangleList(rectangles
,pens
,brushes
[0])
257 dc
.DrawRectangleList(rectangles
,None,brushes
)
258 ## for i in range(10):
259 ## #dc.DrawRectangleList(rectangles,pens,brushes)
260 ## dc.DrawRectangleList(rectangles)
263 log
.write("DrawTime: %s seconds with DrawRectangleList and Numpy Array\n" % (time
.time() - start
))
265 log
.write("Couldn't import Numeric")
269 def TestRectanglesLoop(dc
,log
):
273 dc
.DrawRectangleList(rectangles
,pens
,brushes
)
274 log
.write("DrawTime: %s seconds with DrawRectanglesList\n" % (time
.time() - start
))
277 for i
in range(len(rectangles
)):
279 dc
.SetBrush( brushes
[i
] )
280 dc
.DrawRectangle(rectangles
[i
][0],rectangles
[i
][1],rectangles
[i
][2],rectangles
[i
][3])
282 log
.write("DrawTime: %s seconds with Python loop\n" % (time
.time() - start
))
285 def TestPolygons(dc
,log
):
289 dc
.SetPen(wxPen("BLACK", 1))
290 dc
.DrawPolygonList(polygons
)
291 dc
.DrawPolygonList(polygons
,pens
)
292 dc
.DrawPolygonList(polygons
,pens
[0],brushes
)
293 dc
.DrawPolygonList(polygons
,pens
,brushes
[0])
294 dc
.DrawPolygonList(polygons
,None,brushes
)
295 log
.write("DrawTime: %s seconds with DrawPolygonList\n" % (time
.time() - start
))
300 def TestText(dc
,log
):
305 # NOTE: you need to set BackgroundMode for the background colors to be used.
306 dc
.SetBackgroundMode(wxSOLID
)
309 dc
.DrawTextList(text
, points
, foreground
, background
)
311 log
.write("DrawTime: %s seconds with DrawTextList\n" % (time
.time() - start
))
317 class TestNB(wxNotebook
):
318 def __init__(self
, parent
, id, log
):
320 if wxPlatform
== "__WXMAC__":
322 wxNotebook
.__init
__(self
, parent
, id, style
=style
)
325 win
= DrawPanel(self
, TestEllipses
, log
)
326 self
.AddPage(win
, 'Ellipses')
328 win
= DrawPanel(self
, TestText
, log
)
329 self
.AddPage(win
, 'Text')
331 win
= DrawPanel(self
, TestPolygons
, log
)
332 self
.AddPage(win
, 'Polygons')
334 win
= DrawPanel(self
, TestPoints
, log
)
335 self
.AddPage(win
, 'Points')
337 win
= DrawPanel(self
, TestLines
, log
)
338 self
.AddPage(win
, 'Lines')
340 win
= DrawPanel(self
, TestRectangles
, log
)
341 self
.AddPage(win
, 'Rectangles')
344 class DrawPanel(wxPanel
):
345 def __init__(self
, parent
, drawFun
, log
):
346 wxPanel
.__init
__(self
, parent
, -1)
347 self
.SetBackgroundColour(wxWHITE
)
350 self
.drawFun
= drawFun
351 EVT_PAINT(self
, self
.OnPaint
)
354 def OnPaint(self
, evt
):
357 self
.drawFun(dc
,self
.log
)
360 #----------------------------------------------------------------------
362 def runTest(frame
, nb
, log
):
363 w
= nb
.GetClientSize().width
364 h
= nb
.GetClientSize().height
368 win
= TestNB(nb
, -1, log
)
371 #----------------------------------------------------------------------
376 Some methods have been added to wxDC to help with optimization of
377 drawing routines. Currently they are:
380 DrawPointList(sequence, pens=None)
382 Where sequence is a tuple, list, whatever of 2 element tuples
383 (x, y) and pens is either None, a single pen or a list of pens.
386 DrawLineList(sequence, pens=None)
388 Where sequence is a tuple, list, whatever of 4 element tuples
389 (x1,y1, x2,y2) andd pens is either None, a single pen or a list
393 DrawRectangleList(rectangles, pens=None, brushes=None)
398 DrawEllipseList(ellipses, pens=None, brushes=None)
403 DrawPolygonList(polygons, pens=None, brushes=None)
408 DrawTextList(textList, coords, foregrounds = None, backgrounds = None)
414 if __name__
== '__main__':
417 run
.main(['', os
.path
.basename(sys
.argv
[0])])