class logo

CS 122 - Assignment 4

Graphics fun! Let's write a program that will display pictures loaded from a file.

Here's my solution for this assignment. Note that as with all assignments, my version is not the only way to do it!

solution.py

Posted Friday, August 7

In this assignment you will write a program that reads in a data file in our custom data format. The data file will contain information about a picture that you will display using the graphics library we've been working with during class. The picture will be made up of the primitive shapes we know how to work with, Circle, Line, Rectangle, Oval, and Text.

The program will first ask the user for the name of the file to display. It will then open a graphics window, and display the shapes listed in the file. The graphics window must be sized to 640 x 480 and display the file name in the title bar.

Each data file is made up of lines of instructions that control how an image is drawn using the graphics library. Here is what the output for the different files looks like on my computer. Your window might look slightly different, but the contents of the window should be the same.

And here are links to the data files themselves. Your program must be able to display the following data files correctly:

The data files are intentionally simple. Open them up in smultron (or just in your web browser) to see what they look like. There's no header or footer in the file and each line can be processed on its own, so the format is fairly straight-forward. There are two types of lines in the file. One type defines a circle to display, and the other type defines lines to display. The formats for each type are:

  • Line start_x start_y end_x end_y color
  • Circle x y radius color

I recommend that you tackle this project in small steps. Read all the instructions carefully, but then do just _one_ step at a time:

  1. First get a program that can prompt the user for a file and print all the lines of the file one line at a time. Don't worry about anything to do with graphics yet, just start simple, reading the file and printing out each line one at a time.
  2. Now have your program create a graphics window and have it stay up until the user clicks in it to exit your program. At this point we're not worrying about displaying anything from the files, just get the window up.
  3. Now we get to the meat of things. Instead of just printing out each line in the file, you want to examine the line and do something with it. If the line starts with "Circle" you want to draw a circle, if it starts with "Line", you want to draw a line. Let's start with just with circle, get that working, and then worry about the Lines later. Create a function called displayCircle(win, x, y, radius, color). Split each line up into its parts (using the split() function). If the first element is "Circle", call the displayCircle function using the appropriate elements of the line as parameters. when you get this done, you should be able to display the circle.txt file.
  4. Now add a function for the Line data type. Define a new function displayLine(win, x1, y1, x2, y2, color), split the string into its components and call the function with them.

Add support for additional graphics types which allow more complex drawings. The new types are:

  • Rectangle upleft_x upleft_y lowright_x lowright_y color
  • Oval upleft_x upleft_y lowright_x lowright_y color
  • Text x y color text_to_display

There are some additional data files you should be able to display now:

The output from these files should look something like this:

Add a polygon data type. You'll need to define the format of the line. If you do this, please attach a data file that uses it so I can test your solution.

Submit your python program

This assignment is due Wednesday, August 2nd, at 7:00 am

Use this form to submit your completed assignment. You can submit an assignment multiple times, and I will generally only look at the last submission. If you do submit more than once, make sure that each submission is complete on its own and doesn't rely on something you submitted previously.

Student number (no dashes):
This is the 9-digit number on your student body card. It probably starts 950 or 951 and it looks something like this: 950123456. This number is used to identify your submission so please enter it carefully.
Name (first and last):
Email address:
Notes about this assignment:
Use the notes field to mention if you worked in a group or got help with any part of the assignment. Also note if something doesn't work correctly, it shows that you know about the problem. If you don't mention it, I'll assume you didn't test your project, and thus will grade you more harshly.
Your solution to this assignment
New image data file (optional)
You can create new data files with different instructions in them than I've given here. If you create a cool one, attach it so I can see what you did. (This is an optional portion, you do not need to submit something here).