I could please use some help. I'm not sure what's going on with my
code. Each time OnPaint is called the new text is drawn over the old
text resulting in ugly text. I can move the text around on the screen
so it appears dc.Clear is working but this text issue is really
bugging me. Any ideas?
def OnPaint(self, event):
"""
Redraw image to screen and save if self.save_fd != None
"""
dc = wx.PaintDC(self)
dc.Clear()
for link in self.state.GetLinks():
# Update Drawing code
src_pos = link.GetSrcPos()
dst_pos = link.GetDstPos()
dc.SetPen(wx.Pen(wx.Colour(58,58,58), 2))
if link.Viewable() or self.show_ports:
s = 'src_port: ' + str(link.GetSrcPort()) +
'\ndst_port: ' + str(link.GetDstPort())
x = (src_pos[0]+dst_pos[0])/2 - 20
y = (src_pos[1]+dst_pos[1])/2 - 10
dc.DrawText(s, x, y)
dc.DrawLine(src_pos[0], src_pos[1],
dst_pos[0], dst_pos[1])
When the system uses anti-aliasing to smooth the edges of the drawn text it does so by blending some of the pixels at the edge of the characters with the pixels already present around them on the window. So when you redraw the same text at the same position those pixels are being blended with something different this time (the pixels that were already blended the last time) and so you end up with something that looks worse on each redraw, getting more blocky or muddy each time.
The key is to also redraw the background behind the text in your paint event, as well as the text itself. Using Clear() is one way to do that, but it can lead to flicker depending on the platform and what DCs are being used. Doing double buffering is one way around that.
···
On 3/21/12 2:43 PM, lost wrote:
I could please use some help. I'm not sure what's going on with my
code. Each time OnPaint is called the new text is drawn over the old
text resulting in ugly text. I can move the text around on the screen
so it appears dc.Clear is working but this text issue is really
bugging me. Any ideas?
I could please use some help. I'm not sure what's going on with my
code. Each time OnPaint is called the new text is drawn over the old
text resulting in ugly text. I can move the text around on the screen
so it appears dc.Clear is working but this text issue is really
bugging me. Any ideas?
For efficiency, a PaintDC includes a "clipping region" of only those
parts of the screen that absolutely need to be recreated. Whatever
drawing you do is drawn through that mask, so that parts that didn't
need to be redrawn aren't touched. That can produce unusual results if
the contents vary from call to call. The job of OnPaint is to recreate
the existing image in case part of it was lost. If your app changes its
state so that the current image needs to be redrawn completely, call
self.Refresh(). Don't call that in the OnPaint handler, though -- that
causes an infinite loop.
Alternatively, you can try
dc = wx.PaintDC(self)
dc = wx.ClientDC(self)
The ClientDC covers the entire window, ignoring the clipping region.
You still need to create the PaintDC, in order to clear the "window is
dirty" flag.
···
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
Thank you all for the help. I ended up using dc.GetTextExtent and
dc.DrawRectangle to ensure that the background was clear of any pixels
prior to redrawing the text. I don't think my solution is very good,
but this section of code will be replaced soon anyway.
···
On Mar 21, 5:43 pm, lost <lostpunis...@gmail.com> wrote:
I could please use some help. I'm not sure what's going on with my
code. Each time OnPaint is called the new text is drawn over the old
text resulting in ugly text. I can move the text around on the screen
so it appears dc.Clear is working but this text issue is really
bugging me. Any ideas?
def OnPaint\(self, event\):
"""
Redraw image to screen and save if self\.save\_fd \!= None
"""
dc = wx\.PaintDC\(self\)
dc\.Clear\(\)
for link in self\.state\.GetLinks\(\):
\# Update Drawing code
src\_pos = link\.GetSrcPos\(\)
dst\_pos = link\.GetDstPos\(\)
dc\.SetPen\(wx\.Pen\(wx\.Colour\(58,58,58\), 2\)\)
if link\.Viewable\(\) or self\.show\_ports:
s = 'src\_port: ' \+ str\(link\.GetSrcPort\(\)\) \+