# Description of the program: # Program creates a graphics window 700 pixels by 600 pixels # Then on any mouse event (click, release or drag) it informs the user # about it in the graphics window along with the coordinates of the event from cs1graphics import * class IdentifyMouseEventHandler(EventHandler): def __init__(self,textObj): EventHandler.__init__(self) self._text=textObj self._text.setMessage('No mouse events yet') def handle(self,event): if event.getDescription() == 'mouse click': p=event.getMouseLocation() # get location of Mouse - point t='mouse click at ' + str(p) # alternate: # t='mouse click at' + str(p.getX()) + ',' + str(p.getY()) + ')' self._text.setMessage(t) elif event.getDescription() == 'mouse release': p=event.getMouseLocation() # get location of Mouse - point t='mouse release at ' + str(p) self._text.setMessage(t) elif event.getDescription() == 'mouse drag': p=event.getMouseLocation() # get location of Mouse - point t='mouse drag at ' + str(p) self._text.setMessage(t) else: print 'not a mouse event' def main(): paper=Canvas(700,600,'light yellow','Playing with mouse events') text1=Text('Event:',12,Point(140,550)) text1.setFontColor('dark blue') paper.add(text1) text2=Text('',12,Point(300,550)) paper.add(text2) mouseEvent=IdentifyMouseEventHandler(text2) # create the handler print 'Activating event handler' paper.addHandler(mouseEvent) # activate the handler print 'Event handler should be working now' main()