Problem with device context program - doesn't display what i want

hello, well, i just wrote a small program testing device context, it
for some reason doesn't display as expected. The problem being that it
only display a very small block of rectangle on the top left corner
when i told it to draw dc.DrawRectangle(100, 100, 500, 500)

the program is attached, can someone please explain to me what is wrong?

Thank you very much!

test.py (997 Bytes)

···

--
"It's a wonderful world hobbes ol' buddy, let's go explorin'!" -
Calvin in "Calvin and Hobbes"

Jason Wang wrote:

hello, well, i just wrote a small program testing device context, it
for some reason doesn't display as expected. The problem being that it
only display a very small block of rectangle on the top left corner
when i told it to draw dc.DrawRectangle(100, 100, 500, 500)

the program is attached, can someone please explain to me what is wrong?

Your buffer is initialized when the window is first created, and at that time it is very small by default. You'll want to also capture the EVT_SIZE event and recreate your buffer at that time with the new size.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Robin Dunn wrote:

Your buffer is initialized when the window is first created, and at that time it is very small by default. You'll want to also capture the EVT_SIZE event and recreate your buffer at that time with the new size.

see:

http://wiki.wxpython.org/index.cgi/DoubleBufferedDrawing

For a complete example.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

well, i recently purchased the book wxPython in action, so i'm reading
that book about it, but i still can't make it work even if i added the
OnSize() method and attached the event, attached is my code.

Also, i have another question, why does some of the code i read has
dc.BeginDrawing() and dc.EndDrawing()? Do i have to have that around
my Draw() method?

I also read the DoubleBufferedDrawing page, it is apparently a general
class which you can subclass and work with it, i didn't get it to
work, and i prob need to understand my mistake since it looks pretty
different from mine.

Thanks alot!

···

On 9/18/06, Robin Dunn <robin@alldunn.com> wrote:

Your buffer is initialized when the window is first created, and at that
time it is very small by default. You'll want to also capture the
EVT_SIZE event and recreate your buffer at that time with the new size.

--
"It's a wonderful world hobbes ol' buddy, let's go explorin'!" -
Calvin in "Calvin and Hobbes"

Jason Wang wrote:

I still can't make it work even if i added the
OnSize() method and attached the event, attached is my code.

The attachment didn't come through.

Also, i have another question, why does some of the code i read has
dc.BeginDrawing() and dc.EndDrawing()? Do i have to have that around
my Draw() method?

NO, you don't. Those calls once provided some optimization on some platforms, but I think they do nothing anywhere now. In any case, your code should work with or without them.

I also read the DoubleBufferedDrawing page, it is apparently a general
class which you can subclass and work with it, i didn't get it to
work,

Darn. Platform, Version? what "didn't work"

It's a pretty simple class really. It could well be useful to subclass, but was designed as a teaching example, more than anything.

> It looks pretty different from mine.

A little extra stuff, the but the basics are the same.

by the way, I find it's easiest to learn this stuff by adding things a little at a time. Your post said you were trying to learn about DCs. Why not start with a non-buffered example, so you can figure out the drawing first, then apply it to double buffering. About the easiest example I can come up with for drawing is:

#!/usr/bin/env python2.4

import wx

class DemoFrame(wx.Frame):
     """ This window does some simple drawing """
     def __init__(self, *args, **kwargs):
         wx.Frame.__init__(self, *args, **kwargs)

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

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

         dc.SetPen(wx.BLACK_PEN)
         dc.SetBrush(wx.RED_BRUSH)
         dc.DrawRectangle(20,30, 100, 40)

app = wx.App()
frame = DemoFrame(None, size = (200, 200))
frame.Show()
app.MainLoop()

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Jason Wang wrote:

I still can't make it work even if i added the

OnSize() method and attached the event, attached is my code.

I didn't get your code, but here is a version that works:

-Chris

test.py (1.33 KB)

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

I'm on a role here!

Jason Wang wrote:

I also read the DoubleBufferedDrawing page, it is apparently a general
class which you can subclass and work with it, i didn't get it to
work.

Enclosed is a slightly cleaned-up version, that works just fine for me on wxGTK 2.6.3. Please let me know if it doesn't work for you.

If it does, maybe you could update the Wiki page for me.

Thanks,
-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

I'm on a role here! but not a good role, I forgot he enclosure.

-Chris

Jason Wang wrote:

I also read the DoubleBufferedDrawing page, it is apparently a general
class which you can subclass and work with it, i didn't get it to
work.

Enclosed is a slightly cleaned-up version, that works just fine for me
on wxGTK 2.6.3. Please let me know if it doesn't work for you.

If it does, maybe you could update the Wiki page for me.

Thanks,
-Chris

DoubleBufferDemo.py (7.51 KB)

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

wow, that's fast response, anyways, i attached my original file, which
includes (i think) all of the stuff mentioned in the book, with minor
corrections, after carefully looking at my file, i realized that i
didn't attach EVT_IDLE to OnIdle(), i attached the file to make sure i
did everything correctly (it works :D)

Both of your versions work, i'll try to add the second to the wiki,
i've never worked with a wiki before..

Thank you so much for helping me :smiley:

test2.py (1.27 KB)

···

--
"It's a wonderful world hobbes ol' buddy, let's go explorin'!" -
Calvin in "Calvin and Hobbes"

Jason Wang wrote:

I realized that i
didn't attach EVT_IDLE to OnIdle(), i attached the file to make sure i
did everything correctly (it works :D)

you have to be very careful with Idle events: an idle event is called whenever the event queue becomes empty -- that can be very, very often. For example, as you move the mouse, there are mouse events, but if the system processes them fast enough (mine does) then the queue becomes empty with just about every event, before the next one fires.

Put a print statement in your Idle handler, and see what I mean.

With your code, get a kind of flashy mouse as it deal with all those Idle events.

Why not just init your buffer in OnSize? that's the only time you need to.

If you're trying to keep it from being re-inited a bunch of times as the Window is re-sizing, you need to (re) set a timer on each size event, and only re-init the buffer after the time fires, so it will only happen if there's a few milliseconds between events.

This is done in wx.lib.floatcanvas, and discussion and example code has been posted here a few times.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov