Possible creating "permanent" picture object?

Hi everybody,
I have a new, maybe silly simple question for you:
Is it possible to create a picture object which I can use in a timer event without creating it everytime new in the Timer event?

Until now i have only this code in the timer event:

Static y as integer
Var p As New Picture (300, 200, 32)

p.Graphics.DrawPicture(Mypicture, 0, 0, 100, 200, 0, y, 100, 200)
p.Graphics.DrawPicture(Mypicture, 100, 0, 100, 200, 0, y, 100, 200)
p.Graphics.DrawPicture(Mypicture, 200, 0, 100, 200, 0, y, 100, 200)

DesktopCanvas1.Backdrop = p

y = y + movepic

But I dont like to create a new picture object in every timer event.
Is it possible to create a “permanent” picture object out of timer code, which I can use in the timer everytime it fires?
Or maybe better sth. very different and better working? :slight_smile:
…and…after 3 years of Xojo..I still not have completely understood the way graphics work in Xojo, VB6 was easier somehow…

Thank you a lot, Rolf.

For what do you need the timer? Is this an animation? Then yes, you need a timer but you can make the picture a property of your window/container.

Insert a Module
Add a Property
OffscreenPict As Picture

New it in Window.Opening:
OffscreenPict = New Picture(300,200)

Draw what you want into OffscreenPict

And call it OffscreenPict wherever you need it.

Is this clear enought ?

Oh Emile,your solution works perfectly :slightly_smiling_face:
The only thing one day i could give you back for helping me is on of my simple games i try to make :upside_down_face:
Please have a good day,
yours, Rolf.

Hi Beatrix,
yes, it’s an animation…an image animation for a slotmachine wheel :slight_smile:
Containers, i didn’t use until now, i don’t know how to handle this.

But thank you a lot for helping me.
Yours, Rolf

And… your instructions are very descriptive and detailed compared to some others here.
Beginners like me love this :slight_smile:

You could use static instead:

Static p As New Picture (300, 200, 32)

There are some Pros & Cons of using Static vs. using a Module with a Public property. When using a Static, everything is more hidden and self-contained (e.g. you don’t need to have a separate initialize event). The fact that it’s hidden and self-contained can also be harder to debug…

1 Like

I often use static in timer events,
but here i think i tried once too without success

thank you Mike :slight_smile:

Anyone know whether there is any material difference between dragging (say) a socket or timer onto the layout, as opposed to creating one static in code and settings its properties?

If you are using a Static, you may want to use this format:

Static P as picture
if P = nil then
  P = new Picture(300,200)
  // do the drawing to picture
end if

This ensures that the picture is only created, AND only drawn to a single time.

If you do it this way:

Static P as picture = new Picture(300,200)  // this will happen exactly once
// do the drawing to picture : this will happen on every call

the Picture object will only be created once, but the drawing routine would happen every single call.

2 Likes

Events. With an instance on a window layout, it’s easy to have event handlers that are specific to the window.

You can accomplish this in code by using AddHandler() - but I find this way much harder to code, less easy to understand, and harder to debug. Also easier to make mistakes (memory leaks if you forget about RemoveHandler() etc.)

1 Like