wxpython 之 GDI(二)

Basic primitives

Point

最简单的几何对象是一个点。它是一个窗口上平面的点。

DrawPoint(int x, int y)
#!/usr/bin/python
# -*- coding: utf-8 -*-

import wx
import random

class Points(wx.Frame):
    def __init__(self, parent, id = -1, title = 'points club'):
        wx.Frame.__init__(self, parent, id, title, size=(250, 150))

        self.Bind(wx.EVT_PAINT, self.OnPaint)

        self.Centre()
        self.Show(True)

    def OnPaint(self, event):
        dc = wx.PaintDC(self)

        dc.SetPen(wx.Pen('RED'))

        for i in range(1000):
            w, h = self.GetSize()
            x = random.randint(1, w-1)
            y = random.randint(1, h-1)
            dc.DrawPoint(x, y)
        
       
        
if __name__ == '__main__':
    app = wx.App()
    Points(None)
    app.MainLoop()

Shapes

形状是更复杂的几何对象。我们将在下面的示例绘制各种几何形状。

#!/usr/bin/python
# -*- coding: utf-8 -*-

import wx
class Shapes(wx.Frame):
    def __init__(self, parent, id = -1, title = 'Shapes'):
        wx.Frame.__init__(self, parent, id, title, size=(350, 300))

        self.Bind(wx.EVT_PAINT, self.OnPaint)

        self.Centre()
        self.Show(True)

    def OnPaint(self, event):
        dc = wx.PaintDC(self)

        dc.DrawEllipse(20, 20, 90, 60)
        dc.DrawRoundedRectangle(130, 20, 90, 60, 10)
        dc.DrawArc(240, 40, 340, 40, 290, 20)

        dc.DrawPolygon(((130, 140), (180, 170), (180, 140), (220, 110), (140, 100)))
        dc.DrawRectangle(20, 120, 80, 50)
        dc.DrawSpline(((240, 170), (280, 170), (285, 110), (325, 110)))

        dc.DrawLines(((20, 260), (100, 260), (20, 210), (100, 210)))
        dc.DrawCircle(170, 230, 35)
        dc.DrawRectangle(250, 200, 60, 60)
       
        
if __name__ == '__main__':
    app = wx.App()
    Shapes(None)
    app.MainLoop()

 

Regions

设备上下文可以分为几个部分,称为区域。区域可以是任何形状。区域可以是一个简单的矩形或圆形。与合并、 相交、 相减和 Xor 操作我们可以从简单的创建复杂的区域。区域用于大纲显示、 填充或剪辑。

我们可以通过三种方式来创建区域。最简单的方法是创建一个矩形区域。可以从位图的点列表创建更复杂的地区。

wx.Region(int x=0, int y=0, int width=0, int height=0)
wx.RegionFromPoints(list points, int fillStyle=wx.WINDING_RULE)

 

 此构造函数创建一个多边形区域。FillStyle 参数可以是 wx.WINDING_RULE 或 wx.ODDEVEN_RULE。

可以用下面的类创建更复杂的区域

wx.RegionFromBitmap(wx.Bitmap bmp)

在我们学习区域之前,我们将首先创建一个小例子。我们划分主题分成几个部分,以便更容易理解。您可能会发现它将很好的用到了你在学校里学习到的数学知识。
import wx
from math import hypot, sin, cos, pi

class Example(wx.Frame):
    def __init__(self, *args, **kw):
        super(Example, self).__init__(*args, **kw) 

        self.Bind(wx.EVT_PAINT, self.OnPaint)

        self.SetSize((350, 250))
        self.SetTitle('Lines')
        self.Centre()
        self.Show(True)

    def OnPaint(self, event):
      
        dc = wx.PaintDC(self)
        size_x, size_y = self.GetClientSizeTuple()
        dc.SetDeviceOrigin(size_x/2, size_y/2)

        radius = hypot(size_x/2, size_y/2)
        angle = 0

        while (angle < 2*pi):
            x = radius*cos(angle)
            y = radius*sin(angle)
            dc.DrawLinePoint((0, 0), (x, y))
            angle = angle + 2*pi/360

        
if __name__ == '__main__':
    app = wx.App()
    Example(None, -1, 'xxx')
    app.MainLoop()

 

之隔例子中,我们从客户区域中心点向外围画了260条线。线之间偏一度。

方法 SetDeviceOrigin() 创建一个新的开始的坐标系统。我们把它放到客户端区域的中间。通过重新定位坐标系统,我们使我们的绘图较不复杂。

 

Clipping

修剪是用来限制在某一特定区域绘图。在两种情况下使用剪辑。要创建效果,以提高应用程序的性能。我们用 SetClippingRegionAsRegion() 方法限制到某一区域的绘图。

 下面的示例中我们将修改和加强我们先前的例子。

 

#!/usr/bin/python
# -*- coding: utf-8 -*-

import wx
from math import hypot, sin, cos, pi

