You Are Here:

Community: Developer Discussion Boards

Reply « Previous Thread | Next Thread »

#1 Old Having trouble with the Canvas - 2008-08-21, 17:24

Join Date: Jul 2008
Posts: 33
PyNewbie08
Offline
Registered User
Hi,

I am attempting to update the canvas with text and I have a separate class that handles the updates to the text while the main application drives other images that are being added to the canvas. Therefore, I have been passing the Canvas object to the classes that handle modifying the images.

For some reason, this does not work and I believe my issue has to do with understanding how callbacks work. Here's my code below:

Code:
class DrawText(CurrentCanvas):
	def __init__(self,CurrentCanvas):
		self.text=0
		self.fillarray={}
		self.resboxoffset=2        
		self.canvas = CurrentCanvas(redraw_callback = self.redraw)
		self.draw=Draw(self.canvas)
		appuifw.app.body=self.canvas
		self.redraw()
		
	def redraw(self):
		self.draw_text()
    	
	def draw_text(self):
		self.clear_text()
		newtext=u"%dlb"%self.text
		self.draw.text((35,105),newtext,(0,192,0),"title")
		
	def clear_text(self):
  		#Rectangle
		self.canvas.rectangle((30,60,90,110), fill=0xFFFFFF) 
		

class DrawImages:
	def drawBaseBackground(self,canvas, image):
		canvas.blit(image, target = (90, 75), scale = 0)
	def drawActionIcon(self,canvas, icon):
		canvas.blit(icon, target = (25, 135), scale = 0)
	def drawAdvantageIcon(self,canvas, icons):
		canvas.blit(icons, target = (60, 205), scale = 0)


canvas=appuifw.Canvas()
appuifw.app.body=canvas

myImages = DrawImages()
e32.ao_sleep(1)
myImages.drawBaseBackground(canvas, base_background)
myImages.drawActionIcon(canvas, icon)
myImages.drawAdvantageIcon(canvas, icons )

carbon = DrawCarbon(canvas)
This results in the error = global name Draw is not defined

However, if I use another canvas in the DrawText class (with no interaction with the other images), the text is written correctly.

Can someone tell me why this may be occurring? Thanks.
Reply With Quote

#2 Old Re: Having trouble with the Canvas - 2008-08-22, 00:21

Join Date: Jul 2007
Posts: 159
Location: Eindhoven, the Netherlands
hurenkam
Offline
Regular Contributor
Quote:
Originally Posted by PyNewbie08 View Post
Code:
class DrawText(CurrentCanvas):
	def __init__(self,CurrentCanvas):
		self.text=0
		self.fillarray={}
		self.resboxoffset=2        
		self.canvas = CurrentCanvas(redraw_callback = self.redraw)
		self.draw=Draw(self.canvas)
At this point it seems like you want to instantiate a 'Draw' class, which
is not defined in your code.

Quote:
Originally Posted by PyNewbie08 View Post
This results in the error = global name Draw is not defined
Which is why you get this error.
Reply With Quote

#3 Old Re: Having trouble with the Canvas - 2008-09-08, 03:27

Join Date: Jul 2008
Posts: 33
PyNewbie08
Offline
Registered User
I was not able to get this to work correctly. Essentially, what I would like to do is write text over an existing image. Are there any examples of this?

Code:
import appuifw
import messaging
import e32
import TopWindow
sample_picture=graphics.Image.open(u"e:\\Images\\sample.png")
canvas.blit(sample_picture, target = (0, 0), scale = 1)
# HOW DO I WRITE TEXT ON TOP OF THE IMAGE?
I tried using the DrawScore class below but this did not work. Is there a simpler way to do this?
Reply With Quote

#4 Old Re: Having trouble with the Canvas - 2008-09-08, 03:46

Join Date: Jul 2008
Posts: 103
ck.umraliya's Avatar
ck.umraliya
Offline
Forum Nokia Champion
hi PyNewbie08,
I think the error you are getting is due to the undefined function which you are using in the constructor. So once you define it there should be no error.
Code:
def __init__(self,CurrentCanvas):
		self.text=0
		self.fillarray={}
		self.resboxoffset=2        
		self.canvas = CurrentCanvas(redraw_callback = self.redraw)
		self.draw=Draw(self.canvas)##error line
		appuifw.app.body=self.canvas
		self.redraw()
