Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Sun Jul 22, 2018 5:53 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: Delay on Canvas.Graphics.DrawPicture
PostPosted: Fri Dec 21, 2012 12:45 pm 
Offline

Joined: Fri Dec 21, 2007 9:50 am
Posts: 243
Hi All -

I have a function that generates a picture based on some data input and I pass that picture to a new window, which has a canvas object in it. The constructor for the window looks like this (the only argument is: passpic as picture; EDIT: thepic is a global variable in the new Window)
// Calling the overridden superclass constructor.
Super.Window


thepic=NewPicture(passpic.Width,passpic.height,passpic.depth)
thepic=passpic

Canvas1.Graphics.DrawPicture (thepic,0,0)


So the constructor should pass the picture to a global variable to the new window and load that picture into a canvas. However, my canvas is blank. So I considered a number of possible problems. After verifying almost everything else was working, I considered that the buffer WAS being passed to the new window but wasn't loading within the constructor. I added a pushbutton to the window with the following code
Canvas1.Graphics.DrawPicture thepic,0,0

which should essentially "reload" the picture into the canvas. When I push the pushbutton, the image loads!

My question is, why doesn't the constructor load the image? Also, the pushbutton clips off a small part of the picture. Is there a way to resize the picture to fit in the canvas?

Thanks,
Dan


Top
 Profile  
Reply with quote  
 Post subject: Re: Delay on Canvas.Graphics.DrawPicture
PostPosted: Fri Dec 21, 2012 12:54 pm 
Offline
Real Software Engineer

Joined: Sat Dec 24, 2005 8:18 pm
Posts: 7858
Location: Canada, Alberta, Near Red Deer
You draw the picture to the canvas graphics
I expect eventually the Canvas gets a paint event & wipes it out

Put your drawing code in the canvas' paint event and things should be good

_________________
Norman Palardy (Real Software)


Top
 Profile  
Reply with quote  
 Post subject: Re: Delay on Canvas.Graphics.DrawPicture
PostPosted: Fri Dec 21, 2012 12:56 pm 
Offline
User avatar

Joined: Mon Apr 02, 2007 2:08 am
Posts: 1225
Location: San Francisco, CA, USA
You should avoid drawing to the Canvas' graphics object from outside the Canvas' Paint event, one of the reasons being that anything you draw outside the paint even will disappear the next time the paint even fires.

Put your drawing code into the Paint event of the Canvas and replace it elsewhere with Canvas1.Refresh. This way, every time the Canvas must redraw itself, your code to draw the picture will run too:
// Calling the overridden superclass constructor.
Super.Window ' I'm not sure what this line does. Did you mean Super.Constructor?
thepic=NewPicture(passpic.Width,passpic.height,passpic.depth)
thepic=passpic

Canvas1.Refresh 'Triggers the Paint event once


And in the Canvas1.Paint event:
Sub Paint(g As Graphics)
g.DrawPicture (thepic,0,0)
End Sub

_________________
Boredom Software


Top
 Profile  
Reply with quote  
 Post subject: Re: Delay on Canvas.Graphics.DrawPicture
PostPosted: Fri Dec 21, 2012 12:59 pm 
Offline
Real Software Engineer

Joined: Fri Jan 28, 2011 5:24 pm
Posts: 145
charonn0 wrote:
You should avoid drawing to the Canvas' graphics object from outside the Canvas' Paint event, one of the reasons being that anything you draw outside the paint even will disappear the next time the paint even fires.

Put your drawing code into the Paint event of the Canvas and replace it elsewhere with Canvas1.Refresh. This way, every time the Canvas must redraw itself, your code to draw the picture will run too:


Invalidate is better than Refresh, unless you absolutely need it to update immediately and not the next iteration of the event loop.

_________________
Joe Ranieri
Mac Frameworks & Compiler
REAL Software, Inc.


Top
 Profile  
Reply with quote  
 Post subject: Re: Delay on Canvas.Graphics.DrawPicture
PostPosted: Fri Dec 21, 2012 2:23 pm 
Offline

Joined: Fri Dec 21, 2007 9:50 am
Posts: 243
Thank you everyone. That clears things up alot.


Top
 Profile  
Reply with quote  
 Post subject: Re: Delay on Canvas.Graphics.DrawPicture
