Top Banner
Some Graphics CS303E: Elements of Computers and Programming
23

Some Graphics CS303E: Elements of Computers and Programming.

Jan 05, 2016

Download

Documents

Karin Heath
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Some Graphics CS303E: Elements of Computers and Programming.

Some Graphics

CS303E: Elements of Computers and Programming

Page 2: Some Graphics CS303E: Elements of Computers and Programming.

Announcements

Test NEXT Wednesday, May 1st– Same time (regular class time)– Place: UTC 2.112A

Study guide, sample exams up later today

Page 3: Some Graphics CS303E: Elements of Computers and Programming.

Some Graphics

Very Brief Introduction Uses Zelle’s graphics module

– Download from his website:– http://mcsp.wartburg.edu/zelle/

python/– Put in same directory as your

program

Page 4: Some Graphics CS303E: Elements of Computers and Programming.

Graphics Objects

Recall that in object-oriented programming objects consist of:– Data– Actions on that data (methods)

To use the graphics packages you:– Create objects– Perform actions on those objects

Page 5: Some Graphics CS303E: Elements of Computers and Programming.

Example:A Possible Circle Object Data

– center = (20,20)– radius = 10– interior_color =

“blue”– outline_color =

“green”

Operations– Draw myself – Move myself– Set interior color– Set outline color

Page 6: Some Graphics CS303E: Elements of Computers and Programming.

Windows

Graphics objects are drawn on windows– Windows are also objects– A single program can create multiple

windows Created using the GraphWin()

object

Page 7: Some Graphics CS303E: Elements of Computers and Programming.

GraphWin() Objects

GraphWin(title, width, height)– Constructs a new graphics window– All parameters are optional– Default size is 200x200

setBackground(color)– Sets window’s background to a color– Options: red, cyan, green, blue, purple,

yellow getMouse()

– Pause for a mouse click in the window, and return the Point at which it was clicked

close()

Page 8: Some Graphics CS303E: Elements of Computers and Programming.

Example:Creating a Windowfrom graphics import *

win = GraphWin()win2 = GraphWin(“Second”,300,300)

Page 9: Some Graphics CS303E: Elements of Computers and Programming.

Point Objects

Often used to define the position of other objects

Can also be drawn on the window

Page 10: Some Graphics CS303E: Elements of Computers and Programming.

Points Objects:Operations Point(x,y)

– Construct a point with the specified coordinates x and y

getX()– Returns the x coordinate of the point

getY()– Returns the y coordinate of the point

AND all the operations for drawable objects

Page 11: Some Graphics CS303E: Elements of Computers and Programming.

Drawable Objects

A category of objects that are drawable

All drawable objects implement all the drawable operations

Includes: Point, Line, Circle, Oval, Rectangle, …

Page 12: Some Graphics CS303E: Elements of Computers and Programming.

Drawable Objects:OperationsMethod Description

setFill(color) Sets the interior color of the object to the specified color

setOutline(color) Sets the outline of the object to the specified color

setWidth(pixels) Sets the width of the object’s outline to the specified number of pixels

draw(graphicWindow) Draws the object on the specified graphics window

undraw() Undraws the object

move(dx,dy) Moves the object dx units in the horizontal direction and dy units in the vertical directions

Page 13: Some Graphics CS303E: Elements of Computers and Programming.

Example:Point Objects

pt=Point(15,55) #construct a #point with #x=15, y=55

pt.setOutline(“purple”)pt.draw(win) #draw point on

#the window win

Page 14: Some Graphics CS303E: Elements of Computers and Programming.

Question

Which is a Boolean value?

A. TrueB. trueC. “True”D. “true”

Page 15: Some Graphics CS303E: Elements of Computers and Programming.

Circle Objects

A Circle is defined by its center coordinates (given as a Point) and its radius

Operations:– Circle(centerPoint, radius)

Constructs a circle with a specified center point and radius

– getRadius() Returns the radius of the circle

Page 16: Some Graphics CS303E: Elements of Computers and Programming.

Example:Circle Objects

cir=Circle(Point(50,100), 25) #center=(50,100), radius=25cir.setFill(“yellow”) #set

#interior to yellowcir.draw(win) #draw circle on

#window win

Page 17: Some Graphics CS303E: Elements of Computers and Programming.

Line Objects

Specified by two points Operations:

– Line(point1, point2) Construct a line from point1 to point2

– setArrow(<string>) Set arrow status of the line. Possible values for <string> are: first,

last, both, or none.

Page 18: Some Graphics CS303E: Elements of Computers and Programming.

Example:Line Objects

diag = Line(Point(0,0),Point(200,200)) #line from top left to bottom right

diag.setWidth(15) #increase thickness

diag.setOutline(“blue”) #make it blue

diag.draw(win) #draw line on window win

Page 19: Some Graphics CS303E: Elements of Computers and Programming.

Rectangle Objects

Draws a rectangle

Operations– Rectangle(point1, point2)

Constructs a Rectangle with opposite corners point1 and point2

Page 20: Some Graphics CS303E: Elements of Computers and Programming.

Example:Rectangle Objects

rec1 = Rectangle(Point(3,4),Point(8,10))

rec1.setFill(“green”)rec1.draw(win)

Page 21: Some Graphics CS303E: Elements of Computers and Programming.

Exercise

Page 22: Some Graphics CS303E: Elements of Computers and Programming.

Exercise

Page 23: Some Graphics CS303E: Elements of Computers and Programming.

Question

I’ll come to class Monday, 4/29, prepared to:

A. Take an examB. Ask questions to review for the

examC. Sleep