Reply With Quote

#5 Old Re: Having trouble with the Canvas - 2008-09-10, 19:37

Join Date: Jul 2008
Posts: 33
PyNewbie08
Offline
Registered User
Ahhh, I see.

Is there a reason why the images on the canvas disappear after a few minutes?
Reply With Quote

#6 Old Re: Having trouble with the Canvas - 2008-09-10, 20:09

Join Date: Feb 2008
Posts: 2,542
Location: Bhavnagar, Gujarat, India
Send a message via Yahoo to gaba88 Send a message via Skype™ to gaba88
gaba88's Avatar
gaba88
Offline
Forum Nokia Champion
Quote:
Originally Posted by PyNewbie08 View Post
Ahhh, I see.

Is there a reason why the images on the canvas disappear after a few minutes?
hi PyNewbie
does the image automatically disappears aor it disappears on some click event i mean when you press anything.

Hope you got my question

Enjoy Pythoning
Gaba88


Gargi Das- http://gargidas.blogsot.com

Forum Nokia Python Wiki


Learn Python at http://mobapps.org/PyS60
Reply With Quote

#7 Old Re: Having trouble with the Canvas - 2008-09-12, 13:39

Join Date: Jul 2008
Posts: 103
ck.umraliya's Avatar
ck.umraliya
Offline
Forum Nokia Champion
hi PyNewbie08
images on the canvas will dissappear as soon as some click event occurs such as, preseing the menu key and canceling that.
To keep the image fixed on the canvas and overcome the problem explained above you need to use image handle_redraw() method.
Reply With Quote

#8 Old Re: Having trouble with the Canvas - 2008-09-12, 17:30

Join Date: Jul 2008
Posts: 33
PyNewbie08
Offline
Registered User
I haven't yet mastered how the canvas works. If any of you have suggestions as to where I can go to get a quick overview, that would be great. I've been working with the example below to prevent the images from disappearing after a few minutes. I get an invalid syntax error on appuifw.app.body=canvas but I believe the error is caused by the handle_redraw code. Could someone explain the problem here and possibly explain how the redraw is supposed to work? Thanks.

Code:
import appuifw
import e32
import TopWindow, graphics

from topwindow import *
from appuifw import *
from graphics import *

g_orange = (255,69,0)

canvas=appuifw.Canvas(event_callback=handle_event, redraw_callback=handle_redraw)
appuifw.app.body=canvas
canvas.rectangle([(0,75), (43,220)], g_orange, width=2)  # selected
draw=Draw(canvas)
scoretext=u"Score"
draw.text((0,35),scoretext,g_orange,"title")
canvas.rectangle([(5,95),(100,90)], fill=(g_orange), width=2) 
scoretext=u"0"
draw.text((5,60),scoretext,g_orange,"title")
app_lock = e32.Ao_lock()
app_lock.wait()
Reply With Quote

#9 Old Re: Having trouble with the Canvas - 2008-09-12, 17:52

Join Date: Oct 2007
Posts: 2,841
Location: Deva, Romania
bogdan.galiceanu's Avatar
bogdan.galiceanu
Offline
Forum Nokia Champion
handle_redraw and handle_event are callback functions that you have to define yourself. Here's a working example:
Code:
import appuifw
import e32
import TopWindow, graphics

g_orange = (255,69,0)

img=graphics.Image.new((240,320))

img.rectangle([(0,75), (43,220)], g_orange, width=2)  # selected

scoretext=u"Score"

img.text((0,35),scoretext,g_orange,"title")

img.rectangle([(5,95),(100,90)], fill=(g_orange), width=2)

scoretext=u"0"
img.text((5,60),scoretext,g_orange,"title")

def handle_redraw(rect):
	canvas.blit(img)
canvas=appuifw.Canvas(event_callback=None, redraw_callback=handle_redraw)
appuifw.app.body=canvas

