Difference between revisions of "CSC111 Event-Driven Programming with Graphics111.py 2014"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- <br /> <br /> __TOC__ <br /> <br /> =graphics111.py= <br /> This program is based on Horstmann and Necaise's graphics library. The new version sports several new...")
 
(graphics111.py)
Line 6: Line 6:
 
<br />
 
<br />
 
<br />
 
<br />
=graphics111.py=
+
=Introduction=
 
<br />
 
<br />
 
This program is based on Horstmann and Necaise's graphics library.  The new version sports several new additions:
 
This program is based on Horstmann and Necaise's graphics library.  The new version sports several new additions:
Line 19: Line 19:
  
 
p1 = Polygon( (100, 100, 150, 50, 350, 50, 400, 100),  
 
p1 = Polygon( (100, 100, 150, 50, 350, 50, 400, 100),  
                      (255, 0, 0 ) )
+
              (255, 0, 0 ) )
 
p1.draw( canvas )
 
p1.draw( canvas )
 
</source>
 
</source>

Revision as of 08:42, 20 April 2014

--D. Thiebaut (talk) 08:41, 20 April 2014 (EDT)




Contents



Introduction


This program is based on Horstmann and Necaise's graphics library. The new version sports several new additions:

  • A Circle class that is specified by its center coordinates, x and y, its radius, and its color
  • Wheel class that is made of two concentric circles, the outer one black, the inner one grey. The wheel object is specified by its center coordinates, and its outer radius
  • A Rectangle class that is specified by the coordinates of its top-left corner, its width, height, and color.
  • A Polygon class that is specified by a list of x,y coordinates (at least 3 pairs of coordinates), and its color.
Example:


from graphics111 import Polygon

p1 = Polygon( (100, 100, 150, 50, 350, 50, 400, 100), 
              (255, 0, 0 ) )
p1.draw( canvas )


  • A Menu class that provides 4 basic buttons: a plus symbol, a minus symbol, a left-arrow symbol, and a right arrow symbol. For the menu to work, one must setup a call-back function (see below):


Example


def main():
    global menu 
    
    win = GraphicsWindow(MAXWIDTH, MAXHEIGHT)
    canvas = win.canvas()
    
    canvas.setCallbackFunction( mouseEvent )    
    canvas.setBackground( 0, 250, 250 )

    menu = Menu(  )
    menu.draw( canvas )

    win.wait()
    win.close()


  • A call-back functionality. The main graphics library can be made aware of events such as mouse clicks, and we can define our own function that should be called when the mouse is clicked over the canvas. This is what a call-back function is. This way we can have a function that can modify the canvas in some way when the mouse is clicked at different locations on the canvas.
Example


def mouseEvent( win, canvas, x, y ):
    global menu
    
    # has a menu button been clicked?
    # if so define an action for each one

    button = menu.buttonClicked( x, y )

    if button == "LeftArrow":
        print( "left-arrow clicked!" )
        return
    if button == "RightArrow":
        print( "right-arrow clicked!" )
        return
    if button == "Minus":
        print( "minus clicked!" )
        return
    if button == "Plus":
        print( "plus clicked!" )
        return

def main():
    global menu 
    
    win = GraphicsWindow(MAXWIDTH, MAXHEIGHT)
    canvas = win.canvas()
    
    canvas.setCallbackFunction( mouseEvent )    
    canvas.setBackground( 0, 250, 250 )

    menu = Menu(  )
    menu.draw( canvas )

    win.wait()
    win.close()