Monday, July 27, 2009

Canvas Calender

Introduction

This article demonstrates Calender on canvas. The simplest mobile application development tool, PyS60, is used to develop this canvas based calender. The code snippet given in this article generates canvas based calender.

The following modules are used when developing this application:

*appuifw module
*graphics module
*e32 module

The following functions are used to create canvas based calender:

* draw_screen()':draw the black screen on canvas
* draw_text()':write some text on square
* print_number: print number on square

The following parameters are used when developing this application. They are customizable.

* number_of_square : Initialize number of column
* number_of_lines : Initialize number of row
* Horizontal_Separator : Separate the space between column
* Vertical_Separator : Separate the space between row

All the functions and parameters are defined in source code in detail.


Code snippet

The code for the calender is below.

# import module
import appuifw,graphics,e32

# define color code
BLACK= (0,0,0)
RED=(255,0,0)
BLUE= (0,0,255)
GREEN=(0,255,0)
ORANGE= (255,127,0)
WHITE=(255,255,255)
global width,height
width=320
height=320

#Global variables
sqrt_x=width/16
sqrt_y=height/4
sqrt_width=30
sqrt_height=25
number_of_square=7
number_of_lines=6
Horizontal_Separator=3
Vertical_Separator=3

TextColor=RED
PrintNumber=WHITE
canvas=None
img=None

def handle_redraw(rect):
canvas.blit(img)

# exit function
def quit():
app_lock.signal()

#---------------------------
#draw draw screen
#---------------------------

def draw_screen():

#Global variable for application UI
global canvas

#Create an instance of Canvas and set it as the application's body
canvas = appuifw.Canvas(redraw_callback = handle_redraw,event_callback = None)
appuifw.app.body=canvas

#Set the screen to full
appuifw.app.screen = 'full'
appuifw.app.exit_key_handler=quit

#Global variable for application UI
global img
img = graphics.Image.new((canvas.size))

#Clear the image
img.clear(BLACK)
global x1,y1,x2,y2, sqrt_x, sqrt_y, sqrt_width, sqrt_height,number_of_square, Horizontal_Separator,Vertical_Separator,number_of_lines
j=0
while j i=0
k=0

#define rectangle coordinate
x1=sqrt_x
y1=sqrt_y+(j*(sqrt_height+Vertical_Separator))
x2=x1+sqrt_width
y2=y1+sqrt_height
while (i #draw rectangle
img.rectangle((x1+ (i*sqrt_width),y1,x2+ (i*sqrt_width)-Horizontal_Separator,y2),outline=BLUE)
i=i+1
handle_redraw(None)
j=j+1
Total_x, Total_y = canvas.size
img.text((35,50), u"CALENDER", fill = BLUE,font=(u'Nokia Hindi S60',40,appuifw.STYLE_BOLD))
handle_redraw(None)

#define draw_text function
def draw_text():
global sqrt_x,sqrt_y,t1,t2,sqrt_height,y3,x3, number_of_square
t1=5
t2=20
y3=sqrt_y+t2
i=1
a=[u'S',u'M',u'T',u'W',u'T',u'F',u'S']
while i< number_of_square +1:
# draw text on canvas
img.text(((sqrt_x+(i*t1)+((i-1)*sqrt_height)),y3), unicode(a[i-1]), fill=TextColor,font= (u'Nokia Hindi S60',20,appuifw.STYLE_BOLD))
i=i+1
handle_redraw(None)


def draw_number(i, no_of_days_in_month):
global number_of_lines, number_of_square
j=0
a=1
while j<(number_of_lines-1):
if i==number_of_square:
i=0
t1=5
x1=sqrt_x
y1=sqrt_y+((j+1)*sqrt_width)+14
while (i img.text((x1+ (i*sqrt_height)+((i+1)*t1),y1),unicode(a),fill=PrintNumber)
a=a+1
i=i+1
if a==no_of_days_in_month+1:
break
handle_redraw(None)
#a=a+1
j=j+1
#calling the function
draw_screen()
draw_text()
draw_number(3,31)
app_lock=e32.Ao_lock()

#Wait for the user to request the exit
app_lock.wait()




Screenshots:



























No comments:

Post a Comment