app_lock = e32.Ao_lock()
app_lock.wait()
Last edited by bogdan.galiceanu : 2008-09-12 at 18:02.
Reply With Quote

#10 Old Arrow Re: Having trouble with the Canvas - 2008-09-12, 18:07

Join Date: Jul 2008
Posts: 103
ck.umraliya's Avatar
ck.umraliya
Offline
Forum Nokia Champion
hi PyNewbie08
To solve the problem related with canvasing you need to understand the concept of image double buffering.
I am soon going to write an article on WiKi over it.
Reply With Quote

#11 Old Re: Having trouble with the Canvas - 2008-09-12, 18:11

Join Date: Jul 2008
Posts: 103
ck.umraliya's Avatar
ck.umraliya
Offline
Forum Nokia Champion
hi i think bogdan has shown the exact way.
Just to add it i that the concept of first drawing objects on image and then blinking it over the canvas is reffered to Double Buffering Of image-canvas.
Reply With Quote

#12 Old Re: Having trouble with the Canvas - 2008-09-12, 18:14

Join Date: Oct 2007
Posts: 2,841
Location: Deva, Romania
bogdan.galiceanu's Avatar
bogdan.galiceanu
Offline
Forum Nokia Champion
Chintan is right. It is a sort of double buffering. Basically handle_redraw places an image on the canvas. handle_event is a callback for detecting keypresses.
Reply With Quote

#13 Old Re: Having trouble with the Canvas - 2008-09-12, 18:16

Join Date: Jul 2008
Posts: 103
ck.umraliya's Avatar
ck.umraliya
Offline
Forum Nokia Champion
hi i think bogdan has shown the exact way.
Just to add it i that the concept of first drawing objects on image and then blinking it over the canvas is reffered to Double Buffering Of image-canvas.
Reply With Quote

#14 Old Re: Having trouble with the Canvas - 2008-09-12, 18:34

Join Date: Jul 2008
Posts: 33
PyNewbie08
Offline
Registered User
Thanks Bogdan and CK!

Bogdan, this works for the simple rectangle and text image but when I try this on my full prototype, it doesn't work. The only difference between the full prototype and this is that I'm using:

Code:
canvas.blit(sample_image, target = (0, 0), scale = 1)
right after the line: appuifw.app.body=canvas
Code:
import appuifw
import e32
import TopWindow, graphics

from topwindow import *
from appuifw import *
from graphics import *

g_orange = (255,69,0)

canvas=appuifw.Canvas(event_callback=handle_event, redraw_callback=handle_redraw)
appuifw.app.body=canvas
canvas.rectangle([(0,75), (43,220)], g_orange, width=2)  # selected
draw=Draw(canvas)
scoretext=u"Score"
draw.text((0,35),scoretext,g_orange,"title")
canvas.rectangle([(5,95),(100,90)], fill=(g_orange), width=2) 
scoretext=u"0"
draw.text((5,60),scoretext,g_orange,"title")
app_lock = e32.Ao_lock()
app_lock.wait()
Reply With Quote

#15 Old Re: Having trouble with the Canvas - 2008-09-12, 18:45

Join Date: Oct 2007
Posts: 2,841
Location: Deva, Romania
bogdan.galiceanu's Avatar
bogdan.galiceanu
Offline
Forum Nokia Champion
Can you post the code for the Draw() class? Or, if it's possible, for the full prototype. It would be a lot easier to figure out what is wrong.
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump
Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a partly transparent Canvas to lay on top of another Canvas? mkleijer Mobile Java Media (Graphics & Sounds) 4 2007-05-22 08:00
Order of called methods (Constructor,paint(), run()) when using canvas and runnable? edmund66 Mobile Java Media (Graphics & Sounds) 3 2004-09-22 10:40
Major problem with delegation to Canvas bartekn Mobile Java Media (Graphics & Sounds) 8 2003-10-08 18:46
7650 and 3650 Canvas and FullCanvas memory marcilgen Mobile Java General 5 2003-07-08 13:24
Larger canvas? bjajo780 Mobile Java General 1 2002-06-18 15:17

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia