#!/usr/bin/env python # Skeleton program for csc 15 assignment, import pygtk import gtk width = 800 # width of window height = 650 # height of window window = gtk.Window(gtk.WINDOW_TOPLEVEL) window.set_title("Simplified Drawing Example") window.connect("destroy", lambda w: gtk.main_quit()) area = gtk.DrawingArea() area.set_size_request(width,height) window.resize(width,height) window.add(area) def area_expose_cb(area, event): global drawfigure drawfigure(area.window) area.connect("expose-event", area_expose_cb) window.show_all() gc = area.window.new_gc() cmap = gc.get_colormap() ########### You may optionally experiment with the code below # Colors that you can use green = cmap.alloc_color(red=0,green=65535,blue=0) blue = cmap.alloc_color(red=0,green=0,blue=65535) red = cmap.alloc_color(red=65535,green=0,blue=0) white = cmap.alloc_color("white") black = cmap.alloc_color("black") gray = cmap.alloc_color("gray") yellow = cmap.alloc_color("yellow") purple = cmap.alloc_color("purple") gc.set_foreground(green) gc.set_background(blue) gc.line_width = 1 ## drawcircle actually calls draw_arc: def drawcircle(brush,x,y,radius,fill): brush.draw_arc(gc,fill,x-radius,y-radius,2*radius,2*radius,0,360*64) ############ def drawfigure(brush): ###################### Your code starts here ######################### #### You must work out the coordinates on the worksheet first. ####### ### the following code draws a diamond shape. You need to change it. gc.set_foreground(white) # sets drawing color brush.draw_rectangle(gc,True,0,0,width,height) # clear background gc.set_foreground(black) # draw a diamond centered at coordinate x, y: x = width/2 # width is a variable holding the width of the window y = height/2 # height of window size = 100 # controls size of diamond # draw four sides of the diamond: brush.draw_line(gc,x-size,y,x,y-size) # draw upper left side brush.draw_line(gc,x,y-size,x+size,y) # upper right side brush.draw_line(gc,x+size,y,x,y+size) # lower right side brush.draw_line(gc,x,y+size,x-size,y) # lower left side ## Note: the code to draw a circle is a little different: # drawcircle(brush,x,y,radius,False) # where x,y is center of circle. # and False draws only border ###################### Your code ends here ########################### # end drawfigure ## The following line must be the last line in the program gtk.main()