<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: canvas code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 08:13:41 GMT</pubDate>
    <description>DZone Snippets: canvas code</description>
    <item>
      <title>PyS60 - Gallery Thumbnail Ver. 2</title>
      <link>http://snippets.dzone.com/posts/show/1683</link>
      <description>// Versione riveduta e corretta di Gallery Thumbnail postata da korakot ^_^&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import appuifw&lt;br /&gt;import e32&lt;br /&gt;import graphics&lt;br /&gt;import key_codes&lt;br /&gt;import os&lt;br /&gt;&lt;br /&gt;class FlickrS60Error(Exception): pass&lt;br /&gt;&lt;br /&gt;class FlickrS60Thumb:&lt;br /&gt;&lt;br /&gt;	def __init__(self, path):&lt;br /&gt;&lt;br /&gt;		self.path = unicode(path)&lt;br /&gt;		self.listFile = []&lt;br /&gt;		self.ldivx = 0&lt;br /&gt;		self.ldivy = 0&lt;br /&gt;		self.lock = e32.Ao_lock()&lt;br /&gt;		self.canvas = None&lt;br /&gt;		self.img = graphics.Image.new((176, 144))&lt;br /&gt;		self.img_tmp = graphics.Image.new((42, 36))&lt;br /&gt;		self.x, self.y = 0, 0&lt;br /&gt;		self.p = 0&lt;br /&gt;		self.pages = 0&lt;br /&gt;		self.currpages = 0&lt;br /&gt;		self.mpath = 0&lt;br /&gt;&lt;br /&gt;	def OnRun(self):&lt;br /&gt;&lt;br /&gt;		appuifw.app.exit_key_handler = self.lock.signal&lt;br /&gt;	&lt;br /&gt;		self._createList()&lt;br /&gt;		&lt;br /&gt;		self.canvas = appuifw.Canvas(redraw_callback=self.OnUpdate)&lt;br /&gt;		appuifw.app.body = self.canvas&lt;br /&gt;&lt;br /&gt;		self.canvas.bind(key_codes.EKeyRightArrow, lambda: self.move(1, 0))&lt;br /&gt;		self.canvas.bind(key_codes.EKeyLeftArrow, lambda: self.move(-1, 0))&lt;br /&gt;		self.canvas.bind(key_codes.EKeyUpArrow, lambda: self.move(0, -1))&lt;br /&gt;		self.canvas.bind(key_codes.EKeyDownArrow, lambda: self.move(0, 1))&lt;br /&gt;		self.canvas.bind(key_codes.EKeySelect, self.IMG)&lt;br /&gt;&lt;br /&gt;		self._drawIMG()&lt;br /&gt;		&lt;br /&gt;		self.lock.wait()&lt;br /&gt;&lt;br /&gt;	def OnUpdate(self, rect):&lt;br /&gt;&lt;br /&gt;		self.canvas.blit(self.img)&lt;br /&gt;		self.canvas.rectangle([(self.p+(42*self.x), 36*self.y), (self.p+(42*self.x)+42, (36*self.y)+36)], width=2, outline=0x123456)&lt;br /&gt;&lt;br /&gt;	def move(self, x, y):&lt;br /&gt;&lt;br /&gt;		self.x = (self.x+x)%4&lt;br /&gt;		self.y = (self.y+y)&lt;br /&gt;&lt;br /&gt;		if x == 1: self.p = (self.p+2)%8&lt;br /&gt;		if x == -1: self.p = (self.p-2)%8&lt;br /&gt;&lt;br /&gt;		if self.y == 4:&lt;br /&gt;			if self.currpages &lt; self.pages-1:&lt;br /&gt;				self.y = 0&lt;br /&gt;				self.currpages += 1&lt;br /&gt;				self._drawIMG(start=self.currpages)&lt;br /&gt;			else:&lt;br /&gt;				self.y = 3&lt;br /&gt;			&lt;br /&gt;		if self.y == -1:&lt;br /&gt;			if self.currpages &gt; 0:&lt;br /&gt;				self.y = 3&lt;br /&gt;				self.currpages -= 1&lt;br /&gt;				self._drawIMG(start=self.currpages)&lt;br /&gt;			else:&lt;br /&gt;				self.y = 0&lt;br /&gt;	&lt;br /&gt;		self.mpath = (4*self.y + self.x)+(16*self.currpages)&lt;br /&gt;		&lt;br /&gt;		self.OnUpdate(None)&lt;br /&gt;	&lt;br /&gt;	def IMG(self):&lt;br /&gt;&lt;br /&gt;		try:&lt;br /&gt;			m = self.listFile[self.mpath].replace('_PalbTN\\', '')&lt;br /&gt;			appuifw.Content_handler().open(m)&lt;br /&gt;		except:&lt;br /&gt;			pass&lt;br /&gt;		&lt;br /&gt;	def _drawIMG(self, start=0):&lt;br /&gt;&lt;br /&gt;		self.img.clear(0xffffff)&lt;br /&gt;		z = 0&lt;br /&gt;		&lt;br /&gt;		for id in range(start*16, (start+1)*16):&lt;br /&gt;			j, i = divmod(id-(start*16), 4)&lt;br /&gt;			try:&lt;br /&gt;				self.img_tmp.load(self.listFile[id])&lt;br /&gt;				self.img.blit(self.img_tmp, target=(z+(42*i)+(z+1), 36*j))&lt;br /&gt;				z = (z+1)%4&lt;br /&gt;			except:&lt;br /&gt;				break&lt;br /&gt;&lt;br /&gt;		self.OnUpdate(None)&lt;br /&gt;&lt;br /&gt;	def _createList(self):&lt;br /&gt;&lt;br /&gt;		try:&lt;br /&gt;			for id in os.listdir(self.path):&lt;br /&gt;				self.listFile.append(self.path + id)&lt;br /&gt;&lt;br /&gt;			self.ldivx, self.ldivy = divmod(len(self.listFile), 16)&lt;br /&gt;&lt;br /&gt;			self.pages = self.ldivx&lt;br /&gt;			if self.ldivy &lt;&gt; 0: self.pages += 1&lt;br /&gt;		except:&lt;br /&gt;			raise FlickrS60Error('Errore nella creazione della lista.')&lt;br /&gt;&lt;br /&gt;if __name__ == '__main__':&lt;br /&gt;&lt;br /&gt;	FlickrS60Thumb('E:\\Images\\_PalbTN\\').OnRun()&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 11 Mar 2006 05:36:17 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1683</guid>
      <author>whitetiger ()</author>
    </item>
    <item>
      <title>Gallery thumbnails</title>
      <link>http://snippets.dzone.com/posts/show/1677</link>
      <description>&lt;code&gt;&lt;br /&gt;from appuifw import *&lt;br /&gt;from graphics import Image&lt;br /&gt;from key_codes import *&lt;br /&gt;#from status import *&lt;br /&gt;#from e32db import format_time&lt;br /&gt;import os, e32&lt;br /&gt;&lt;br /&gt;dir = u'C:\\Nokia\\Images\\_PAlbTN\\'&lt;br /&gt;os.chdir(dir)&lt;br /&gt;fs = os.listdir('')&lt;br /&gt;mtime = os.path.getmtime&lt;br /&gt;# newest first&lt;br /&gt;fs.sort(lambda a,b: cmp(mtime(b), mtime(a)))&lt;br /&gt;&lt;br /&gt;app.body = canvas = Canvas()&lt;br /&gt;# show just 16 images&lt;br /&gt;for k in range(min(16, len(fs))):&lt;br /&gt;    j, i = divmod(k, 4)&lt;br /&gt;    im = Image.open(dir + fs[k])&lt;br /&gt;    canvas.blit(im, target=(2+44*i, 2+34*j))&lt;br /&gt;canvas.rectangle([(0,0), (43,33)], 0xff, width=2)  # selected&lt;br /&gt;&lt;br /&gt;x, y, k = 0, 0, 0&lt;br /&gt;def move(dx, dy):&lt;br /&gt;    global x, y, k&lt;br /&gt;    canvas.rectangle([(44*x,34*y), (44*x+43,34*y+33)], 0xffffff, width=2)  &lt;br /&gt;    k = 4*y + x + 4*dy + dx&lt;br /&gt;    y, x = divmod(k, 4)&lt;br /&gt;    canvas.rectangle([(44*x,34*y), (44*x+43,34*y+33)], 0xff, width=2)&lt;br /&gt;    if 0 &lt;= k &lt; len(fs):&lt;br /&gt;        app.title = u''+fs[k]&lt;br /&gt;        #status_on(format_time(mtime(fs[k])))&lt;br /&gt;&lt;br /&gt;# move cursor and open image&lt;br /&gt;canvas.bind(EKeyUpArrow,   lambda: move(0,-1))&lt;br /&gt;canvas.bind(EKeyDownArrow, lambda: move(0,1))&lt;br /&gt;canvas.bind(EKeyLeftArrow, lambda: move(-1,0))&lt;br /&gt;canvas.bind(EKeyRightArrow,lambda: move(1,0))&lt;br /&gt;canvas.bind(EKeySelect,    lambda: Content_handler().open(dir[:-8]+fs[k]))&lt;br /&gt;&lt;br /&gt;# standard code for non-loop app&lt;br /&gt;lock = e32.Ao_lock()&lt;br /&gt;app.exit_key_handler = lock.signal&lt;br /&gt;lock.wait()&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Fri, 10 Mar 2006 08:03:21 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1677</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>Wrap text to fit canvas screen</title>
      <link>http://snippets.dzone.com/posts/show/833</link>
      <description>Pys60 provide you with 3 types for app.body&lt;br /&gt;- Text&lt;br /&gt;- ListBox&lt;br /&gt;- Canvas&lt;br /&gt;&lt;br /&gt;Sometimes you want some text and image together, you need to&lt;br /&gt;use canvas. However, there's no way to calculate the length&lt;br /&gt;of text and wrap them properly. Simo's dashboard provide a&lt;br /&gt;solution to this using his akntextutils C++ extension.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;from akntextutils import wrap_text_to_array&lt;br /&gt;&lt;br /&gt;# long_str is a long string to be wrapped&lt;br /&gt;lines = wrap_text_to_array(long_str, 'dense', 176)&lt;br /&gt;&lt;br /&gt;x, y = 2, 0&lt;br /&gt;for line in lines:&lt;br /&gt;   y += 14&lt;br /&gt;   canvas.text((x, y), line, font='dense')&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 26 Oct 2005 17:14:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/833</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>Canvas and its callbacks in OO code</title>
      <link>http://snippets.dzone.com/posts/show/731</link>
      <description>I learn to use 2 different types of Canvas callbacks&lt;br /&gt;in the &lt;a href=http://www.bigbold.com/snippets/posts/show/730&gt;last snippet&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Typically, when I wrote a non-OO code, I will use&lt;br /&gt;&lt;code&gt;app.body = c = Canvas()&lt;/code&gt; where I already had &lt;code&gt;from appuifw import *&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The shortcoming is that I need to define callbacks first,&lt;br /&gt;then pass it to the constructor&lt;br /&gt;c = Canvas(redraw_callback, event_callback)&lt;br /&gt;By using OO, the canvas is created in __init__() and it&lt;br /&gt;can access other methods that come later in the code.&lt;br /&gt;In this case, I use Canvas(self.update) which means that&lt;br /&gt;the self.update will be used to redraw screen.&lt;br /&gt;&lt;br /&gt;The secode way to use callback is Canvas.bind() method.&lt;br /&gt;I have always been using this approach to binding any event&lt;br /&gt;callback to a canvas. In some case, the event_callback in&lt;br /&gt;the constructor maybe more elegant, though.&lt;br /&gt;&lt;br /&gt;Notice my use of &lt;code&gt;self.canvas.bind(EKeySelect, self.toggle)&lt;/code&gt;&lt;br /&gt;Here I can bind the select key to self.toggle whose definition&lt;br /&gt;will follow. This is more convenient than having to define&lt;br /&gt;it first.  So, I think OO code is easier to write in this way.&lt;br /&gt;&lt;br /&gt;I also use class variables instead of instance variables.&lt;br /&gt;I found declaring it outside __init__() is more natural&lt;br /&gt;and similar to my previous non-OO approach. &lt;br /&gt;(still easy to read, with variable &amp; def declarations)&lt;br /&gt;When I write self.myvar inside __init__(), I feel the code&lt;br /&gt;is somewhat bloated. The class will have only 1 instance&lt;br /&gt;anyway.</description>
      <pubDate>Sat, 17 Sep 2005 05:19:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/731</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>Graphical Plotter //Javascript Class</title>
      <link>http://snippets.dzone.com/posts/show/682</link>
      <description>&lt;a href="http://www.jsfromhell.com/dhtml/graphical-plotter"&gt;&lt;br /&gt;&lt;br /&gt;[UPDATED CODE AND HELP CAN BE FOUND HERE]&lt;br /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;An unuseful thing to draw using javascript, it's slow as hell :)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;//+ Jonas Raoni Soares Silva&lt;br /&gt;//@ http://jsfromhell.com/dhtml/graphical-plotter [v1.0]&lt;br /&gt;&lt;br /&gt;Canvas = function(){&lt;br /&gt;	var o = this;&lt;br /&gt;	( o.penPos = { x: 0, y: 0 }, o.pixelSize = 10, o.pen = { style: "solid", size: 1, color: "#000" }, o.brush = { style: "solid", color: "#000" } );&lt;br /&gt;};&lt;br /&gt;with( { p: Canvas.prototype } ){&lt;br /&gt;	p.pixel = function( x, y, color ) {&lt;br /&gt;		var o = this, s = document.body.appendChild( document.createElement( "div" ) ).style;&lt;br /&gt;		return ( s.position = "absolute", s.width = ( o.pen.size * o.pixelSize ) + "px", s.height = ( o.pen.size * o.pixelSize ) + "px", s.fontSize = "1px", s.left = ( x * o.pixelSize ) + "px", s.top = ( y * o.pixelSize ) + "px", s.backgroundColor = color || o.pen.color, o );&lt;br /&gt;	};&lt;br /&gt;	p.line = function( x1, y1, x2, y2 ){&lt;br /&gt;		if( Math.abs( x1 - x2 ) &lt; Math.abs( y1 - y2 ) )&lt;br /&gt;			for( y = Math.min( y1, y2 ) - 1, x = Math.max( y1, y2 ); ++y &lt;= x; canvas.pixel( ( y * ( x1 - x2 ) - x1 * y2 + y1 * x2 ) / ( y1 - y2 ), y ) );&lt;br /&gt;		else&lt;br /&gt;			for( x = Math.min( x1, x2 ) - 1, y = Math.max( x1, x2 ); ++x &lt;= y; canvas.pixel( x, ( x * ( y1 - y2 ) - y1 * x2 + x1 * y2 ) / ( x1 - x2 ) ) );&lt;br /&gt;		return this;&lt;br /&gt;	};&lt;br /&gt;	p.arc = function( x, y, raio, startAngle, degrees ) {&lt;br /&gt;		for( degrees += startAngle; degrees --&gt; startAngle; this.pixel( Math.cos( degrees * Math.PI / 180 ) * raio + x, Math.sin( degrees * Math.PI / 180 ) * raio + y ) ); return this;&lt;br /&gt;	};&lt;br /&gt;	p.rectangle = function( x, y, width, height, rotation ){&lt;br /&gt;		return this.moveTo( x, y ).lineBy( 0, height ).lineBy( width, 0 ).lineBy( 0, -height ).lineBy( -width, 0 );&lt;br /&gt;	};&lt;br /&gt;	p.moveTo = function( x, y ){ var o = this; return ( o.penPos.x = x, o.penPos.y = y, o ); };&lt;br /&gt;	p.moveBy = function( x, y ){ var o = this; return o.moveTo( o.penPos.x + x, o.penPos.y + y ); };&lt;br /&gt;	p.lineTo = function( x, y ){ var o = this; return o.line( o.penPos.x, o.penPos.y, x, y ).moveTo( x, y ); };&lt;br /&gt;	p.lineBy = function( x, y ){ var o = this; return o.lineTo( o.penPos.x + x, o.penPos.y + y ); };&lt;br /&gt;	p.curveTo = function( cx, cy, x, y ){};&lt;br /&gt;	p.polyBezier = function( points ){};&lt;br /&gt;	p.path = function( points ){};&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;canvas = new Canvas;&lt;br /&gt;&lt;br /&gt;canvas.pen.color = "#f00";&lt;br /&gt;canvas.rectangle( 30, 20, 20, 20 );&lt;br /&gt;canvas.pen.color = "#080";&lt;br /&gt;canvas.rectangle( 35, 25, 10, 10 );&lt;br /&gt;canvas.pen.color = "#008";&lt;br /&gt;canvas.arc( 50, 30, 10, 180, 270 );&lt;br /&gt;canvas.arc( 30, 30, 10, 0, 270 );&lt;br /&gt;canvas.pen.color = "#ff0";&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 09 Sep 2005 07:25:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/682</guid>
      <author>jonasraoni (Jonas Raoni Soares Silva)</author>
    </item>
    <item>
      <title>Working with both Text and Canvas</title>
      <link>http://snippets.dzone.com/posts/show/577</link>
      <description>In some applications, you need to write some code&lt;br /&gt;in the input text, while the result is display on&lt;br /&gt;a canvas display. For PC apps, you can just make&lt;br /&gt;a two-pane display.&lt;br /&gt;&lt;br /&gt;On pys60, however, the phone can display only&lt;br /&gt;either a Canvas, a Text or a Listbox at the same time.&lt;br /&gt;You cannot use two-pane interface.&lt;br /&gt;&lt;br /&gt;The following code works around this by switching&lt;br /&gt;between two displays.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;from appuifw import *&lt;br /&gt;import e32&lt;br /&gt;sleep = e32.ao_sleep&lt;br /&gt;&lt;br /&gt;t = Text()&lt;br /&gt;c = Canvas()&lt;br /&gt;&lt;br /&gt;running = 1&lt;br /&gt;def quit():&lt;br /&gt;    global running&lt;br /&gt;    running = 0&lt;br /&gt;app.exit_key_handler = quit&lt;br /&gt;&lt;br /&gt;while running:&lt;br /&gt;    app.body = t&lt;br /&gt;    sleep(5)&lt;br /&gt;    # show result&lt;br /&gt;    app.body = c&lt;br /&gt;    c.clear()&lt;br /&gt;    c.text((10, 50), t.get())&lt;br /&gt;    sleep(0.5)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 11 Aug 2005 17:59:55 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/577</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>M Clock on pys60</title>
      <link>http://snippets.dzone.com/posts/show/571</link>
      <description>See this article in Guido's blog&lt;br /&gt;http://www.artima.com/weblogs/viewpost.jsp?thread=122250&lt;br /&gt;I then try to implement M Clock on pys60. I remove a few&lt;br /&gt;things to make the code short.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;from __future__ import division&lt;br /&gt;from appuifw import *&lt;br /&gt;import math, time, e32&lt;br /&gt;&lt;br /&gt;app.body = c = Canvas()&lt;br /&gt;&lt;br /&gt;radius = 72&lt;br /&gt;bigsize = radius * .975&lt;br /&gt;litsize = radius * .67&lt;br /&gt;mx, my = 88, 72&lt;br /&gt;N = 9   # 2,3,4,5,6, 32, 128&lt;br /&gt;&lt;br /&gt;def draw(hh, mm, ss, colors=(0, 1, 2)):&lt;br /&gt;    # Set bigd, litd to angles in degrees for big, little hands&lt;br /&gt;    # 12 =&gt; 90, 3 =&gt; 0, etc.&lt;br /&gt;    bigd = (90 - (mm*60 + ss) / 10) % 360&lt;br /&gt;    litd = (90 - (hh*3600 + mm*60 + ss) / 120) % 360&lt;br /&gt;    # Set bigr, litr to the same values in radians&lt;br /&gt;    bigr = bigd * math.pi / 180&lt;br /&gt;    litr = litd * math.pi / 180&lt;br /&gt;    # Draw the background colored arcs&lt;br /&gt;    drawbg(bigd, litd, colors)&lt;br /&gt;    # Draw the hands&lt;br /&gt;    c.line([mx, my, &lt;br /&gt;            mx + bigsize*math.cos(bigr),&lt;br /&gt;            my - bigsize*math.sin(bigr)],&lt;br /&gt;            0, width = radius/50)&lt;br /&gt;    c.line([mx, my, &lt;br /&gt;            mx + litsize*math.cos(litr),&lt;br /&gt;            my - litsize*math.sin(litr)],&lt;br /&gt;            0, width = radius/33)&lt;br /&gt;    # Draw the text&lt;br /&gt;    c.text([5, 144-5], u"%02d:%02d:%02d" % (hh, mm, ss), 0xffffff)&lt;br /&gt;&lt;br /&gt;def drawbg(bigd, litd, colors=(0, 1, 2)):&lt;br /&gt;    c.clear(0)&lt;br /&gt;    table = []&lt;br /&gt;    for angle, colorindex in [(bigd - 180/N, 0),&lt;br /&gt;                              (litd - 180/N, 1),&lt;br /&gt;                              (  90 - 180/N, 2)]:&lt;br /&gt;        angle %= 360&lt;br /&gt;        for i in range(N):&lt;br /&gt;            color = 255&lt;br /&gt;            if colorindex in colors:&lt;br /&gt;                color = (N-1-i)*color//(N-1)&lt;br /&gt;            table.append((angle, color, colorindex))&lt;br /&gt;            angle += 360/N&lt;br /&gt;            if angle &gt;= 360:&lt;br /&gt;                angle -= 360&lt;br /&gt;                table.append((0, color, colorindex))&lt;br /&gt;    table.sort()&lt;br /&gt;    table.append((360, None))&lt;br /&gt;    fill = [0, 0, 0]&lt;br /&gt;    for i in range(len(table)-1):&lt;br /&gt;        angle, color, colorindex = table[i]&lt;br /&gt;        fill[colorindex] = color&lt;br /&gt;        if angle &lt; 359:     # for bug when 359==360==0&lt;br /&gt;            c.pieslice([mx-radius,my-radius,mx+radius,my+radius], &lt;br /&gt;                      angle * math.pi/180, 0,    # start, end&lt;br /&gt;                      fill=tuple(fill), width=0)&lt;br /&gt;    c.line([mx+1, my, mx+radius-1, my], tuple(fill))  # complete at 360 deg&lt;br /&gt;&lt;br /&gt;running = 1&lt;br /&gt;def quit():&lt;br /&gt;    global running&lt;br /&gt;    running = 0&lt;br /&gt;app.exit_key_handler= quit&lt;br /&gt;&lt;br /&gt;while running:  # redraw loop&lt;br /&gt;    t = time.time() + time.clock()%1    # time() lack decimal precision&lt;br /&gt;    hh, mm, ss = time.localtime(t)[3:6] # +7*60*60&lt;br /&gt;    draw(hh, mm, ss, (0,1,2))&lt;br /&gt;    e32.ao_sleep(1-t%1)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;You can see the sceenshot from here.&lt;br /&gt;http://flickr.com/photos/korakot/32337789/</description>
      <pubDate>Tue, 09 Aug 2005 03:08:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/571</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>Using Icon as Image</title>
      <link>http://snippets.dzone.com/posts/show/445</link>
      <description>Pys60 1.1.3 have 2 differnt classes for graphics data.&lt;br /&gt;Icon class represents icon (typically from .mbm file)&lt;br /&gt;which can be put in ListBox for selection.&lt;br /&gt;Image class represents a bigger image which can be&lt;br /&gt;drawn upon. These 2 classes can't convert to/from&lt;br /&gt;each other.&lt;br /&gt;&lt;br /&gt;So, I create a library that read .mbm file and draw&lt;br /&gt;an Image from the data there. So, you can make&lt;br /&gt;an icon into an image (now only 1-bit icons).&lt;br /&gt;the module can be downloaded from here&lt;br /&gt;http://larndham.net/service/pys60/icon_image.py&lt;br /&gt;&lt;code&gt;&lt;br /&gt;from appuifw import *&lt;br /&gt;import icon_image, e32&lt;br /&gt;&lt;br /&gt;app.body = c = Canvas()&lt;br /&gt;# choose a bitmap from plenty inside mbm (multi-bitmap)&lt;br /&gt;icon = icon_image('z:\\system\\data\\avkon.mbm', 28) &lt;br /&gt;c.blit(icon)&lt;br /&gt;&lt;br /&gt;e32.ao_sleep(10)  # 10 sec to end program&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Look here for more information.&lt;br /&gt;http://discussion.forum.nokia.com/forum/showthread.php?s=&amp;threadid=63608</description>
      <pubDate>Sun, 03 Jul 2005 02:02:10 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/445</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>Camera preview</title>
      <link>http://snippets.dzone.com/posts/show/417</link>
      <description>Taken (will some mod.) from &lt;br /&gt;http://discussion.forum.nokia.com/forum/showthread.php?s=&amp;threadid=63464&lt;br /&gt;&lt;br /&gt;pys60 (the full name is python for series 60) 1.1.3&lt;br /&gt;provide both camera and graphics display.&lt;br /&gt;So, it is trivial to take photo and preview.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;from appuifw import *&lt;br /&gt;import camera&lt;br /&gt;&lt;br /&gt;canvas=Canvas()&lt;br /&gt;app.body=canvas&lt;br /&gt;&lt;br /&gt;running=1&lt;br /&gt;def quit():&lt;br /&gt;    global running&lt;br /&gt;    running=0&lt;br /&gt;app.exit_key_handler=quit&lt;br /&gt;&lt;br /&gt;while running:&lt;br /&gt;    image = camera.take_photo(size= (160,120))&lt;br /&gt;    canvas.blit(image, target=(8, 12, 168, 132))  # center it&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Here I use the small size (160x120) for preview.&lt;br /&gt;When the real photo is taken I can use the full (defualt)&lt;br /&gt;size of (640x480) on my 6600 phone.</description>
      <pubDate>Thu, 30 Jun 2005 17:02:31 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/417</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>15-square game</title>
      <link>http://snippets.dzone.com/posts/show/414</link>
      <description>py_s60 1.1.3 was released a week ago.&lt;br /&gt;I wonder why there's no more people playing with it.&lt;br /&gt;So, I have created a simple game as an example.&lt;br /&gt;&lt;br /&gt;I don't know what this game is called. It has 4x4 square box&lt;br /&gt;with 15 pieces (1 piece missing). You need to move all the&lt;br /&gt;pieces into sorting order.&lt;br /&gt;&lt;br /&gt;Since the program is so simple, I decided to make the&lt;br /&gt;moving of a piece smoother by moving it bit by bit.&lt;br /&gt;Hope this doesn't make the code to hard to read.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;from appuifw import *&lt;br /&gt;from key_codes import *&lt;br /&gt;import e32, random&lt;br /&gt;&lt;br /&gt;# run and break-loop type of app&lt;br /&gt;sleep = e32.ao_sleep&lt;br /&gt;running = 1&lt;br /&gt;def set_exit():&lt;br /&gt;    global running&lt;br /&gt;    running = 0&lt;br /&gt;app.exit_key_handler= set_exit&lt;br /&gt;&lt;br /&gt;# canvas and typical colors&lt;br /&gt;c = Canvas()&lt;br /&gt;app.body = c&lt;br /&gt;red, green, blue, gray, white = 0xff0000, 0x00ff00, 0x0000ff, 0x777777, 0xffffff&lt;br /&gt;&lt;br /&gt;# randomize number order for pieces&lt;br /&gt;seq = range(1,17)&lt;br /&gt;random.shuffle(seq)&lt;br /&gt;b = [seq[0:4], seq[4:8], seq[8:12], seq[12:16]]&lt;br /&gt;&lt;br /&gt;def piece(i, j, n):&lt;br /&gt;    if n &lt; 10:&lt;br /&gt;        c.text( (12+20*i, 20+20*j), unicode(n))&lt;br /&gt;    else:&lt;br /&gt;        c.text( (7+20*i, 20+20*j), unicode(n))&lt;br /&gt;&lt;br /&gt;def box(i, j, color, fill=None):&lt;br /&gt;    c.rectangle( [5+20*i, 5+20*j, 25+20*i, 25+20*j], color, fill)&lt;br /&gt;&lt;br /&gt;# draw board pieces&lt;br /&gt;for k in range(16):&lt;br /&gt;    j, i = divmod(k, 4)&lt;br /&gt;    piece(i, j, b[j][i])&lt;br /&gt;&lt;br /&gt;y, x = divmod(seq.index(16), 4)&lt;br /&gt;box(x, y, white, white)&lt;br /&gt;moving = 0     # cursor to lock if animating&lt;br /&gt;&lt;br /&gt;# move cursor in dx, dy direction&lt;br /&gt;def move(dx, dy):&lt;br /&gt;    global x,y, moving&lt;br /&gt;    if moving:&lt;br /&gt;        return&lt;br /&gt;    moving = 1&lt;br /&gt;    if 0 &lt;= x-dx &lt; 4 and 0 &lt;= y-dy &lt; 4:&lt;br /&gt;        b[y][x] = b[y-dy][x-dx]&lt;br /&gt;        animate(x-dx, y-dy, dx, dy, b[y][x])&lt;br /&gt;        x -= dx  # the hole move in opposite direction&lt;br /&gt;        y -= dy&lt;br /&gt;    moving = 0&lt;br /&gt;&lt;br /&gt;# moving a piece for x,y to dx, dy direction&lt;br /&gt;def animate(x, y, dx, dy, n):&lt;br /&gt;    if n &lt; 10:&lt;br /&gt;        px = 12&lt;br /&gt;    else:&lt;br /&gt;        px = 7&lt;br /&gt;    for i in range(5):&lt;br /&gt;        c.text( (px+20*x + 4*i*dx, 20+20*y + 4*i*dy), unicode(n))&lt;br /&gt;        sleep(0.05)&lt;br /&gt;        c.text( (px+20*x + 4*i*dx, 20+20*y + 4*i*dy), unicode(n), white)&lt;br /&gt;    c.text( [px+20*(x+dx), 20+20*(y+dy)], unicode(n) )&lt;br /&gt;&lt;br /&gt;# bind arrow keys&lt;br /&gt;c.bind(EKeyRightArrow,lambda:move(1, 0))&lt;br /&gt;c.bind(EKeyLeftArrow,lambda:move(-1, 0))&lt;br /&gt;c.bind(EKeyUpArrow,lambda:move(0, -1))&lt;br /&gt;c.bind(EKeyDownArrow,lambda:move(0, 1))&lt;br /&gt;&lt;br /&gt;# main loop, just wait&lt;br /&gt;while running:&lt;br /&gt;    sleep(0.1)</description>
      <pubDate>Mon, 27 Jun 2005 00:02:53 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/414</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
  </channel>
</rss>
