-
Notifications
You must be signed in to change notification settings - Fork 21
/
BlitTest.py
executable file
·101 lines (76 loc) · 2.67 KB
/
BlitTest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python
"""
demo oif blitting from a MemoryDC to the screen using DC.DrawBitmap
"""
from __future__ import print_function, unicode_literals
import random
import wx
class MainWindow(wx.Frame):
""" This window displays some random lines """
def __init__(self, parent, id, title):
wx.Frame.__init__(
self,
None,
-1,
"Blit Test",
wx.DefaultPosition,
size=(500, 500),
style=wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)
wx.EVT_CLOSE(self, self.OnQuit)
self.Bind(wx.EVT_TIMER, self.OnTimer)
self.Bind(wx.EVT_SIZE, self.BuildImage)
self.Numtimer = 0
self.NumLines = 100
self.t = wx.Timer(self)
self.BuildImage()
self.t.Start(100)
def OnQuit(self, Event):
self.Destroy()
def BuildImage(self, event=None):
Size = self.ClientSize
# Make new offscreen bitmap: this bitmap will always have the
# current drawing in it, so it can be used to save the image to
# a file, or whatever.
print("making new buffer:", Size)
self._Buffer = wx.EmptyBitmap(Size[0], Size[1])
dc = wx.MemoryDC()
dc.SelectObject(self._Buffer)
self.Lines = []
for i in range(self.NumLines):
x1, y1, x2, y2 = (random.randint(1, max(Size)),
random.randint(1, max(Size)),
random.randint(1, max(Size)),
random.randint(1, max(Size)))
color = self.random_color()
self.Lines.append([color, (x1, y1, x2, y2)])
dc.Clear()
for line in self.Lines:
dc.SetPen(wx.Pen(line[0], 2))
dc.DrawLine(*line[1])
def OnTimer(self, event):
self.Numtimer += 1
print("Timer fired: %i times" % self.Numtimer)
# change one color:
self.Lines[random.randrange(self.NumLines)][0] = self.random_color()
# update the screen
dc = wx.MemoryDC()
dc.SelectObject(self._Buffer)
dc.Clear()
for line in self.Lines:
dc.SetPen(wx.Pen(line[0], 2))
dc.DrawLine(*line[1])
# del dc
wx.ClientDC(self).DrawBitmap(self._Buffer, 0, 0)
def random_color(self):
return wx.Colour(random.randrange(255),
random.randrange(255),
random.randrange(255),
)
class MyApp(wx.App):
def OnInit(self):
frame = MainWindow(None, wx.ID_ANY, title="BlitTest")
self.SetTopWindow(frame)
frame.Show()
return True
app = MyApp(0)
app.MainLoop()