Question

Problem Description Design and implement a Python called draw.txt a list of 2D graphical instructions and executes them on th

Хо, YO) and width and height given by W and H, using current OColor, the current OColor and black, red, green, accepted by ez

media%2F1c1%2F1c17c33f-bdd2-49f4-a230-69

0 0
Add a comment Improve this question Transcribed image text
Answer #1


from ezgraphics import GraphicsWindow


# Create the graphics window.
win = GraphicsWindow(400, 400)
win.setTitle("Simple Canvas")
canvas = win.canvas()
errorlist = []
count = 0

with open('draw.txt', 'r') as f:
    for line in f:
        count += 1 # counter for lines
        drawinstr = line.rstrip('\n').split(' ') # striping off newline and splitting string based on whitespace
        operator = drawinstr[0]

        if operator.lower() == 'line':
            # Check for number of parameters
            if len(drawinstr) != 5:
                errorlist.append('in line ' + str(count) + ' : Wrong'
                                 + ' number of parameters for oval')
                continue
            # Check for interger parameters
            if not (drawinstr[1].isdigit() and drawinstr[2].isdigit()
            and drawinstr[3].isdigit() and drawinstr[4].isdigit()):
                   errorlist.append('in line ' + str(count) + ' : Wrong '
                                    + 'parameter types')  
                   continue
             
            canvas.drawLine(int(drawinstr[1]), int(drawinstr[2]),
                                 int(drawinstr[3]), int(drawinstr[4]))

        if operator.lower() == 'oval':
            # Check for number of parameters
            if len(drawinstr) != 5:
                errorlist.append('in line ' + str(count) + ' : Wrong'
                                 + ' number of parameters for oval')
                continue
            # Check for interger parameters  
            if not (drawinstr[1].isdigit() and drawinstr[2].isdigit()
            and drawinstr[3].isdigit() and drawinstr[4].isdigit()):
                errorlist.append('in line ' + str(count) + ' : Wrong '
                             'parameter types')
                continue
          
            canvas.drawOval(int(drawinstr[1]), int(drawinstr[2]),
                                 int(drawinstr[3]), int(drawinstr[4]))
          
        if operator.lower() == 'rectangle':
            # Check for number of parameters
            if len(drawinstr) != 5:
                errorlist.append('in line ' + str(count) + ' : Wrong '
                                  'number of parameters for rectangle')
                continue
            # Check for interger parameters
            if not (drawinstr[1].isdigit() and drawinstr[2].isdigit()
            and drawinstr[3].isdigit() and drawinstr[4].isdigit()):
                errorlist.append('in line ' + str(count) + ' : Wrong '
                             'parameter types')     
                continue
          
            canvas.drawRectangle(int(drawinstr[1]), int(drawinstr[2]),
                                 int(drawinstr[3]), int(drawinstr[4]))
          
        if operator.lower() == 'text':
            # Check for number of parameters
            if len(drawinstr) != 5:
                errorlist.append('in line ' + str(count) + ' : Wrong'
                                 + 'number of parameters for text')
                continue
            # Check for interger parameters
            if not (drawinstr[1].isdigit() and drawinstr[2].isdigit()
            and drawinstr[3].isdigit()):
                errorlist.append('in line ' + str(count) + ' : Wrong '
                             'parameter types')
                continue
          
            # Set Text Font Size
            canvas.setTextFont(size = int(drawinstr[3]))
            # Draw text as desired place on canvas
            canvas.drawText(int(drawinstr[1]), int(drawinstr[2]),
                            drawinstr[4])

        if operator.lower() == 'fcolor':
            if len(drawinstr) == 2:
                # Check for color lists for FColor
                if not (drawinstr[1] == 'gray' or drawinstr[1] == 'black'
                        or drawinstr[1] == 'red' or drawinstr[1] == 'green'
                        or drawinstr[1] == 'blue' or drawinstr[1] == 'magenta'):
                    errorlist.append('in line ' + str(count) + ' : Wrong'
                                 + ' color name for OColor')
                    continue
                # Set fill color of drawing object
                canvas.setFill(drawinstr[1])
              
        if operator.lower() == 'ocolor':
            if len(drawinstr) == 2:
                # Check for color lists for OColor
                if not (drawinstr[1] == 'gray' or drawinstr[1] == 'black'
                        or drawinstr[1] == 'red' or drawinstr[1] == 'green'
                        or drawinstr[1] == 'blue' or drawinstr[1] == 'magenta'):
                    errorlist.append('in line ' + str(count) + ' : Wrong'
                                 + ' color name for FColor')
                    continue
                 # Set outline color of drawing object
                canvas.setOutline(drawinstr[1])
      
        # Check for Unknown instruction
        if not (operator.lower() == 'line' or operator.lower() == 'oval' or
        operator.lower() == 'rectangle' or operator.lower() == 'text' or
        operator.lower() == 'fcolor' or operator.lower() == 'ocolor'):
            errorlist.append('in line ' + str(count) + ' : Unknown ' +
                             'instruction')
            continue
      
# Display Errors      
print('There are %d errors'%len(errorlist))
print('List of errors:')
for error in errorlist:
    print(error)
                                  
# Wait for the user to close the window.
win.wait()


------------------------------------------------------------

contents of input file "draw.txt"

FColor gray
Rectangle 20 20 150
Rectangle 20 20 150 150
FColor red
Oval 20 20 150 150
OColor green
Text 60.5 90 16 COMP2101
Text 60 90 16 COMP2101
OColor magenta
Line 170 170 180 180
FColor gray
OColor black
Rectangle 180 180 100 100
FColor blue
Oval 180 180 100 100
OColor red
Text 190 220 14 Homework#3
OColor magenta
Line 280 280 290 290
FColor gray
Circle
OColor black
Rectangle 290 290 50 50
FColor green
Oval 290 290 50 50
FColor color
OColor blue
Text 295 310 12 SP2019

------------------------------------

output

IPython console Simple Canvas Console 1/A × Python 3.7.1 (default, Dec 14 2018, 19:28:38) Type copyright, credits or lic

Add a comment
Know the answer?
Add Answer to:
Problem Description Design and implement a Python called draw.txt a list of 2D graphical instructions and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (Python) Write a program called sales.py that uses a list to hold the sums of the...

    (Python) Write a program called sales.py that uses a list to hold the sums of the sales for each month. The list will have 12 items with index 0 corresponds with month of “Jan”, index 1 with month “Feb”, etc. o Write a function called Convert() which is passed a string which is a month name (ex. “Jan”) and returns the matching index (ex. 0). Hint – use a list of strings to hold the months in the correct order,...

  • Activities Define a struct called Unit containing the following fields I Unit code Unit credit hours...

    Activities Define a struct called Unit containing the following fields I Unit code Unit credit hours Unit semester of study List of prerequisites Number of prerequisites List of post requisites Number of post requisites Inside your main function declare an array of type Unit called units with size 20 elements. This array will contain the information related to all the units in our diploma program. Each of the elements of this array correspond with one our diploma units. II III...

  • Description In this homework, you are asked to implement a multithreaded program that will allow ...

    Description In this homework, you are asked to implement a multithreaded program that will allow us to measure the performance (i.e, CPU utilization, Throughput, Turnaround time, and Waiting time in Ready Queue) of the four basic CPU scheduling algorithms (namely, FIFO, SJE PR, and RR). Your program will be emulating/simulating the processes whose priority, sequence of CPU burst time(ms) and I'O burst time(ms) will be given in an input file. Assume that all scheduling algorithms except RR will be non-preemptive,...

  • I'm making a To-Do list in Python 2 and had the program running before remembering that...

    I'm making a To-Do list in Python 2 and had the program running before remembering that my professor wants me to put everything in procedures. I can't seem to get it to work the way I have it now and I can't figure it out. I commented out the lines that was originally in the working program and added procedures to the top. I've attached a screenshot of the error & pasted the code below. 1 todo_list = [] 2...

  • Instructions: Consider the following C++ program. It reads a sequence of strings from the user and...

    Instructions: Consider the following C++ program. It reads a sequence of strings from the user and uses "rot13" encryption to generate output strings. Rot13 is an example of the "Caesar cipher" developed 2000 years ago by the Romans. Each letter is rotated 13 places forward to encrypt or decrypt a message. For more information see the rot13 wiki page. #include <iostream> #include <string> using namespace std; char rot13(char ch) { if ((ch >= 'a') && (ch <= 'z')) return char((13...

  • can you please follow all the instructions ? The answers that I got has either missing...

    can you please follow all the instructions ? The answers that I got has either missing range for loop or one funtion . Read the instructions carefully. At least 10% will be deducted if the instructions are not followed. For general lab requirements, see General Lab Requirements. Do not prompt the user for input during the execution of your program. Do not pause at the end of the execution waiting for the user's input. Notes 1. Your program should use...

  • 1. Define Structure UNIT as described in the list: ınit char codease complete the question int...

    1. Define Structure UNIT as described in the list: ınit char codease complete the question int numberOfCreditHours int semester int numberOfPreReq; pointer to number of pre-requisites eg:like char preRequisite[numberOfPreReq][10]; char* preReq int numberOfPostReq; pointer to number of post-requisites eg:like char postRequisite[numberOfPostReq] [1 0]; char* postReq NOTE: this is a BIG question and has to be asked seperately, answering only one question here. pluginfile.php/94428/mod resource/content/1/lab 6202 %20 %28file %20and%20parsing % 29.pdf Expected C++Skills to finish this activity Simple data types C++...

  • AA. Final Project - Improved JavaFX GUI Personal Lending Library Description: In this project we will...

    AA. Final Project - Improved JavaFX GUI Personal Lending Library Description: In this project we will improve our personal lending library tool by (1) adding the ability to delete items from the library, (2) creating a graphical user interface that shows the contents of the library and allows the user to add, delete, check out, or check in an item. (3) using a file to store the library contents so that they persist between program executions, and (4) removing the...

  • I need help with this assignment in C++, please! *** The instructions and programming style detai...

    I need help with this assignment in C++, please! *** The instructions and programming style details are crucial for this assignment! Goal: Your assignment is to write a C+ program to read in a list of phone call records from a file, and output them in a more user-friendly format to the standard output (cout). In so doing, you will practice using the ifstream class, I'O manipulators, and the string class. File format: Here is an example of a file...

  • The second phase of your semester project is to write pass one of a two‑pass assembler...

    The second phase of your semester project is to write pass one of a two‑pass assembler for the SIC assembler language program. As with Phase 1, this is to be written in C (not C++) and must run successfully on Linux. Pass one will read each line of the source file, and begin the process of translating it to object code. (Note: it will be to your advantage to have a separate procedure handle reading, and perhaps tokenizing, the source...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT