DOC PREVIEW
Objects and Graphics

This preview shows page 1-2-3-19-20-39-40-41 out of 41 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Chapter 5 Objects andGraphicsObjectives• To understand the concept of objects and how they can be used to simplifyprograms.• To be familiar with the various objects available in the graphics library.• To be able to create objects in programs and call appropriate methods toperform graphical computations.• To understand the fundamental concepts of computer graphics, especiallythe role of coordinate systems and coordinate transformations.• To understand how to work with both mouse and text-based input in agraphical programming context.• To be able to write simple interactive graphics programs using the graphicslibrary.5.1 OverviewSo far we have been writing programs that use the built-in Python data types fornumbers and strings. We saw that each data type could represent a certain setof values, and each had a set of associated operations. Basically, we viewed thedata as passive entities that were manipulated and combined via active opera-tions. This is a traditional way to view computation. To build complex systems,123124 Chapter 5. Objects and Graphicshowever, it helps to take a richer view of the relationship between data andoperations.Most modern computer programs are built using an object-oriented (OO) ap-proach. Object orientation is not easily defined. It encompasses a number ofprinciples for designing and implementing software, principles that we will re-turn to numerous times throughout the course of this book. This chapter pro-vides a basic introduction to object concepts by way of some computer graphics.Graphical programming is a lot of fun and provides a great vehicle for learn-ing about objects. In the process, you will also learn the principles of computergraphics that underlie many modern computer applications. Most of the ap-plications that you are familiar with probably have a so-called Graphical UserInterface (GUI) that provides visual elements like windows, icons (representa-tive pictures), buttons and menus.Interactive graphics programming can be very complicated; entire textbooksare devoted to the intricacies of graphics and graphical interfaces. Industrial-strength GUI applications are usually developed using a dedicated graphics pro-gramming framework. Python comes with its own standard GUI module calledTkinter. As GUI frameworks go, Tkinter is one of the simplest to use, and Pythonis a great language for developing real-world GUIs. Still, at this point in yourprogramming career, it would be a challenge to learn the intricacies of any GUIframework, and doing so would not contribute much to the main objectives ofthis chapter, which are to introduce you to objects and the fundamental princi-ples of computer graphics.To make learning these basic concepts easier, we will use a graphics library(graphics.py) specifically written for use with this textbook. This library is awrapper around Tkinter that makes it more suitable for beginning programmers.It is freely available as a Python module file1and you are welcome to use it asyou see fit. Eventually, you may want to study the code for the library itself as astepping stone to learning how to program directly in Tkinter.5.2 The Object of ObjectsThe basic idea of object-oriented development is to view a complex system as theinteraction of simpler objects. The word objects is being used here in a specifictechnical sense. Part of the challenge of OO programming is figuring out the1See Appendix B for information on how to obtain the graphics library and other supportingmaterials for this book.5.3. Simple Graphics Programming 125vocabulary. You can think of an OO object as a sort of active data type thatcombines both data and operations. To put it simply, objects know stuff (theycontain data), and they can do stuff (they have operations). Objects interact bysending each other messages. A message is simply a request for an object toperform one of its operations.Consider a simple example. Suppose we want to develop a data processingsystem for a college or university. We will need to keep track of considerableinformation. For starters, we must keep records on the students who attendthe school. Each student could be represented in the program as an object. Astudent object would contain certain data such as name, ID number, coursestaken, campus address, home address, GPA, etc. Each student object would alsobe able to respond to certain requests. For example, to send out a mailing, wewould need to print an address for each student. This task might be handled bya printCampusAddress operation. When a particular student object is sent theprintCampusAddress message, it prints out its own address. To print out all theaddresses, a program would loop through the collection of student objects andsend each one in turn the printCampusAddress message.Objects may refer to other objects. In our example, each course in the collegemight also be represented by an object. Course objects would know things suchas who the instructor is, what students are in the course, what the prerequisitesare, and when and where the course meets. One example operation might beaddStudent, which causes a student to be enrolled in the course. The studentbeing enrolled would be represented by the appropriate student object. Instruc-tors would be another kind of object, as well as rooms, and even times. You cansee how successive refinement of these ideas could lead to a rather sophisticatedmodel of the information structure of the college.As a beginning programmer, you’re probably not yet ready to tackle a collegeinformation system. For now, we’ll study objects in the context of some simplegraphics programming.5.3 Simple Graphics ProgrammingIn order to run the graphical programs and examples in this chapter (and therest of the book), you will need a copy of the file graphics.py that is suppliedwith the supplemental materials. Using the graphics library is as easy as placinga copy of the graphics.py file in the same folder as your graphics program(s).Alternatively, you can place it in a system directory where other Python librariesare stored so that it can be used from any folder on the system.126 Chapter 5. Objects and GraphicsThe graphics library makes it easy to experiment with graphics interactivelyand write simple graphics programs. As you do, you will be learning principlesof object-oriented programming and computer graphics that can be applied inmore sophisticated graphical programming environments. The details of thegraphics module will be


Objects and Graphics

Download Objects and Graphics
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Objects and Graphics and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Objects and Graphics 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?