class Example(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(350, 300))

        self.Bind(wx.EVT_PAINT, self.OnPaint)

        self.Centre()
        self.Show(True)

    def OnPaint(self, event):
        dc = wx.PaintDC(self)

        dc.SetPen(wx.Pen('#424242'))
        size_x, size_y = self.GetClientSizeTuple()
        dc.SetDeviceOrigin(size_x/2, size_y/2)

        points = (((0, 85), (75, 75), (100, 10), (125, 75), (200, 85),
            (150, 125), (160, 190), (100, 150), (40, 190), (50, 125)))

        region = wx.RegionFromPoints(points)
        dc.SetClippingRegionAsRegion(region)

        radius = hypot(size_x/2, size_y/2)
        angle = 0

        while (angle < 2*pi):
            x = radius*cos(angle)
            y = radius*sin(angle)
            dc.DrawLinePoint((0, 0), (x, y))
            angle = angle + 2*pi/360

        dc.DestroyClippingRegion()

        
if __name__ == '__main__':
    app = wx.App()
    Example(None, -1, 'xxx')
    app.MainLoop()

 

 我们依然画了360条线,但是这次之在一个特定的区域画。这个特定的区域是用points集合生成的regin区域。

region = wx.RegionFromPoints(points)
dc.SetClippingRegionAsRegion(region)

 

我们用wx.RegionFromPoins()及points集合创建一个区域。SetClippingRegionAsRegion()方法设置绘画的区域。
dc.DestroyClippingRegion()

 

clipregion使用完后,我们必须destory it。
 

Operations over Regions

Regions可以组合成更复杂的形状。我们可以使用4类运算。合并,相交,相减 xor 运算。

下面的例子演示了这四种运算。

#!/usr/bin/python
# -*- coding: utf-8 -*-

import wx
from math import hypot, sin, cos, pi

class Example(wx.Frame):
     def __init__(self, parent, id, title):
         wx.Frame.__init__(self, parent, id, title, size=(270, 220))

         self.Bind(wx.EVT_PAINT, self.OnPaint)

         self.Centre()
         self.Show(True)

     def OnPaint(self, event):
         dc = wx.PaintDC(self)
         dc.SetPen(wx.Pen('#d4d4d4'))
         
         #First
         dc.DrawRectangle(20, 20, 50, 50)
         dc.DrawRectangle(30, 40, 50, 50)

         #Second
         dc.SetBrush(wx.Brush('#ffffff'))
         dc.DrawRectangle(100, 20, 50, 50)
         dc.DrawRectangle(110, 40, 50, 50) 
         region1 = wx.Region(100, 20, 50, 50)
         region2 = wx.Region(110, 40, 50, 50)
         region1.IntersectRegion(region2)
         rect = region1.GetBox()
         dc.SetClippingRegionAsRegion(region1)
         dc.SetBrush(wx.Brush('#ff0000'))
         dc.DrawRectangleRect(rect)
         dc.DestroyClippingRegion()

         # 3
         dc.SetBrush(wx.Brush('#ffffff'))
         dc.DrawRectangle(180, 20, 50, 50)
         dc.DrawRectangle(190, 40, 50, 50)
         region1 = wx.Region(180, 20, 50, 50)
         region2 = wx.Region(190, 40, 50, 50)
         region1.UnionRegion(region2)
         dc.SetClippingRegionAsRegion(region1)
         rect = region1.GetBox()
         dc.SetBrush(wx.Brush('#fa8e00'))
         dc.DrawRectangleRect(rect)
         dc.DestroyClippingRegion()
         
         #4
         dc.SetBrush(wx.Brush('#ffffff'))
         dc.DrawRectangle(20, 120, 50, 50)
         dc.DrawRectangle(30, 140, 50, 50)
         region1 = wx.Region(20, 120, 50, 50)
         region2 = wx.Region(30, 140, 50, 50)
         region1.XorRegion(region2)
         rect = region1.GetBox()
         dc.SetClippingRegionAsRegion(region1)
         dc.SetBrush(wx.Brush('#619e1b'))
         dc.DrawRectangleRect(rect)
         dc.DestroyClippingRegion()
         
         #5
         dc.SetBrush(wx.Brush('#ffffff'))
         dc.DrawRectangle(100, 120, 50, 50)
         dc.DrawRectangle(110, 140, 50, 50)
         region1 = wx.Region(100, 120, 50, 50)
         region2 = wx.Region(110, 140, 50, 50)
         region1.SubtractRegion(region2)
         rect = region1.GetBox()
         dc.SetClippingRegionAsRegion(region1)
         dc.SetBrush(wx.Brush('#715b33'))
         dc.DrawRectangleRect(rect)
         dc.DestroyClippingRegion()

         #6
         dc.SetBrush(wx.Brush('#ffffff'))
         dc.DrawRectangle(180, 120, 50, 50)
         dc.DrawRectangle(190, 140, 50, 50)
         region1 = wx.Region(180, 120, 50, 50)
         region2 = wx.Region(190, 140, 50, 50)
         region2.SubtractRegion(region1)
         rect = region2.GetBox()
         dc.SetClippingRegionAsRegion(region2)
         dc.SetBrush(wx.Brush('#0d0060'))
         dc.DrawRectangleRect(rect)
         dc.DestroyClippingRegion()

        
if __name__ == '__main__':
    app = wx.App()
    Example(None, -1, 'xxx')
    app.MainLoop()

Mapping modes

 

 

posted on 2012-12-30 16:44  |残阳|露  阅读(1321)  评论(0编辑  收藏  举报

导航