PostPosted: Fri Dec 21, 2012 3:57 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
You should also think about making a copy of the image (you almost have it, but you're throwing away the copy).

thepic=NewPicture(passpic.Width,passpic.height,passpic.depth) // makes a new image the same size
thepic=passpic // throws the new image away and just points to the original

If you change the original, you may not want the copy of it to change as well. If you set thepic=passpic, then it will change.

Try,

thepic=NewPicture(passpic.Width,passpic.height,passpic.depth)
thepic.Graphics.DrawPicture(0,0,passpic)

Now you have a completely independent copy of the original. You can recreate/redraw the original without affecting your copy.


Top
 Profile  
Reply with quote  
 Post subject: Re: Delay on Canvas.Graphics.DrawPicture
PostPosted: Thu Dec 27, 2012 8:37 am 
Offline

Joined: Fri Dec 21, 2007 9:50 am
Posts: 243
timhare wrote:
You should also think about making a copy of the image (you almost have it, but you're throwing away the copy).

thepic=NewPicture(passpic.Width,passpic.height,passpic.depth) // makes a new image the same size
thepic=passpic // throws the new image away and just points to the original

If you change the original, you may not want the copy of it to change as well. If you set thepic=passpic, then it will change.

Try,

thepic=NewPicture(passpic.Width,passpic.height,passpic.depth)
thepic.Graphics.DrawPicture(0,0,passpic)

Now you have a completely independent copy of the original. You can recreate/redraw the original without affecting your copy.

Thanks for the tip Tim. Just a quick question, because my code is in the constructor of the window, doesn't thepic set to passpic only once (when the constructor window is instantiated)? I have one window which generates passpic and each time passpic is generated it is passed to a new instance of the window. Each time a window opens with a new picture and the other windows remain unchanged. So, I think I did make a copy of the picture but I could be mistaken.


Top
 Profile  
Reply with quote  
 Post subject: Re: Delay on Canvas.Graphics.DrawPicture
PostPosted: Thu Dec 27, 2012 4:53 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
It doesn't make a copy. It just points to the picture object stored elsewhere. Now, if you always create a new picture when you create a new window, then you won't have a problem. But if you just reuse that picture, the change will be reflected in all the windows, because they all point to that picture object.


Top
 Profile  
Reply with quote  
 Post subject: Re: Delay on Canvas.Graphics.DrawPicture
PostPosted: Thu Dec 27, 2012 5:21 pm 
Offline

Joined: Fri Dec 21, 2007 9:50 am
Posts: 243
timhare wrote:
It doesn't make a copy. It just points to the picture object stored elsewhere. Now, if you always create a new picture when you create a new window, then you won't have a problem. But if you just reuse that picture, the change will be reflected in all the windows, because they all point to that picture object.


Let's say I create a picture, which in turn creates a new window to display it. I repeat the process with a different picture. So I have two windows, each displaying a different picture. I then resize both the windows, firing the paint event of the canvas. If I was pointing to one picture, both windows' canvas objects should display the same picture, right? That isn't what I'm seeing though. I'm not trying to argue by any means, just not sure what I am missing. I definitely appreciate your expert advice tim.


Top
 Profile  
Reply with quote  
 Post subject: Re: Delay on Canvas.Graphics.DrawPicture
PostPosted: Thu Dec 27, 2012 5:45 pm 
Offline
User avatar

Joined: Mon Apr 02, 2007 2:08 am
Posts: 1225
Location: San Francisco, CA, USA
Using the assignment operator ("=") merely creates a new reference to the object on the right side of the operator; both objects now refer to the same in-memory picture, they're just different "names" for the same thing:

Dim pic1 As New Picture(10, 10, 32) 'Declare and create a picture object named pic1
Dim pic2 As Picture 'Declare a picture object named pic2 but don't create it

pic2 = pic1 ' pic2 now points to pic1; any changes to pic1 are immediately reflected in pic2 and vice versa


To copy a picture into a new object independent of the first object, you must draw the original onto the duplicate:

Dim pic1 As New Picture(10, 10, 32) 'Declare and create a picture object named pic1
Dim pic2 As Picture 'Declare a picture object named pic2 but don't create it

pic2 = New Picture(pic1.Height, pic1.Width, pic1.Depth) 'Create the pic2 object with pic1's dimensions
pic2.Graphics.DrawPicture(pic1, 0, 0) 'Draw pic1 into pic2

_________________
Boredom Software


Top
 Profile  
Reply with quote  
 Post subject: Re: Delay on Canvas.Graphics.DrawPicture
PostPosted: Fri Dec 28, 2012 12:07 am 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
DanSandbergUCONN wrote:
Let's say I create a picture, which in turn creates a new window to display it. I repeat the process with a different picture. So I have two windows, each displaying a different picture. I then resize both the windows, firing the paint event of the canvas. If I was pointing to one picture, both windows' canvas objects should display the same picture, right? That isn't what I'm seeing though. I'm not trying to argue by any means, just not sure what I am missing. I definitely appreciate your expert advice tim.

Then you are creating a new picture object for each window. That will work fine. What won't work is something along the lines of
dim pic as New Picture(100,100,32)

// create first window
pic.graphics.forecolor = &cFF0000
pic.graphics.fillrect(0,0,100,100)
w1 = new myWindow(pic)

// create second window
pic.graphics.forecolor = &c00FF00
pic.graphics.fillrect(0,0,100,100)
w2 = new myWindow(pic)

What we have here is 2 windows pointing to the same picture.


Top
 Profile  
Reply with quote  
 Post subject: Re: Delay on Canvas.Graphics.DrawPicture
PostPosted: Fri Dec 28, 2012 10:14 am 
Offline

Joined: Fri Dec 21, 2007 9:50 am
Posts: 243
timhare wrote:
DanSandbergUCONN wrote:
Let's say I create a picture, which in turn creates a new window to display it. I repeat the process with a different picture. So I have two windows, each displaying a different picture. I then resize both the windows, firing the paint event of the canvas. If I was pointing to one picture, both windows' canvas objects should display the same picture, right? That isn't what I'm seeing though. I'm not trying to argue by any means, just not sure what I am missing. I definitely appreciate your expert advice tim.

Then you are creating a new picture object for each window. That will work fine. What won't work is something along the lines of
dim pic as New Picture(100,100,32)

// create first window
pic.graphics.forecolor = &cFF0000
pic.graphics.fillrect(0,0,100,100)
w1 = new myWindow(pic)

// create second window
pic.graphics.forecolor = &c00FF00
pic.graphics.fillrect(0,0,100,100)
w2 = new myWindow(pic)

What we have here is 2 windows pointing to the same picture.



Gotcha! Thanks Tim, that makes total sense now.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group