130 likes | 209 Views
Graphics Lab: MyPaint. Dan Maselko. MyPaint Description. For this assignment you will be implementing a very simple paint program. You should be able to draw shapes with an arbitrary number of sides by clicking with the mouse. You should allow the user to specify the fill color for the shape.
E N D
Graphics Lab: MyPaint Dan Maselko
MyPaint Description • For this assignment you will be implementing a very simple paint program. • You should be able to draw shapes with an arbitrary number of sides by clicking with the mouse. • You should allow the user to specify the fill color for the shape. • The drawing should exit when the user double clicks and they are not in the middle of drawing a shape. • Graphics Library Quick Reference
Step 0 – Getting Started • Make a lab8 directory in your 201/labs directory • Copy the graphics.py file from Mr. Lupoli’s directory into your lab8 directorycp /afs/umbc.edu/users/s/l/slupoli/pub/labCode201/graphics.py . • Use emacs to create a lab8.py • Important Note: You need to use a different version of python to run this lab:/usr/local/bin/python2.4 lab8.py
Drawing the Shape • Each Point that the user clicks is a point in the shape • Lines should be drawn between each point as the shape is being drawn • When you finish drawing a shape, you should double click on the last point that you want to draw. This should cause a Polygon to be drawn using the points that the user clicked • Keep a list of Points that the user clicks • We want to stop drawing Polygons when the user double clicks and they aren’t in the middle of drawing a shape
Drawing the Shape • Why do we need the Polygon? • The user should be able to type in the fill color of the shape using an Entry object • We can’t “fill” the space between Lines, but we can fill a Polygon • http://userpages.umbc.edu/~slupoli/notes/Python/labs/videos/Paint%20Lab.html
Step 1 – Really Getting Started • Begin by opening a Graphics window • Get two separate mouse clicks from the user and append them to a list • Draw a line between the two points
Step 2 – (Really slow) Double Clicking • We need to have a function that checks if two points are equal so we know to end drawing. We can’t do this: • We actually need to compare the X and Y coordinates of each Point, not just the points themselves • Put this comparison into a function! • Using this function, you should be able to pass two Points to it, and the function should check if their X and Y values are equal • Why would we want to make this into a function? pt1 = Point(12, 34) pt2 = Point(12, 34) while pt1 == pt2: # Do stuff
Hint time!! • Using a list, what code would we use to get the last Pt added to the list • pointList[-1] • How about the one before THAT!!! • pointList[-2]
Step 3 – Drawing Until a Double Click • Modify your code so that you can continue to draw lines while you have not double clicked • What kind of control structure should we use? • You should check if the first two points in the list are equal • Make sure you start with an empty list when you start drawing each line
Step 4 – Drawing More Than Just Lines • We want to keep adding points to the list while the last two points that we clicked are not equal • What is already in our program that checks this condition? • We also want to always draw lines between the last two points that we clicked • Keep in mind that we want to do all of this inside of the control structure that we just made in the previous step
Step 5 – The Polygon • If the user is drawing a bunch of lines, and they double click, a Polygon should be drawn • To make this Polygon, you should use the list of points that you’ve been keeping track of as the user clicked the mouse
Step 6 – Adding Color • Add an Entry object to your graphics window when you open it for the first time, so that the user can type in the color they want to fill the Polygon with • Right before we draw the Polygon, you should get the text from the Entry object, and set the fill color of the Polygon to that color • Make sure to strip off any whitespace from the fill color! polygon.setFill(fillColor.strip())
Bonus Step – Outline Color • Add another Entry object so that the user can specify the outline color of the Polygon • The color should be applied to the outline color of the Polygon when we draw it, and it should be the color of each Line that we draw while we are clicking to draw our shape