# lab8app.py # Walker M. White (wmw2) # October 13, 2012 """GUI application for Lab 8. The only thing you will need to modify in this file is the method drawShapes.""" # Import a bunch of Kivy stuff import kivy from kivy.app import App from kivy.uix.floatlayout import FloatLayout from kivy.graphics.vertex_instructions import Rectangle from kivy.graphics.context_instructions import Color from kivy.config import Config # Local modules for drawing shapes. import math import colormodel from shapes import * from lab8 import * class Panel(FloatLayout): """Drawing canvas to display the shape. A simple Kivy panel that contains Shape objects. Each Shape object maintains its own position; the Panel draws them 'where they lay'.""" def __init__(self,**kw): """Constructor: Makes a new Panel for drawing shapes Precondition: **kw are Kivy key-word arguments""" # Pass Kivy arguments to super class. super(Panel,self).__init__(**kw) # Make the background solid white color = Color(1.0,1.0,1.0,1.0) self.canvas.add(color) rect = Rectangle(pos=self.pos, size=self.size) self.canvas.add(rect) # Draw the shapes self.drawShapes() def draw(self,shape): """Adds Widget only if Canvas is defined. Trick to prevent seg-faults. Precondition: shape is a Shape object.""" assert isinstance(shape,Shape), `shape`+' is not a shape' if not shape.canvas is None: self.add_widget(shape) def drawShapes(self): """Draws the shapes on the panel. Only needs to be called once at creation. Ones the shapes are 'drawn' they need never be drawn again.""" # the "1st" parallelogram is the top right one. h = 30 # length of horizontal side of 1st parallelogram v = 50 # length of other side of 1st parallelogram d1 = 20 # distance from (x,y) to 1st parallelogram's bottom horizontal line d2 = 10 # distance from (x,y) to 1st rhombus's bottom horizontal line x = 125 # Center x-coordinate of the shape. y = 175 # of the top-left point of the 1st parallelogram if d is 0 # vertical distance to top of parallelogram vert1 = int(round(math.sqrt(v*v - d1*d1))) # vertical distance to top of rhombus vert2 = int(round(math.sqrt(h*h - d2*d2))) print '\n\n' # Some blank lines # Drawing commands s1 = Parallelogram(x, y, h, v, d1) s1.color = colormodel.RED self.draw(s1) print str(s1) s2 = Parallelogram(x-d1-h, y, h, v, -d1) s2.color = colormodel.RED self.draw(s2) print str(s2) s3 = Parallelogram(x, y-vert1, h, v, -d1) s3.color = colormodel.RED self.draw(s3) print str(s3) s4 = Parallelogram(x-d1-h, y-vert1, h, v, d1) s4.color = colormodel.RED self.draw(s4) print str(s4) s5 = Square(x-h, y-vert1-2*h, 2*h) s5.color = colormodel.GREEN self.draw(s5) print str(s5) s6 = Rhombus(x, y-vert1-2*h-vert2, h, d2) s6.color = colormodel.BLUE self.draw(s6) print str(s6) s7 = Rhombus(x-h-d2, y-vert1-2*h-vert2, h, -d2) s7.color = colormodel.BLUE self.draw(s7) print str(s7) # REPLACE ME! s8 = Line(x+h+d1, y, x+h, y-vert1-2*h) s8.color = colormodel.BLACK self.draw(s8) print str(s8) # REPLACE ME! s9 = Line(x-h-d1, y, x-h, y-vert1-2*h) s9.color = colormodel.BLACK self.draw(s9) print str(s9) print '\n\n' # Some blank lines class Lab8App(App): """Primary application object. Create and run to get the panel.""" def build(self): """Build application with a single internal panel""" Config.set('graphics', 'width', '250') Config.set('graphics', 'height', '250') return Panel(size=(250,250)) # Application Code if __name__ == '__main__': Lab8App().run()