How do I manage the foreground drawing pen

The color and style of the lines you draw on your device context are controlled by the current pen, which is an instance of the class wx.Pen. To get the current pen, use the device context method GetPen(). Not surprisingly, you can set the current pen with SetPen(pen).

The wx.Pen class is a simple structure with several properties related to drawing lines. The constructor for wx.Pen lets you set some of the properties.

wx.Pen(colour, width=1, style=wx.SOLID)

The colour parameter is either a wx.Colour object or any of the things that can be automatically converted to one—a tuple of RGB values, a string color name, or a string of RGB hex values such as "#12C588". The width is the width of the pen line, in pixels. The style controls the way in which the line is displayed. Table 12.5 displays the legal values of the style parameter—note that not all styles are supported on all platforms.

Table 12.5 Drawing styles of wx.Pen

Style

Description

wx.BDIAGONAL_HATCH

The pen will draw backward (northwest to southeast) hatch lines.

wx.CROSSDIAG_HATCH

A combination of wx.bdiagonal_hatch and wx.fdiagonal_hatch— in other words, it creates x shapes.

wx.CROSS_HATCH

Crosshatched + shapes.

wx.DOT

Small dots.

wx.DOT_DASH

Alternating between small dots and longer dashes.

wx.FDIAGONAL_HATCH

The pen will draw forward (southwest to northeast) hatch lines.

wx.HORIZONTAL_HATCH

Short, horizontal hash lines.

wx.LONG_DASH

Long dashes.

CHAPTER 12

Manipulating basic graphical images Table 12.5 Drawing styles of wx.Pen (continued)

CHAPTER 12

Manipulating basic graphical images Table 12.5 Drawing styles of wx.Pen (continued)

Style

Description

wx.SHORT_DASH

Shorter dashes.

wx.SOLID

A solid line. This is the default.

wx.STIPPLE

Use a provided bitmap as the pen stroke.

wx.TRANSPARENT

Do not draw any lines.

wx.USER_DASH

Use the provided dash pattern.

wx.VERTICAL_HATCH

Vertical hash lines.

In addition to the complete constructor, there are a number of predefined pens, listed next. The description of the pens should be clear from the name—any attribute not specified in the pen name will be set the default value.

wx.BLACK_DASHED_PEN

wx.BLACK_PEN

wx.CYAN_PEN

wx.GREEN_PEN

wx.GREY_PEN

wx.LIGHT_GREY_PEN

wx.MEDIUM_GREY_PEN

wx.RED_PEN

wx.TRANSPARENT_PEN

wx.WHITE PEN

The color, width, and style properties of the pen are accessible after creation with the normal property method names—GetColour(), SetColour(color), Get-Width(), SetWidth(width), GetStyle(), and SetStyle(). There are a couple of other properties that you can change in the pen. One is the end cap, the style that the pen uses at the end of a line. Typically, this is only visible for lines that are wider than one pixel. The methods are GetCap() and SetCap(cap). The valid values for the cap are: wx.cap_butt, which ends the line with a straight edge, wx.cap_projecting, which puts a square projection on the end of the line, and wx.cap_round, which rounds off the end of the line with a semicircle. The default is wx.CAP_ROUND. Similarly, you can define the join style, which is what the pen draws when two lines meet. The methods for this are wx.GetJoin(), and wx.Set-Join(join). The legal join values are: wx.JOlN_BEVEL, where the outside corners of the two lines are merely connected with a straight line, wx.join_miter, where the outer edges of the two lines are continued until they meet at a point, and wx.JoIN_RoUND, which places a circular arc over the meeting points to round off the corner. The default is wx.join_round.

If you create the pen with the wx.user_dash style, you can set the specific style of the dash with the methods GetDashes() and SetDashes(dashes). The dashes parameter is a list of 16-bit strings of ones and zeroes. Each dash is interpreted with a 1 being a drawn pixel and a 0 being an undrawn pixel. The user dash pen style cycles through the provided dashes when drawing a pen line. If the pen has the wx.stipple style, you can set a bitmap to be used as the stipple with Set-Stipple(stipple), and you can recover the bitmap with GetStipple().

When you are drawing with a pen (or blitting from another bitmap), the exact algorithm used to set the pixel in the destination context is called the logical function, and you can set it with SetLogicalFunction(function). The default value is wx.copy, which places the source color as-is onto the destination. The other logical functions perform various bitwise operations using the color values of the source and destination. The most commonly used operations are wx.XOR and wx.invert, both of which can be used to manage rubber-banding or other graphic mechanisms where you need to set a pixel to another color temporarily, and then set it right back. Those are far from the only functions offered by wxPython, however. Table 12.6 displays the complete list, with the algorithms written out in Python bitwise operators.

Following is a quick just-in-time refresher on Python bitwise operators.

& bitwise and

| bitwise or

^ bitwise exclusive or

~ bitwise complement

Table 12.6 Logical copy functions

Function

Algorithm

wx.AND

source & destination

wx.AND_INVERT

—source & destination

wx.AND_REVERSE

source & —destination

wx.CLEAR

All pixels set to 0—black

wx.COPY

source

wx. EQUIV

—source ~ destination

Table 12.6 Logical copy functions (continued)

Function

Algorithm

wx. INVERT

—destination

wx.NAND

—source | —destination

wx.NOR

—source & —destination

wx.NO_OP

destination

wx.OR

source | destination

wx.OR_INVERT

—source | destination

wx.OR_REVERSE

source | —destination

wx.SET

All pixels set to 1—white

wx.SRC_INVERT

—source

wx.XOR

source ~ destination

Again, the logical function applies any time pixels are drawn to the device context, whether using the draw functions or blit.

Was this article helpful?

0 -1

Post a comment